one line of code

please help. I can't figure out just one line of code. Standard deviation calculation. I calculate the mean, which is correct, then subtract the mean from each of the values and square the result. Then I try to find the mean of square result. I add them up - and that's where it adds up to wrong number. I spend hours, and couldn't help myself . Any hint?

here is my file info:

Maria_Lenova 5 9 1987 4.0
Peter_Collins 5 6 1990 3.65
Leonardo_Vinchi 6 7 1978 3.8
Marc_Pincus 5 8 1960 4.0


and code:
#include <iomanip>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <stdlib.h>
#include <ctime>
#include <cmath>
using namespace std;



////////////////////////CREATE TWO STRUCTURES/////////////////////////////////
struct Date
{ int month;
int day;
int year;
} ;
struct Student
{ string name;
Date dob;
double GPA;
}S[200];


int main()
{
int i = 0;
double meanGPA = 0;
double meanAGE = 0;
double sum = 0;
double ssum = 0;
double sum1 = 0;
double mmeanAge = 0;
double sigmaGPA;
double sigmaAGE;
double coeffAGE;
double coeffGPA;


////////////////////////READ FROM THE FILE ////////////////////////////////////
ifstream rData;
rData.open ("student.txt", ios::out);


int n = 0;


while(rData >> S[n].name >> S[n].dob.day >>S[n].dob.month >> S[n].dob.year
>> S[n].GPA) n++;


/////////////////////////////PROCESS ARRAY/////////////////////////////////////
for(;i < n; i++)
{
sum += S[i].GPA;
meanGPA = sum/n; //mean
ssum += ((S[i].GPA-meanGPA)*(S[i].GPA- meanGPA)); // sum of squared differences





sum1 += (2013 - S[i].dob.year);
meanAGE = sum1/n;


////////////THIS ------------------------------LINE!!!!!!///////////////////
mmeanAge += ((2013-S[i].dob.year) - meanAGE)*((2013-S[i].dob.year) - meanAGE); // sum of squared differences

}

ssum = ssum/n; //mean of squared differences

cout << mmeanAge <<endl;


mmeanAge = mmeanAge/n; //mean of squared differences

sigmaGPA = sqrt(ssum);
sigmaAGE = sqrt(mmeanAge);


/////////////////COEFFECIENT OF VARIANCE: (STD. DEV/ MEAN)///////////////////
coeffGPA = sigmaGPA / meanGPA;
coeffAGE = sigmaAGE / meanAGE;




///////////////////////////REPORT///////////////////////////////////////////

cout <<"\n\n\n\n\n\nREPORT ON AGE:\nmean: " << meanAGE << "\nstandard deviation: "
<< sigmaAGE << "\ncoeffecient of variation " << coeffAGE;


cout <<"\n\nREPORT ON GPA:\nmean: " << meanGPA << "\nstandard deviation: "
<< sigmaGPA << "\ncoeffecient of variation " << coeffGPA;
}

1043.38






REPORT ON AGE:
mean: 34.25
standard deviation: 16.1507
coeffecient of variation 0.471552

REPORT ON GPA:
mean: 3.8625
standard deviation: 1.79699
coeffecient of variation 0.465241
RUN SUCCESSFUL (total time: 111ms)


Topic archived. No new replies allowed.