Forums: » Register « |  Free Tools |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

New Free Tools on Dev Shed!

#1
September 3rd, 2012, 12:18 PM
Registered User

Join Date: Sep 2012
Posts: 1
Time spent in forums: 2 m 19 sec
Reputation Power: 0

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?

#include <stdio.h>

int main()
{
int matrix[50][50]={{0}};
int i, j, sum=0, max=0, min=250, class_average, num_classes,class_size;
int avg_array[50]={0};

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 (i=0;i<num_classes;++i)
{
if(avg_array[i]>max)
max=avg_array[i];
}
printf("Class number %d - you have been chosen to represent us at Brobdingnag!\n",max);

/*Calculate min*/
for (i=0;i<num_classes;++i)
{
if (avg_array[i]<min)
min=avg_array[i];
}
printf("Class number %d - you have been chosen to represent us at Lilliput!\n", min);

return 0;
}

#2
September 3rd, 2012, 02:28 PM
 bdb
Contributing User

Join Date: Aug 2012
Posts: 156
Time spent in forums: 1 Week 15 h 48 m 11 sec
Reputation Power: 33
You need to reset sum.

I'm sure min only worked because you were unlucky. Try swapping the first and second class: my guess is that the min will now be the average of the 2nd (now 1st) class.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Help please!