C program to calculate mean using arrays

The following program takes n numbers as an input from the user that is stored in an array and mean is calculated (c program to calculate mean using arrays).

The mean is the average value of the data items. The mean is equal to the total of all the data items divided by the number of data items. The formula to calculate mean is:

Mean = sum / number of data items

Please go through following C programming articles to understand the concept of the program.

C program to calculate average or mean using arrays

#include <stdio.h>
#define SIZE 100

int main()
{
   int i, n, sum = 0;
   int data[ SIZE ];
   float mean;

   printf("Enter the number of data items: ");
   scanf("%d", &n);

   if (n > SIZE || n <= 0)
   {
      printf("Error!!! Please enter the number in range of (0 - 100): ");
      scanf("%d", &n);
   }

   //taking data items from user
   printf("Enter data:\n");
   for (i = 0; i < n; ++i)
   {
      scanf("%d", &data[ i ]);
      sum += data[ i ];
   }

   mean = sum / (float)n;
   printf("Mean: %.4f", mean);

   return 0;
}

[adsense1]
Output
C program to calculate mean using arrays

Explanation
In the above program, we have defined the maximum size of an array to be 100 using macro SIZE.

Now if condition checks whether the number of data items entered by the user are in the range of 0 – 100 or not.

Using for loop program takes data entered by user and calculate sum.
Finally, an average of the data item is calculated using formula.