Mean Program In C


Advertisements

Mean is an average value of given set of numbers. It is calculated similar to that of average value. Adding all given number together and then dividing them by the total number of values produces mean.

For Example − Mean of 3, 5, 2, 7, 3 is (3 + 5 + 2 + 7 + 3) / 5 = 4.

Algorithm

We can draw its algorithm in following steps −

START
   Step 1 → Take an integer set A of n values
   Step 2 → Add all values of A together
   Step 3 → Divide result of Step 2 by n
   Step 4 → Result is mean of A's values
STOP

Pseudocode

We shall now write pseudocode for the above mentioned algorithm.

procedure mean()
   
   Array A
   FOREACH value i of A DO
      sum = sum + i
   ENDFOR
   MEAN = sum / n

end procedure

Implementation

Implementation of this algorithm is given below −

#include <stdio.h>

int main() {
   float mean;
   int sum, i;
   int n = 5;
   int a[] = {2,6,7,4,9};

   sum = 0;

   for(i = 0; i < n; i++) {
      sum+=a[i];
   }

   printf("Mean = %f ", sum/(float)n);

   return 0;
}

Output

Output of the program should be −

Mean = 5.600000
mathematical_programs_in_c.htm
Advertisements