Calculation of Sum, Average and Standard Deviation using Functions and Pointers.
Write a function that receives 5 integers and returns the sum, average and standard
deviation of these numbers. Call this function from main() and print the results in main().
#include<stdio.h>
#include<math.h>
int calc (float a, float b, float c, float d, float e, float *sum, float *avg,float *sd);
int main()
{
float a, b, c, d, e, sum=0.0, avg=0.0;
float sd=0.0;
printf("Enter Five Numbers:");
scanf("%f %f %f %f %f",&a,&b,&c,&d,&e);
calc (a, b, c, d, e, &sum, &avg, &sd);
printf("\nSum=%f", sum);
printf("\nAverage=%f", avg);
printf("\nStandard Deviation=%f\n", sd);
getchar();
return 0;
}
calc (float a, float b, float c, float d, float e, float *sum, float *avg, float *sd)
{
float Calc=0.0;
*sum = a+b+c+d+e;
*avg = *sum / 5.0;
Calc += ( a - *avg) * ( a - *avg);
Calc += ( b - *avg) * ( b - *avg);
Calc += ( c - *avg) * ( c - *avg);
Calc += ( d - *avg) * ( d - *avg);
Calc += ( e - *avg) * ( e - *avg);
*sd = sqrt((double)Calc/5.0);
}
Notes for calculation of Standard Deviation:
http://es.wikipedia.org/wiki/Standard_deviation
The program can be found at:
Download File
deviation of these numbers. Call this function from main() and print the results in main().
#include<stdio.h>
#include<math.h>
int calc (float a, float b, float c, float d, float e, float *sum, float *avg,float *sd);
int main()
{
float a, b, c, d, e, sum=0.0, avg=0.0;
float sd=0.0;
printf("Enter Five Numbers:");
scanf("%f %f %f %f %f",&a,&b,&c,&d,&e);
calc (a, b, c, d, e, &sum, &avg, &sd);
printf("\nSum=%f", sum);
printf("\nAverage=%f", avg);
printf("\nStandard Deviation=%f\n", sd);
getchar();
return 0;
}
calc (float a, float b, float c, float d, float e, float *sum, float *avg, float *sd)
{
float Calc=0.0;
*sum = a+b+c+d+e;
*avg = *sum / 5.0;
Calc += ( a - *avg) * ( a - *avg);
Calc += ( b - *avg) * ( b - *avg);
Calc += ( c - *avg) * ( c - *avg);
Calc += ( d - *avg) * ( d - *avg);
Calc += ( e - *avg) * ( e - *avg);
*sd = sqrt((double)Calc/5.0);
}
Notes for calculation of Standard Deviation:
http://es.wikipedia.org/wiki/Standard_deviation
The program can be found at:
Download File
Comments
http://www.a2zhacks.blogspot.com to learn C,C++,VB programming.
http://mycodelog.com/2010/03/27/c-aggregates/