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
Click For Summary

Discussion Overview

The discussion revolves around a programming issue related to calculating the standard deviation of an array of numbers in C++. Participants are troubleshooting a loop that is not producing the expected result when calculating the standard deviation based on a provided formula.

Discussion Character

  • Homework-related
  • Technical explanation
  • Exploratory

Main Points Raised

  • One participant describes their attempt to calculate the standard deviation using the formula and notes that their program is returning an incorrect value of 1.44 instead of the expected 111.6.
  • Another participant points out that the variable 'std' is being reassigned in each iteration of the loop, which may be causing the issue.
  • There is a suggestion that the absence of a summation operation (a plus sign) in the loop could be the reason for the incorrect output.
  • One participant expresses confusion about how to accumulate the results correctly, questioning whether the loop should automatically iterate through all elements of the array.
  • Another participant confirms that the accumulation of 'std' values should work as intended if implemented correctly.
  • There is a discussion about the importance of initializing variables and ensuring the correct variable is displayed in the output.
  • Ultimately, a participant reports that changing variable names from 'std' to 'ans' resolved their issue, indicating a misunderstanding in variable usage.

Areas of Agreement / Disagreement

Participants generally agree on the need for proper variable initialization and accumulation in the loop. However, there is no consensus on the best approach to implement the solution, as various suggestions are made without a definitive resolution.

Contextual Notes

Some participants mention potential issues with variable declarations and the need for clarity in variable naming, which may have contributed to the confusion in the calculations.

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 ·
Replies
7
Views
2K
  • · Replies 24 ·
Replies
24
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K