# Need help with standard deviation

Page 1 of 1

## 7 Replies - 1839 Views - Last Post: 03 May 2007 - 07:39 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=27546&amp;s=6c59f23bed3f9142c941af636e66b91d&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 00Joe

Reputation: 0
• Posts: 3
• Joined: 01-May 07

# Need help with standard deviation

Posted 02 May 2007 - 09:42 AM

I wrote a smaller c++ program to test out the math for my standard deviation function for a much larger program.

Basically, you input a bunch of numbers, and end it with a negative number. It should output the standard deviation of that set of numbers. The problem is, that I keep getting I.#INF . What is that, and how do I fix my code to get the math right?

#include <iostream>
#include <iomanip>
#include <math.h>
using namespace std;
void main ()
{
int pos, cnt;
float avg, data[50], tot, num;

cout.setf(ios::fixed);
cout.precision(1);

pos=0;
cnt=0;
cout << "Num? ";
cin >> num;
while (num >=0)
{
pos++;
cnt++;
data[pos]=num;
cout << "Num? ";
cin >> num;
}

tot=0;
for (pos=0; pos<cnt-1; pos++)
tot+=(data[pos+1]-data[pos])*(data[pos+1]-data[pos]);

avg=tot/cnt;
cout << "The standard deviation is " << sqrt(avg) << endl;

once I get this right, I'll integrate this code into my larger program.

Is This A Good Question/Topic? 0

## Replies To: Need help with standard deviation

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: Need help with standard deviation

Posted 02 May 2007 - 09:53 AM

What platform/compiler are you using...that code runs fine for me, in that it outputs a number...I've not checked to see if it's actually the standard dev or not.

### #3 00Joe

Reputation: 0
• Posts: 3
• Joined: 01-May 07

## Re: Need help with standard deviation

Posted 02 May 2007 - 09:55 AM

Amadeus, on 2 May, 2007 - 09:53 AM, said:

What platform/compiler are you using...that code runs fine for me, in that it outputs a number...I've not checked to see if it's actually the standard dev or not.

I'm running this on Windows xp, if that's what you were asking... I also typed it up in visual c++ 2005 express edition

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: Need help with standard deviation

Posted 02 May 2007 - 10:18 AM

Hmm...well, it really runs for me...can you give an example of the input you enter, then the exact line displayed in the output?

### #5 00Joe

Reputation: 0
• Posts: 3
• Joined: 01-May 07

## Re: Need help with standard deviation

Posted 02 May 2007 - 10:25 AM

Amadeus, on 2 May, 2007 - 10:18 AM, said:

Hmm...well, it really runs for me...can you give an example of the input you enter, then the exact line displayed in the output?

sure:

I punch in 95, 80, 82.5, 95, 70, 75, 73, 89, 66, 62, 78, 51, -1
I should get 13.2. Instead, I get 100000(20 lines later)0000.\$
I think that I got the old answer before I changed something, but that's still the same code for this answer.

• g+ + -o drink whiskey.cpp

Reputation: 250
• Posts: 13,507
• Joined: 12-July 02

## Re: Need help with standard deviation

Posted 02 May 2007 - 10:29 AM

Hmm....I got 30.4, but got an answer.

### #7 NickDMax

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: Need help with standard deviation

Posted 03 May 2007 - 04:09 PM

My first observation is that this is not the std deviation.

would be the ussual formula. To calculate this you would first find the average -- this is generally the first step in any version of the standard deviation.

So step 1 find the average.
Step 2 find the average of the square of distance from the average.
Step 3 Take the sqrt of the "average of the square of the distance from the average"

Having said that why does Amadeus get a small answer and you get an ugly one... Well my bet would be your compiler or platform. There is a logic error in your program that gets you to use some uninitialized data. This confused me at first because what I calculated by hand and what I was looking at were not the same... translation -- either C++ has forgotten how to preform arithmatic, I had forgotten how to read C++, or we were not using the same numbers.

When you collect the data you don't use data[0] becuase you places your pos++ before you assigned the data. But later you use pos=0 as the begining of your for loop. Change that loop to start with pos=1 and your program will calculate the "standard (unsigned) differential" -- not the standard deviation.

### #8 GWatt

Reputation: 307
• Posts: 3,105
• Joined: 01-December 05

## Re: Need help with standard deviation

Posted 03 May 2007 - 07:39 PM

This might not be relevent, but do you want the sample standard deviation, or the population standard deviation?
Population: Ask every single eligible person in the USA who they will vote for in the 08 election
Sample: ask a few thousand people who they will vote for instead.