I'm trying to accept the input of a matrix, calculate the average of ever row and see what the largest and smallest row average is.
For some reason min works but max doesnt, any ideas?
Code:
#include <stdio.h>
int main()
{
int matrix[50][50]={{0}};
int i, j, a, b, sum=0, max=0, min=250, class_average, num_classes,class_size;
int avg_array[50];
printf("Please Enter the number of classes: ");
scanf("%d",&num_classes);
printf("Please Enter the size of each class: ");
scanf("%d",&class_size);
/*input matrix*/
for(i = 0; i < num_classes; ++i){
for(j = 0; j < class_size; ++j)
scanf("%d", &matrix[i][j]);
}
/*Go over rows and calculate average*/
for(i=0;i<num_classes;++i)
{
for(j=0; j<class_size;++j)
{
sum+=matrix[i][j];
}
class_average=(sum/class_size);
avg_array[i]=(class_average);
}
/*Calculate max*/
for (a=0;a<num_classes;++a)
{
if(avg_array[a]>max)
max=avg_array[a];
}
printf("Class number %d - you have been chosen to represent us at Brobdingnag!\n",max);
/*Calculate min*/
for (b=0;b<num_classes;++b)
{
if (avg_array[b]<min)
min=avg_array[b];
}
printf("Class number %d - you have been chosen to represent us at Lilliput!\n", min);
return 0;
}