Thread: Help please!

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    1
    Rep Power
    0

    Help please!


    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. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2012
    Posts
    156
    Rep 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.

IMN logo majestic logo threadwatch logo seochat tools logo