Problem With Calculations

I'm trying to write a program that calculates the mean and standard deviation for a set of numbers. I broke the calculation down into a couple parts, and for some reason when I try to sum the differences squared I keep getting the wrong answer. Not sure why though. For example if I enter 1,2,3,4,5 the sum of the differences squared should be 10 but I get 80.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// INCLUDES
#include<iostream>
#include<iomanip>
#include<cmath>
	using namespace std;

// PROTOTYPES
double meanStandDev(double, int); // sum & userArraySize
double standardDeviation(double sumTwo, int userArraySize); // difference squared, userArray, radicand

// MAIN
int main(void) {

	// declare variables
	double mean = 0.0; 
	double standDev = 0.0;
	int i;
	double sum = 0.0;
	double sumTwo = 0.0;
	double radicand = 0.0;
	double differenceSquared = 0.0;

	// ARRAY, create variable for the array
	int arraySize = 50; 
	
	// declare an array and fill it with 0.0
	double zeroArray[50] = {0.0}; 

	// PROMPT user to fill array
	int userArraySize = 0;
	double userArray[50]; // maximum size of 50

	do {
		cout << "Enter a value for the array (negative value to quit): ";
		cin >> userArray[userArraySize];

		// increment our counter
		userArraySize++;
	} while (userArray[userArraySize -1] >= 0.0);

	// fix to make to eliminate the negative value
	userArraySize--;

	// print new array
	for (int i=0; i < userArraySize; i++) {

		cout << userArray[i] << endl;
	}

	// sum of all elements in the array
	for (i=0; i<userArraySize; i++)
	{
		sum = sum + userArray[i];
	}

	// sum of differences squared
	for (i=0; i<sum; i++) {

		differenceSquared = pow(userArray[i] - mean, 2);
		sumTwo = sumTwo + differenceSquared;
	}
	

	// call function to calculate mean
	mean = meanStandDev(sum, userArraySize);
	// call function to calculate standard deviation
	standDev = standardDeviation(sumTwo, userArraySize);

	// output
	cout << "The mean is " << mean << "." << endl;
	cout << "Array total is " << sum << endl;
	cout << "Array size is " << userArraySize << endl;

	cout << "The sum of the differences squared is " << sumTwo << endl;
	cout << "The radicand is " << radicand << endl;
	cout << "The standard deviation is " << standDev << endl;

	system("pause");
	return 0;
} // end of main

// function definitions for the mean
double meanStandDev(double sum, int userArraySize) {

	// declare a return value for the mean
	double mean = 0.0;
	//calculate the mean
	mean = (sum / userArraySize);
	// delcare a return value for the mean
	return mean;
}

// function definitions for standard deviation
double standardDeviation(double sumTwo, int userArraySize) {
	
	double radicand = 0.0;
	double standDev = 0.0;
	
	// divide by n-1
	radicand = sumTwo / (userArraySize - 1);
cout << radicand << endl;
	// square root
	standDev = sqrt(radicand);

	//declare a return value
	return standDev;
}
Compare your working with this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
double stdev(double a[], int n)
{ assert(n > 1);
  double sum=0;//initialize sum to zero
  for (int i=0; i<n; i++)
    sum += a[i];
  double mean = sum/n;  
  sum=0;
  double deviation;
  for (int i=0; i<n; i++)
  { deviation = a[i] - mean;
    sum += deviation*deviation;
  }
  return sqrt(sum/(n-1));  
}
Its because you are haven't calculated your mean. You are just using 0.0 as the mean.
calculate the mean just before calculating the square of differences.
Enjoy!
Topic archived. No new replies allowed.