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

In summary: Thanks again for the help!In summary, the conversation involved a student seeking help with a programming assignment to find the standard deviation of an array of numbers. They shared their code and discussed possible errors in their program, eventually realizing that they needed to change the variable name from "std" to "ans" to get the correct output. The expert provided guidance and helped the student successfully complete their assignment.
  • #1
Smiles302
27
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
  • #2
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:
 
  • #3
:smile:

*facepalm*

back to drawing board
 
  • #4
Hint: I don't see a plus sign anywhere in your program.
 
  • #5
Found the solution asa I found the error (Uh, we don't have a smiley for glee?)
 
  • #6
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
 
  • #7
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);
}
 
  • #8
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:
 
  • #9
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
 

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

1. What is a loop in C++?

A loop in C++ is a programming construct that allows you to repeat a set of instructions multiple times. It is used to perform repetitive tasks efficiently and is an essential part of all programming languages.

2. Why can't I figure out what my loop is doing in C++?

There could be several reasons why you can't figure out what your loop is doing in C++. Some common reasons include incorrect syntax, logic errors, or not understanding the flow of the loop. It is important to carefully review your code and debug any issues to understand the behavior of your loop.

3. How do I debug my loop in C++?

To debug your loop in C++, you can use a debugger tool or add print statements at various points in your code to track the values of variables and understand the flow of the loop. Another helpful method is to use breakpoints to pause the execution of the program at specific points and inspect the values of variables.

4. What are the different types of loops in C++?

C++ offers three types of loops: for loop, while loop, and do-while loop. A for loop is used to iterate a specific number of times, while a while loop is used to repeat until a certain condition is met. A do-while loop is similar to a while loop but guarantees to execute the loop at least once.

5. How do I optimize my loop in C++?

To optimize your loop in C++, you can use techniques like loop unrolling, loop fusion, and loop tiling to reduce the number of iterations and improve performance. You can also try to avoid unnecessary operations within the loop and use the most efficient loop for your specific task.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Introductory Physics Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
23
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
Back
Top