Mason74
September 12th, 2001, 01:44 AM
Hello! I need some help big time for a C++ project I am working on. The professor wants the class to find the average and variance for a set of scores using "sentinel controlled repletetion" structure. My answers also have to print out in double value form. Here is where the problems start though. With variance...
The teacher states that variance is calculated as by summing the squares of all the scores a user might use in a set, dividing this sum by the count (or number of scores that are placed), and then subtracting the square of the average from this amount. One problem, the professor mentions or has not taught how to define powers or squares yet! Maybe I don't need to define the "power" and "square root" but it seems as if I do.
I am having a hard time putting what I know into code to make the statement work right. I have the average already specified and working but not the variance....here is what I have thus far:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
# include <iomanip>
using std::setprecision;
using std::setiosflags;
int main()
{
int total,
scoreCounter,
score;
double average;
double variance;
double pow;
double sqrt;
total = 0;
scoreCounter = 0;
cout << "Greetings! Please enter your first score, -1 to end: \n";
cin >> score;
while (score != -1 )
{
total = total + score;
scoreCounter = scoreCounter +1;
cout << "Please enter your next score, -1 to end: \n";
cin >> score;
}
if ( scoreCounter !=0 )
{
average = static_cast< double >( total ) / scoreCounter;
cout << "The average of the scores is " << setprecision( 1 )
<< setiosflags( ios::fixed | ios::showpoint )
<< average << endl;
}
else
cout << "Sorry, no scores were entered, therefore an average can't be calculated." << endl;
if ( scoreCounter !=0 )
{
variance = static_cast< double >( WHERE I NEED HELP) / scoreCounter - sqrt( average );
cout << "The variance of the scores is " << setprecision( 1 )
<< setiosflags( ios::fixed | ios::showpoint )
<< variance << endl;
}
else
cout << "Sorry, no scores were entered, therefore a variance can't be calculated." << endl;
return 0;
Any help would be greatly appreciated. I am just trying to understand the basics here of how to solve for variance the right way.
Andrew
The teacher states that variance is calculated as by summing the squares of all the scores a user might use in a set, dividing this sum by the count (or number of scores that are placed), and then subtracting the square of the average from this amount. One problem, the professor mentions or has not taught how to define powers or squares yet! Maybe I don't need to define the "power" and "square root" but it seems as if I do.
I am having a hard time putting what I know into code to make the statement work right. I have the average already specified and working but not the variance....here is what I have thus far:
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;
# include <iomanip>
using std::setprecision;
using std::setiosflags;
int main()
{
int total,
scoreCounter,
score;
double average;
double variance;
double pow;
double sqrt;
total = 0;
scoreCounter = 0;
cout << "Greetings! Please enter your first score, -1 to end: \n";
cin >> score;
while (score != -1 )
{
total = total + score;
scoreCounter = scoreCounter +1;
cout << "Please enter your next score, -1 to end: \n";
cin >> score;
}
if ( scoreCounter !=0 )
{
average = static_cast< double >( total ) / scoreCounter;
cout << "The average of the scores is " << setprecision( 1 )
<< setiosflags( ios::fixed | ios::showpoint )
<< average << endl;
}
else
cout << "Sorry, no scores were entered, therefore an average can't be calculated." << endl;
if ( scoreCounter !=0 )
{
variance = static_cast< double >( WHERE I NEED HELP) / scoreCounter - sqrt( average );
cout << "The variance of the scores is " << setprecision( 1 )
<< setiosflags( ios::fixed | ios::showpoint )
<< variance << endl;
}
else
cout << "Sorry, no scores were entered, therefore a variance can't be calculated." << endl;
return 0;
Any help would be greatly appreciated. I am just trying to understand the basics here of how to solve for variance the right way.
Andrew