Thursday, 8 August 2013

Chapter 5 solutions Problem[F][b]

Question-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( ). 
Solution- Totally mathematical Problem. an easy one. check it out. i used call by reference for the sake of simplicity.

#include<stdio.h>
#include<math.h>

int main()
{
int a=2,b=3,c=5,d=6,e=9,sum;  /* i am taking int , you can choose float as well if you wish */
float avg,stdev; 
printf("Enter any 5 integers\n");
scanf("%d%d%d%d%d",&a,&b,&c,&d,&e);
statistics(&a,&b,&c,&d,&e,&sum,&avg,&stdev);
printf("Sum=%d\nAvg=%f\nStandard Deviation=%f\n",sum,avg,stdev);
return 0;
}
statistics(int *a,int *b,int *c,int *d,int *e,int *sum,float *avg,float *stdev)
{
*sum=*a+*b+*c+*d+*e;
*avg=*sum/5;
*stdev=sqrt(pow(*avg-*a,2)+pow(*avg-*b,2)+pow(*avg-*c,2)+pow(*avg-*d,2)+pow(*avg-*e,2))/2.236;       /*Square root of 5, i am just following the definition */
return(*sum,*avg,*stdev);
}

No comments:

Post a Comment