Sunday, July 12, 2009

C++ Function to compute average and standard deviation?

The books asks me to the following: Write a function that computes the average and standard deviation of four scores. The standard deviation is defined to be the square root of the average of the four values: (si - a)2 where a is the average of the four scores s1, s2, s3, and s4. The function will have six parameters and will call two other functions. Embed the function in a driver program that allows you to test the function again and again until you tell the program you are finished.





I am given a program:


http://www.forex1000.com/codegiven.txt





But my professor said:


http://www.forex1000.com/codeasked.txt








I am unsure of where to make the corrections for half of the parameters are gone... Any help or any hint toward what to do would be greatly appreciated.





Thanks

C++ Function to compute average and standard deviation?
Actually, you've got everything you need - all you need to do is write a driver that inputs the numbers (say, n1, n2, n3, n4), and then you can call the following:





double avg = myMean(n1,n2,n3,n4);


double stdev = myStdev(n1,n2,n3,n4);


displayStats(n1,n2,n3,n4);





There is some redundancy here though. Typically, all you'd really have to do is call the displayStats function and that would call the others:





double myStdev(double n1, double n2, double n3, double n4)


{





double avg = myMean(n1, n2, n3, n4);





// Calculate stdev below using four numbers and average


double stdev;


...





return stdev;


}





void displayStats(double n1, double n2, double n3, double n4)


{





double avg = myMean(n1, n2, n3, n4);


double stdev = myStdev(n1, n2, n3, n4);





cout %26gt;%26gt; "\nAverage = " %26gt;%26gt; avg %26gt;%26gt; "." %26gt;%26gt; endl;


cout %26gt;%26gt; "\nStandard Deviation = " %26gt;%26gt; stdev %26gt;%26gt; "." %26gt;%26gt; endl;





}


No comments:

Post a Comment