by Dinesh Thakur Category: Array

For a list of numbers x whose elements are referred to as x0, x1,…..., xn-1, the mean bar x and the standard deviation a are defined as

                           

The program given below calculates the values of bar x and sigma. It uses array x of type float to store the given numbers (maximum 50) and variable n of type int to represent the number of elements in array x. Also, the variables sum, mean and sd (all of type float) are used to represent the sum, mean and standard deviation, respectively, of the given numbers.

/* Calculate mean and standard deviation */

#include <stdio.h>

#include <math.h>

void main()

{

       float x [50];             /* max. 50 elements in array x */

       int n;                       /*number of elements in array x */

       float sum, mean, sd;  /* sum, mean and standard deviation.*/

       int i;

       clrscr();

        /* read data */

       printf("How many numbers (max 50)? ");

       scanf("%d", &n);

       printf ("Enter numbers: ");

       for(i = 0; i<n; i++)

           scanf("%f", &x[i]);

           /* calculate mean */

           sum= 0.0;

      for(i = 0; i < n; i++)

           sum+= x[i];

           mean = sum / n;

      /* calculate standard deviation */

           sum= 0.0;

      for(i = 0; i < n; i++)

           sum+= (x[i] - mean) * (x[i] - mean);

           sd = sqrt(sum / n);

           printf("Mean = %6.3f\n", mean);

           printf("Standard Deviation: %6.3f\n", sd);

           getch();

}

        C program to calculate the mean and standard deviation



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles