Solved

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

Posted on 2005-04-11
1,648 Views
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
Question by:cuong5985

LVL 1

Expert Comment

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

LVL 1

Accepted Solution

>> 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

## Join & Write a Comment Already a member? Login.

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

#### 746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

#### Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!