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

  • Context: Comp Sci 
  • Thread starter Thread starter Smiles302
  • Start date Start date
  • Tags Tags
    C++ Figure Loop
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
13 replies · 2K views
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?
 
Ah I was displaying std not ans.

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

*dances*

=D

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

Thank you!
 
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
 
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