Home > Mobile >  Avergaging elements in array
Avergaging elements in array

Time:12-06

I'm trying to add the elements in an array. It's just a simple program to calculate the average of student grades. I know this is probably a rudimentary way to code this, I'm looking to do it more efficiently. However my code is not returning the average. I would greatly appreciate any help. I did try this with a for loop but got the same incorrect answer.

#include <stdio.h>
int main()
{
  int grades[6];
  int average;
  int sum = 0;
  printf("Please enter your five test scores:\n");
  scanf("%d", &grades[0]);
  scanf("%d", &grades[1]);
  scanf("%d", &grades[2]);
  scanf("%d", &grades[3]);
  scanf("%d", &grades[4]);
  scanf("%d", &grades[5]);
                              
  sum = sum   grades[6];  
  average = sum / 5;
  printf("The average of the students test scores is %d:\n", average);
                                                                      
  return 0;
}

CodePudding user response:

You should sum all grades and then divide by their amount (in your case it is 6 not 5, because grades array has 6 elements). Here is a code sample:

#include <stdio.h>
int main()
{
  int grades[6];
  int average;
  int sum = 0;
  printf("Please enter your six test scores:\n");
  scanf("%d", &grades[0]);
  scanf("%d", &grades[1]);
  scanf("%d", &grades[2]);
  scanf("%d", &grades[3]);
  scanf("%d", &grades[4]);
  scanf("%d", &grades[5]);
                              
  for (int i = 0; i < 6; i  )
      sum = sum   grades[i];

  average = sum / 6;
  printf("The average of the students test scores is %d:\n", average);
                                                                      
  return 0;
}

CodePudding user response:

I'm trying to add the elements in an array.

This is maybe already a wrong track regarding the title (and the code) where it is about averaging elements in an array.

How you build the array is up to you; I choose the simplest way. An important decisions is: is size of array and number of values the same? That is what n_grades does. The four zeroes in the array initialization illustrate the difference.

An average most likely should be a floating point number.

A nasty problem with averages is the lurking overflow. Very unlikely in this setting, but there is a more robust (and elegant) algorithm. The (double) cast is the un-elegant part, but is needed because the division is between two integers. Still this is the compact core:

  for (i = 0; i < n_grades; i  )
       aver  = (double) grades[i] / n_grades;

corresponding to the math formula:

    i<n
A = Sum G_i/n
    i=0

("Sum" is the big Sigma)

#include <stdio.h>
int main()
{
  int grades[] = {10,10,9,10,11,11,0,0,0,0};

  int n_grades = sizeof grades / sizeof*grades;   // use all elements
  //n_grades = 6;                                 // use only first n elements

  double aver = 0;
      
  for (int i = 0; i < n_grades; i  )
       aver  = (double) grades[i] / n_grades;

  printf("The average of the students test scores is %f (n= %d):\n", 
          aver, n_grades);

  return 0;
}

Now it prints:

The average of the students test scores is 6.100000 (n= 10):

or, uncommented, i.e. limited to 6 grades::

The average of the students test scores is 10.166667 (n= 6):

CodePudding user response:

You're assuming that grades[6] holds the sum of all the values in the grades array, which is wrong of course.

You need something like:

for (int i = 0; i < 6; i  )
    sum = sum   grades[i];
  • Related