I need help, (Calculating statistics, Reading from a Text File)

Ok, I have worked on this for approximately 5 hours, and I have not received the output I wanted, not even close. The program is pretty simple, its not long at all, here are the specs.

She wants us to use a While loop only the by the way.

//SPEC SHEET:
//1. Declare a variable to hold a counter (that will count the number of
// values read from the file)

//2. (NO stop value is needed. The loop stops when all values have been read
// from the file)

//3. Declare one variable to be used (And re-used) for the reading of each
// value from the file.

//4. Declare variables for the variance, the mean, and the standard deviation

//5. Declare variable for two total sums: sum of values and sum of each value
// squared

//6. Read the values from the file one at a time, and update each of the 2
// totals

//7. use both totals to calculate the variance

//8. Use the first total (sum of values) to calculate the mean (mean = total
// / number of values)

//9. Display the variance, standard deviation and mean

I am having trouble, specifically with number 1, 6, and 7. I am positive that I have a mistake in at least one of those. Number 6, is one that I dont even know how to do.

Here is the first link to the program, which also contains the specs at the bottom.

http://ideone.com/369X6O

Here is the link to the file which I am reading from, and it also displays what the specific output of the program should look like at the bottom. (My professor provides us with these to help us.) A few numbers may also be in different colors, but pay no mind to it.

http://ideone.com/kZ6g77

Last edited on
1. numValue should be initialized to 0, not 1. The first line of your while loop should be ++numValue;.

6. sumvalueSquare += pow (sumValue, 2.0);

7. I have no idea how to calculate the variance. If it's the same formula as here:

http://en.wikipedia.org/wiki/Variance#Population_variance

then u (aka the mean) is sumValue / numValue. Obviously x is num. You should easily be able to find (x - u)2. So I suppose variancenumValue = (variancenumValue-1 * (numValue - 1) + (num - mean)2) / numValue.
That makes more sense. Another problem, however, which i should have mentioned because it is so important is that my program does that for each number.

For example instead of finding the variance, mean, stand deviation for the total of the 35 it does it for each value:

This is what I got:

99.5
The variance is :-1.#INF
The standard deviation is:-1.#IND
The mean is:1
97.2
The variance is :0.5
The standard deviation is:0.707107
The mean is:0.5

and then it does the same for the other numbers

I should have just recieved only numbers 99.5 and then 97.2, and then at the bottom I should have got the variance, standard deviaiton, and mean for all of the 35 numbers put together.





Last edited on
That makes more sense. Another problem, however, which i should have mentioned because it is so important is that my program does that for each number.


Do you mean that your program is supposed to do that for each number but doesn't, or that your program is not supposed to do that but does?

Also, no idea how you got variance = -1. You must be using a different formula. The formula I gave you would be

variance1 = (variance0 * (1 - 1) + (num - mean)2) / 1

And since there's only 1 number so far, mean == num so that all boils down to

variance1 = (0 * (0) + (0)2) / 1 = 0
Topic archived. No new replies allowed.