Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,140 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,063 people online right now. Registration is fast and FREE... Join Now!




Standard Deviation

 
Reply to this topicStart new topic

Standard Deviation, HomeworkHelp

MGS12345
post 7 Oct, 2008 - 03:49 PM
Post #1


New D.I.C Head

*
Joined: 7 Oct, 2008
Posts: 2

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!
Attached File  golfdataSML.txt ( 126bytes ) Number of downloads: 1


Mod Edit - Fixed code tags
User is offlineProfile CardPM

Go to the top of the page

GWatt
post 7 Oct, 2008 - 04:48 PM
Post #2


human inside

Group Icon
Joined: 1 Dec, 2005
Posts: 2,150



Thanked 16 times

Dream Kudos: 450
My Contributions


Wikipedia defines the standard deviation of a discrete set of numbers, which is what you have, as thus:

square root of the sum of the square of the difference between the average and each data point divided by the number of data points.
algorithm

foreach number in data
differenceSumSquared += (number - data average)^2

standardDeviation = squareRoot(differenceSumSquare / data count)


Just for clarity, I would suggest moving the standard deviation to its own function, just so that all of the code for it is in pretty much the same place. Right now you have little bits of everything all over the place, making it slightly difficult to understand.
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 12:31PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month