IHYPRESS.NET > C PROGRAMMING
HOME | ASP | C | HTML/CSS | JAVASCRIPT | MATLAB | PERL | PHP | PYTHON | RUBY
C > 07. Arrays I (1D) [PROGRAM #03]
CODERESULT
<
/* This program computes the meand and standard deviations of 
the values inside an array */
#include <stdio.h>
#include <math.h>
#define MAX 5

int 
main (void)
{

	double mean, sd, sum, sumsq;
	double x[] = {10.0, 15.0, 20.0, 10.0, 30.0};
	int i;

	sum = 0;
	sumsq = 0;

	/* computing the sum and sum of squares */
	for (i=0; i<MAX; ++i)
	{
		sum = sum + x[i];
		sumsq = sumsq + x[i] * x[i];
	}


	/* computing mean and standard deviation */
	mean = sum / MAX;
	sd = sqrt(sumsq / MAX - mean * mean);


	/* printing report */
	printf ("The mean is %lf. \n", mean);
	printf ("The standard deviation is %lf. \n", sd);
	
	return (0);
}
 

Legend: preprocessor directives | variable declarations main function | helper functions | user-defined structures comments
The mean is 17.000000.
The standard deviation is 7.483315.
>
COPYRIGHT © 2019 IHYPRESS.NET. A DIVISION OF IHY PRESS, KIRKLAND, WA 98083 USA. ALL RIGHTS RESERVED.
Deals of the Week: New deals every week, online only!
SHARE