Need help, (Calculate statistics - Reading from a text file)

0 Bawer Sultan · March 22, 2015
Ok, I have worked on this for approximately 5 hours, and I have not received the output I wanted, not even slose. 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. 

//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.

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.

Post a Reply


Oldest  Newest  Rating
0 Dol Lod · March 23, 2015
I1)  would like to add that you have undefined behaviour. sumValue is added to itself despite the fact that it is never initialized. At the start, initialize sumValue to 0. The same thing should be done with sumValueSquare. That should also be initialized to 0. 

2) Additionally like Krootushas says, numValue never changes. Initialize it to 0. Then add one to it every time in the while loop. 

3) Are you supposed to calculate the variance, standard deviation and mean and print them out every single time you add a number or just once at the end. I think it should just be once at the end. 

I did manage to create a variant of the code you put up and it mostly works. It's a bit off as var as variance goes, but everything else looks fine based on having 131 instead of 135. It just needs a few corrections here and there and it should be good. 

That's the good news anyway. 
  • 1



Used in many types of software including music players, video games, and many large scale applications.

Bucky Roberts Administrator