I am having a lot of trouble getting standard deviation to work on my program.
The data from the input is:
1 6 1 3 6 2 1 4 3 2 4
2 3 2 2 2 2 4 4 4 2 2
3 -1 3 3 4 5 3 4 2 5 2
4 2 4 5 4 3 4 1 3 5 4
but it is saved as a .txt file.
I am pretty sure that my problem is with the formula. My average score is printing correctly, so if there is any way that you can help me with the formula that would be great.
cpp
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>
using namespace std;
/* Setting up my proper funcations that the the
program will run. */
int main ()
{
int playerNumber, lowestScore, highestScore,
handicap,score, total, result, runningTotal,
sqrdRunTot, numberOfPlayers, winningPlayer;
/* Setting up all of my variables that will be
intergers, or numbers. */
numberOfPlayers = 0;
runningTotal = 0;
lowestScore = 1000;
highestScore = 0;
/* These will set the variable numbers,
these numbers will be reset throughout
the program */
double averageScore, sqrdAvrg, standDevi,
standDeviNum, totScoSqrd, alltotalsSqrd;
alltotalsSqrd = 0;
/* The double values, because all of these deal
with averageScore, which is a double */
bool scoresValid = true;
ifstream inputFile;
string fileName;
cout << "Enter the name of the file:";
cin >> fileName;
inputFile.open(fileName.c_str());
/* The command that prompts you to enter the file,
and the file is being opened */
while (!inputFile)
{
inputFile.clear ();
cout << "Invalid Entry" << endl;
cout << "Enter the name of the file:";
cin >> fileName;
inputFile.open(fileName.c_str());
}
/*This command is used for correcting if the
data file is false */
cout << "Player" << setw (10) << "Handicap" << setw (20) <<
"Scores" << setw (18) << "Total" << setw(8) << "Result"
<< setw (15) << "Under Par?" << endl;
cout << "------" << setw (10) << "--------" << setw (30) <<
"-------------------------" << setw (8) << "-----"
<< setw (8) << "------" << setw (15) << "-----------" << endl;
/* This is the display for the heading from the data file. */
inputFile >> playerNumber;
inputFile >> handicap;
while (inputFile)
{
total = 0;
cout << setw (5) << playerNumber << setw(5);
cout << setw (10) << handicap << " ";
if (handicap < 0)
{
scoresValid = false;
}
/* Reads in the Player Number and the Handicap
from the data file */
for (int i = 0; i < 9; i++)
{
inputFile >> score;
cout << setw (3) << score;
if( score < 0 )
{
scoresValid = false;
}
total = total + score;
}
/* Reading in the scores by using a for loop
because the scores all all 9 of the
following numbers */
result = total - handicap;
/* Calculating the result from the total amount counted,
and then subtracting the handicap */
if (scoresValid)
{
numberOfPlayers++;
runningTotal = runningTotal + result;
totScoSqrd = pow(total,2.0);
alltotalsSqrd = alltotalsSqrd + totScoSqrd;
cout << setw (7) << total;
cout << setw (7) << result;
if (result <= 30)
{
cout << setw (14) << "Yes" << endl;
}
if (result > 30)
{
cout << setw (14) << "No" << endl;
}
/* Stating that if the data is true according to the
expression in the first if statement. Print
the total, result, and if the player is
under par. Every time this is calculated the
player number increases by one and the
Running Total will increase accordingly.*/
}
else
{
cout << " ***** Invalid Data *****" <<endl;
scoresValid = true;
}
/* States what to do if the if statement above is false. */
while (result < lowestScore)
{
lowestScore = result;
winningPlayer = playerNumber;
}
while (result > highestScore)
{
highestScore = result;
}
inputFile >> playerNumber;
inputFile >> handicap;
} // end while
/* Calculates te lowest and highest scores from
the data entered above. */
averageScore = (runningTotal/numberOfPlayers);
/* The line that will calculate the average score
that all of the players made. */
cout << "Number of Players Counted in Results: " << numberOfPlayers << endl;
cout << "Lowest Score:" << lowestScore << endl;
cout << "Highest Score: " << highestScore << endl;
cout << "Average Score: " << averageScore << endl;
/* Prints the above calcualtions out for the user
to read. */
sqrdRunTot = runningTotal * runningTotal;
sqrdAvrg = (sqrdRunTot/(numberOfPlayers * 9));
standDeviNum = alltotalsSqrd - sqrdAvrg;
standDevi = sqrt(standDeviNum/(numberOfPlayers * 9));
/* The calculations used for standard Deviation. */
cout << "Standard Deviation: " << standDevi << endl;
/*Prints out the result for Standard Deviation */
cout << "Winning Player Number: " << winningPlayer << endl;
/* Prints out the wininng player and their number */
return 0;
/* Ends my program */
}
Thanks,
MG
P.S here is a data file attached!
golfdataSML.txt ( 126bytes )
Number of downloads: 1Mod Edit - Fixed code tags