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");
}
}
iand the inner looping overj(as is common), then nothing says that you need to usematrix[i][j], you could just as well domatrix[j][i].highest_mark[i]but assigning the new value tohighest_mark[j]