0

Please can someone explain to me how to iterate through a two dimensional array column wise i.e keeping the row constant until we reach the end of the column? Thanks.

I'm writing a program to calculate scores of some subjects in a class of students. It calculates the total marks obtained by each student and also the highest mark in each subject.

The tricky part i'm having difficulty is calculating the lowest mark in each subject. This will require to iterate downwards each column and change the row only after getting the highest score in that column(subject)

The code (also - http://pastebin.com/FmP4yX59 ):

#include <stdio.h>
#include <stdlib.h>
#define MAX_ST 3
#define MAX_SU 3

int main() {
  int student[MAX_ST][MAX_SU];

  int total_mark[MAX_ST];
  printf("\n_________________PRESS TAB KEY TO SHIFT A COLUMN_________________\n");
  printf("\nRoll_NO   subject1    subject2    subject3\n");
  printf("________________________________________________________________\n");
  for(int i = 0; i < MAX_ST; i++) {
    printf("%d\t|\t",i+1);
    for(int j = 0; j < MAX_SU; j++){
      scanf("%d",&student[i][j]);
    }
  }
  //total marks obtained by each student
  printf("\nTOTAL MARKS\n");
  for(int i = 0; i < MAX_ST; i++) {
    total_mark[i] = 0;
    for(int j = 0; j < MAX_SU; j++) {
      total_mark[i] += student[i][j];
    }
    printf("Student %d = %d\n",i+1, total_mark[i]);
  }

  //highest total mark
  int overRoll = 0;
  int high_S = 0;
  for(int i = 0; i < MAX_ST; i++) {
    if(high_S <= total_mark[i]) {
      high_S = total_mark[i];
      overRoll = i;
    }
  }

  printf("Highest total marks is %d for Student %d\n", high_S,overRoll + 1);

  //highest mark in each subject
  //where the problem lies
  int highest_mark[MAX_SU];
  int highest_sudent[MAX_SU];

  for(int i  = 0; i < MAX_SU;i++){
    for(;j = t;) {
      Highest_mark[i] = 0;
      if(student[i][j] > highest_mark[i]) {
        highest_mark[j] = student[][j];
        highest_student[i] = i;
      }
    }
    printf("the highest");
  }
}
4
  • 4
    Don't post links to code, links can go stale and make your question worthless. We also don't need to see code that is irrelevant to the question and the problem you're having. Instead try to create a Minimal, Complete, and Verifiable Example and show in the body of the question. You might also want to read about how to ask good questions. Commented Jan 19, 2016 at 8:43
  • 1
    As for your problem, if you have two nested loops, the outer looping over i and the inner looping over j (as is common), then nothing says that you need to use matrix[i][j], you could just as well do matrix[j][i]. Commented Jan 19, 2016 at 8:46
  • Note that the loop is comparing mark with highest_mark[i] but assigning the new value to highest_mark[j] Commented Jan 19, 2016 at 8:52
  • Thank guys for all your help. Commented Jan 19, 2016 at 11:47

3 Answers 3

1

Typically you'd do something like this:

typedef void action_t (int*);

void traverse_matrix ( size_t col, 
                       size_t row, 
                       int matrix[col][row],
                       action_t* action )
{
  for(size_t x=0; x<col; x++)
  {
    for(size_t y=0; y<row; y++)
    {
      action(&matrix[x][y]);
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

You have rows and cols mixed up.
@SelçukCihan Nope. If you wish to define cols as inner-most or outer-most dimension is entirely up to the programmer. C doesn't care, all the [][] syntax tells C is which memory cell to access. I think (x, y) is a more intuitive coordinate system than (y, x), since the former is compatible with formal mathematics.
0
#define rows 5
#define columns 4    

int matrix[rows][columns];

int r, c;

for (c = 0; c < columns; c++)
{
    for (r = 0; r < rows; r++)
    {
        matrix[r][c]; \\ the data
    }
}

Comments

0

thanks guys for helping me figure it out

int highest_mark[MAX_SU];
int highest_student[MAX_SU];
int curr_sub;
for(int i  = 0; i < MAX_SU; i++)        
{

       highest_mark[i] = 0;
    for(int j = 0; j < MAX_ST; j++)
    {

            if(student[j][i] >  highest_mark[i])
            {
                highest_mark[i] = student[j][i];
                highest_student[i] = j+1;
                curr_sub = i+1;
            }

    }
     printf("the highest score subject %d is %d  by student %d\n", curr_sub, highest_mark[i],highest_student[i]);   
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.