• Status: Solved
• Priority: Medium
• Security: Public
• Views: 1905

# program to computes the average and standard deviation of four scores.

Topic: 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: (s - a)^2 where a is average of the four scores s1, s2, s3,s4.  The function wil 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.

This is my program.

#include <iostream>
#include <cmath>

using namespace std;

void inputScores (double& s1, double& s2, double& s3, double& s4);
// Precondition: The user is ready to enter four numbers.
// Postcondition: The values of s1, s2, s3, and s4 are set to the
// four numbers entered by the user.

void computeStats (double s1, double s2, double s3, double s4,
double& avg, double& stdev);
// Precondition: The parameters s1, s2, s3, and s4 contain four numbers.
// Postcondition: The parameters avg and stdev have been set to
// the average and standard deviation, respectively, of the four numbers.

double average (double s1, double s2, double s3, double s4);
// Precondition: The parameters s1, s2, s3, and s4 contain four numbers.
// Returns the average of s1, s2, s3, and s4.

double standardDeviation (double s1, double s2, double s3, double s4, double avg);
// Precondition: The parameters s1, s2, s3, and s4 contain four real numbers
// and the parameter avg contains the average of the four numbers.
// Returns the standard deviation of s1, s2, s3, and s4.

void printStats (double s1, double s2, double s3, double s4,
double avg, double stdev);
// Precondition: The parameters avg and stdev contain the average
// and standard deviation of the four numbers s1, s2, s3, s4.
// Postcondition: The four numbers along with their average and
// standard deviation have been printed to the screen.

int main ()
{

double score1, score2, score3, score4;
double avg, stdev;
char ans;

do {

inputScores (score1, score2, score3, score4);
computeStats (score1, score2, score3, score4, avg, stdev);
printStats (score1, score2, score3, score4, avg, stdev);

cout << endl;
cout << "Try four more numbers (y or n)? ";
cin >> ans;
cout << endl;
}
while (ans == 'y' || ans == 'Y');

return 0;
}

void inputScores (double& s1, double& s2, double& s3, double& s4)
{
cout << "Enter four scores, separated by blank spaces: ";
cin >> s1 >> s2 >> s3 >> s4;
cout << endl;
}

void computeStats (double s1, double s2, double s3, double s4,
double& avg, double& stdev)
?????????????????????????????????????????????

void printStats (double s1, double s2, double s3, double s4,
double avg, double stdev)
{
cout << "The average of " << s1 << ", " << s2 << ", " << s3
<< ", and " << s4 << " is " << avg
<< " and the standard deviation is " << stdev << endl;

}

I dont know what to do with the code at shown????????????????????? Can someone help me with this, thanks
0
cuong5985
• 2
1 Solution

Commented:
cuong5985,
so you're asking what the code should look like inside the Average Function and the StandardDeviation Function (given 4 numbers provided by the user)?

Your <computeStats > function would be something simple like this:
-----------------------------------------------------------------------------------------------------------
void computeStats (double s1, double s2, double s3, double s4, double& avg, double& stdev)
{
// Precondition: The parameters s1, s2, s3, and s4 contain four numbers.
// Postcondition: The parameters avg and stdev have been set to
// the average and standard deviation, respectively, of the four numbers.

avg = average (s1, s2, s3, s4);
stdev = standardDeviation (s1, s2, s3, s4, avg);
}

------------------------------------------------------------------------------------
The average is the sum of all numbers divided by the number count.

example input:
1 39 84 23

Average = (1+39+84+23) / 4
Average = 36.75
------------------------------------------------------------------------------------

The Standard-Deviation is a little more complicated, and I don't have that info with me at the current moment, so I can get back to you on that. Most likely in the next day or so.

Hope this helps a little for you. I'll get back to you asap for more help.
cnesb860
0

Commented:
>> The standard deviation is defined to be the square root of the average of the four
>> values: (s - a)^2 where a is  average of the four scores s1, s2, s3,s4.

If that is the Standard Deviation equation you're usuing, in pseudo-code... here would be a coded version of it (I think).
stdev = sqrt(avg);

// so the function-definition could look something like this...
double standardDeviation (double avg)
{
// Precondition: The parameters s1, s2, s3, and s4 contain four real numbers
// and the parameter avg contains the average of the four numbers.
// <avg has already been calculated, so s1, s2, s3, and s4 are NOT necesary parameters.
// Returns the standard deviation of s1, s2, s3, and s4.

// in my last post, you see that <avg> was calculated before standardDeviation() was called.
// therefore s1, s2, s3, and s4 are unnecesary parameters.
return sqrt(avg);
}

// and, your function-call could look something like this...
stdev = standardDeviation (avg);

hope this elps you out. If that isn't the stdev equation that you were looking for, let me know and I can muster up a better one for you, no prob.

cnesb860
0

## Featured Post

• 2
Tackle projects and never again get stuck behind a technical roadblock.