Write a function named ComputeStats( ) that takes four arguments and returns voi
ID: 3628383 • Letter: W
Question
Write a function named ComputeStats( ) that takes four arguments and returns void.The first two arguments are an array of integers and the size of the array. The array
holds the data for which the function will compute the average and the standard
deviation. The next two arguments are reference arguments which return the computed
average and standard deviation to the calling function, main().
The standard deviation is defined to be the square root of the average of the values:
( )2
si - a , where a is the average of the values in the array and the si are the individual
values. That is, the standard deviation = ( )2
1
1 n
i
i
s a
n =
S - .
Note: The average of { 2, 6, 8, 10, 15 } is 8.2 and the standard deviation is 4.308
Explanation / Answer
void ComputeStats(int a[],int k, double &avg,double &sd)
{
double sum = 0;
for(int i=0; i<k; i++)
sum = sum + a[i];
avg =sum/(double)k;
sum = 0;
for(int i=0; i<k; i++)
sum = sum + (a[i]-avg)*(a[i]-avg);
sd = sum/(double)k;
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.