Math not computing correctly

My problem is that my summation function is not working correctly.

when I subtract the Integer value minus the mean I do not get the correct answer.

I am trying to get (x-mean)^2

The answer I get on the calculator is -6.5 while my program computes -6.25.
The other values in the function are all a bit off as well

I'm sure I am making some dumb mistake.

Thanks in advance for the help!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
//Includes ***************************************************

#include<iostream> // for cin and cout
#include<cmath>    // for square root function sqrt()
using namespace std; // for cin and cout in <iostream>


//Constants **************************************************
float Val1 = 16;							    //Given Integer Value 1
float Val2 = 21;							//Given Integer Value 2
float Val3 = 23;							//Given Integer Value 3
float Val4 = 29;							//Given Integer Value 4
double IntValMean = (Val1 + Val2 + Val3 +Val4)/4; //Compute Mean Value of the Integers
double RunTotal = 0.0;					// Running Total for Summations
double Deviation = 0.0;                   // Calculated Standard Deviation
//Functions***************************************************

double Summation()
{
  //Calculate the Summation for each individual integer
	RunTotal = (Val1-IntValMean)*(Val1-IntValMean);
	RunTotal = RunTotal + ((Val2-IntValMean)*(Val2-IntValMean));
	cout << ((Val1-IntValMean)*(Val1-IntValMean)) << endl;
	cout << ((Val2-IntValMean)*(Val2-IntValMean)) << endl;
	cout << ((Val3-IntValMean)*(Val3-IntValMean)) << endl;
	cout << ((Val4-IntValMean)*(Val4-IntValMean)) << endl;
	RunTotal = RunTotal + ((Val3-IntValMean)*(Val3-IntValMean));
	RunTotal = RunTotal + ((Val4-IntValMean)*(Val4-IntValMean));
	return RunTotal;
}
oh and I have tried declaring the values like Val1 with a decimal like 16.0 and it does not make a difference

I have also tried switching to all doubles and to all floats with no difference
Is this the entire code?
no....do you want all of it?
yes, otherwise we can't figure it out as well.
Last edited on
//Includes ***************************************************

#include<iostream> // for cin and cout
#include<cmath> // for square root function sqrt()
using namespace std; // for cin and cout in <iostream>


//Constants **************************************************
float Val1 = 16; //Given Integer Value 1
float Val2 = 21; //Given Integer Value 2
float Val3 = 23; //Given Integer Value 3
float Val4 = 29; //Given Integer Value 4
double IntValMean = (Val1 + Val2 + Val3 +Val4)/4; //Compute Mean Value of the Integers
double RunTotal = 0.0; // Running Total for Summations
double Deviation = 0.0; // Calculated Standard Deviation
//Functions***************************************************

double Summation()
{
//Calculate the Summation for each individual integer
RunTotal = (Val1-IntValMean)*(Val1-IntValMean);
RunTotal = RunTotal + ((Val2-IntValMean)*(Val2-IntValMean));
cout << ((Val1-IntValMean)*(Val1-IntValMean)) << endl;
cout << ((Val2-IntValMean)*(Val2-IntValMean)) << endl;
cout << ((Val3-IntValMean)*(Val3-IntValMean)) << endl;
cout << ((Val4-IntValMean)*(Val4-IntValMean)) << endl;
RunTotal = RunTotal + ((Val3-IntValMean)*(Val3-IntValMean));
RunTotal = RunTotal + ((Val4-IntValMean)*(Val4-IntValMean));
return RunTotal;
}
double StdDeviation()
{
double PreSqrd = RunTotal/(4-1); //Summation/(n-1) as part of formula
double EvalDev = sqrt(PreSqrd); //Take the Square Root to finish std dev equation
Deviation = EvalDev;
return Deviation; //Evaluated Standard Devitation Equation

}

void ResulPrint(double Dev, double Mean) {
cout << "The Standard Deviation of the Integers " << Val1 << "," << Val2 << ",";
cout << Val3 << ", and " << Val4 << " is:" << endl << endl;
cout << Dev << endl <<endl;
cout << "and their mean value is:" << endl <<endl;
cout << IntValMean << endl;
}

// Main Function**********************************************
int main() {
// Call the Standard Deviation Function
Summation();
StdDeviation();

//Print Result + Mean Value to Screen
ResulPrint(Deviation,IntValMean);

//Program Wait for Termination
char exit_char; // Temporary for input
cout << "\nPress any key and <enter> to exit \n";
cin >> exit_char; //Wait for key response before exiting.
return 0;
}

[/code]
Which of these is wrong? I don't get it, what -6.25?
39.0625
1.5625
0.5625
45.5625
The Standard Deviation of the Integers 16,21,23, and 29 is:

5.37742

and their mean value is:

22.25

Press any key and <enter> to exit 

Topic archived. No new replies allowed.