Comp Sci Why Is My C++ Loop Not Calculating Standard Deviation Correctly?

  • Thread starter Thread starter Smiles302
  • Start date Start date
  • Tags Tags
    C++ Figure Loop
AI Thread Summary
The discussion focuses on troubleshooting a C++ program intended to calculate the standard deviation of an array of numbers. The user encounters an issue where the program consistently returns an incorrect value of 1.44 instead of the expected 111.6. Key points include the realization that the variable used to store results was incorrectly named and that the loop did not accumulate the squared differences properly. After correcting the variable names and ensuring the summation of squared differences was implemented correctly, the user successfully resolved the issue. The conversation highlights the importance of careful variable management and proper accumulation in programming.
Smiles302
Messages
26
Reaction score
0

Homework Statement



I need to write a program (which passes by reference) to find the standard deviation of an array of numbers.

Homework Equations



http://en.wikipedia.org/wiki/Standard_deviation < the simple standard deviation formula is the one I am using.

I've tested the program and the only bit not working is this (mean - x)^2

The Attempt at a Solution




for(int i = 0; i < num; i++)
{
std = (mean - xArray);

if (std < 0)
{
std = std*(-1);
}

std = (std*std);
}

Should this loop give me this equation:

The array is {12, 4, 5, 3, 4, 0, 1, 8, 2, 3};

And my mean is 4.2.

(12 - 4.2)^2 + (4 - 4.2)^2 + (5 - 4.2)^2 + (3 - 4.2)^2 + ( 4 - 4.2)^2 + (0 - 4.2)^2 + (1 - 4.2)^2 + ( 8 - 4.2)^2 + (2 - 4.2)^2 + (3 - 4.2)^2.

This should give me 111.6 but it keeps giving me the answer 1.44

Can anyone tell me what my program is going to get 1.44 =/
 
Physics news on Phys.org
Smiles302 said:

Homework Statement



I need to write a program (which passes by reference) to find the standard deviation of an array of numbers.

Homework Equations



http://en.wikipedia.org/wiki/Standard_deviation < the simple standard deviation formula is the one I am using.

I've tested the program and the only bit not working is this (mean - x)^2

The Attempt at a Solution




for(int i = 0; i < num; i++)
{
std = (mean - xArray);


Here you assign std = (mean - xArray), replacing any previous value it had.
if (std < 0)
{
std = std*(-1);
}

std = (std*std);
Here you square it.

}

Should this loop give me this equation:

The array is {12, 4, 5, 3, 4, 0, 1, 8, 2, 3};

And my mean is 4.2.

(12 - 4.2)^2 + (4 - 4.2)^2 + (5 - 4.2)^2 + (3 - 4.2)^2 + ( 4 - 4.2)^2 + (0 - 4.2)^2 + (1 - 4.2)^2 + ( 8 - 4.2)^2 + (2 - 4.2)^2 + (3 - 4.2)^2.

This should give me 111.6 but it keeps giving me the answer 1.44

Can anyone tell me what my program is going to get 1.44 =/

Therefore, std keeps changing and in the end, first you assign (3 - 4.2) to it, then square that, which is 1.44 :smile:
 
:smile:

*facepalm*

back to drawing board
 
Hint: I don't see a plus sign anywhere in your program.
 
Found the solution asa I found the error (Uh, we don't have a smiley for glee?)
 
Adding a " ans = ans + std; " doesn't solve the program ?

It's my first time using an Array. It doesn't automatically run through all the i's?

Example:

for(int i = 0; i < num; i++)
{
sum = sum + xArray;
}
Gives me the full sum as it adds up the full array...

Shouldn't adding an ans = ans + std give me

ans = 0 + first std
ans = first std + second std.
etc
 
Yes it will. Although your sum (is it same as std?) doesn't make a lot of sense to me.
One of the ways to do it would be to calculate std for each term {aka std = (mean - xArray)*(mean - xArray)} and keeping adding these (ans = ans + std) .

Also note that you don't need the if sequence you originally had.
if (std < 0)
{
std = std*(-1);
}
 
for (int i = 0; i < num; i++)
{
std = (mean - xArray)*(mean - xArray);
ans = ans + std;
}

Should work and give me 111.6?

It is still giving me an answer of 1.44. I am so confused. :smile:
 
Okay, book-keeping first. Do you initialize ans = 0?
Second, are you displaying ans or std?
 
  • #10
Ah I was displaying std not ans.

But now it's giving me zero... =/
 
  • #11
Core code looks fine to me. Maybe there are issues in the declaration, etc. (the boring part) Can you post the whole code here?
 
  • #12
I got it

*dances*

=D

I changed all the "std"'s to "ans" and it worked.

Thank you!
 
  • #13
Smiles302 said:
I got it

*dances*

=D

I changed all the "std"'s to "ans" and it worked.

Thank you!

You mean you changed

for (int i = 0; i < num; i++)
{
std = (mean - xArray)*(mean - xArray);
ans = ans + std;
}

to

for (int i = 0; i < num; i++)
{
ans = (mean - xArray)*(mean - xArray);
ans = ans + ans;
}

?

I'm surprised it works, then. You're welcome :D
 
  • #14
Oh no haha!

from this

void standiv (double xArray[], double& num, double& sum, double& std, double& mean)

to

void standiv (double xArray[], double& num, double& sum, double& ans, double& mean)

and in a couple more places in the main.

I'll get a hand of this coding lack eventually haha
 

Similar threads

Replies
7
Views
2K
Replies
5
Views
2K
Replies
10
Views
3K
Replies
5
Views
7K
Replies
23
Views
3K
Replies
3
Views
1K
Replies
6
Views
4K
Back
Top