Mean, median, mode and standard deviation in C
=======================================
Mean
=====
#include <stdio.h>
#include <conio.h>
void main ()
{
int n,i,a[25];
float mean,sum=0.0;
clrscr ();
printf("How many numbers are there for calculating the mean?: ");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("[%d]. ",i+1);
scanf("%d",&a[i]);
sum=sum+a[i];
}
printf("\nThe sum is: %f\n",sum);
mean=sum/n;
printf("\nThe mean is: %f",mean);
getch ();
}
Median & Mode
=============
#include <stdio.h>
#include <conio.h>
void main ()
{
float x,m,z;
clrscr ();
printf("Enter the value of Mean: ");
scanf("%f",&x);
printf("Enter the value of median: ");
scanf("%f",&m);
z=3*m-2*x;
printf("The value of Mode is: %f",z);
getch ();
}
Standard deviation
===============
#include <stdio.h>
#include <math.h>
#include <conio.h>
#define MAX 100
void main ()
{
int i,n=0;
float value[MAX],deviation,sum=0.0,sumsqr=0.0,x,v,std;
clrscr ();
printf("\nEnter value of enter -1 to exit:\n");
for (i=0;i<MAX;i++)
{
scanf("%f",&value[i]);
if (value[i] == -1)
break;
sum += value[i];
n++;
}
x = sum/(float)n;
for (i=1;i<=n;i++)
{
deviation = value[i] - x;
sumsqr += deviation*deviation;
}
v = sumsqr/(float)n;
std = sqrt(v);
printf("Number of items: %d\n",n);
printf("Mean: %f\n",x);
printf("Standard Deviation: %f",std);
getch ();
}
No comments:
Post a Comment