We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,534 Members — Technology Publication meets Social Media

# standard deviation to code?

This is my code so far:

``````#include <iostream>
#include <cmath>
#include <cstdlib>
using namespace std;

int main()
{
float numbers[15];
float sum, lowest, highest;
float min, max, range, stDev, mean, median;
int indexOfMin;
float above = 0, below = 0;

sum = 0;
for(int testNum = 0; testNum < 15; testNum++)
{
cin >> numbers[testNum];
sum = sum + numbers[testNum];
}
mean = sum/15;
for(int testNum = 0; testNum < 15; testNum++)
{
if(numbers[testNum] < mean)
{
below++;
}
else if(numbers[testNum] > mean)
{
above++;
}
}
lowest = numbers[0];

for(int testNum = 0; testNum < 15; testNum++)
{
if(numbers[testNum] < lowest)
lowest = numbers[testNum];
}

highest = numbers[0];
for(int testNum = 0; testNum < 15; testNum++)
{
if(numbers[testNum] > highest)
highest = numbers[testNum];
}

for(int outer = 0; outer < 14; outer++)
{
min = numbers[ outer]; //set the proposed min to the first element in the first part of the array that is not yet sorted
indexOfMin = outer; //save the index number of this min value
for(int inner = outer+1; inner < 7; inner++) //now loop thru the rest of the array to find the smallest element
{
if(numbers[inner] < min ) //find the new min out of the rest of the elements besides the starting element
{
min = numbers[inner];
indexOfMin = inner;
}
}
numbers[indexOfMin] = numbers[outer]; //swap the new min value with the first element in this iteration
numbers[outer] = min;

}

cout << "Your mean is: " << sum/15 << endl;
cout << "Your range is: " << highest-lowest << endl;
cout << "Your median is: " << min << endl;
cout << "Your standard deviation is: " << stDev << endl;
cout << "Your maximum is: " << highest << endl;
cout << "Your minimum is: " << lowest << endl;

return 0;
}
``````

Im unsure of how to code in the standard deviation of the input (the 15 values the user will input). Could someone help show me or explain to me how I would add that into my code?

3
Contributors
2
Replies
1 Hour
Discussion Span
1 Year Ago
Last Updated
4
Views
hay123
Newbie Poster
3 posts since Apr 2012
Reputation Points: 0
Skill Endorsements: 0

It is difficult to read this. Consulting the site's guidelines on how to post source code. Here is an example:

``````    // This is a comment
// Here is some code
for (int i = 0; i < 1024; i++)
{
if ((i % 100) == 0)
{
cout << (dec) << i << endl;
}
}
``````
rubberman
Posting Maven
2,676 posts since Mar 2010
Reputation Points: 378
Skill Endorsements: 53

http://en.wikipedia.org/wiki/Standard_deviation

The standard dev is the square root of 1/N*sum (x-mean)^2.

``````float stDevSum(0);
for(int testNum = 0; testNum < 15; testNum++) {
stDevSum += powf((numbers[testNum]-mean),2.0f);
}
stDev = sqrt(stDevSum/15);
``````

You might also see it done as sqrt(E[X^2]-E[X]^2) but if the standard dev is really small you might get NAN (Not A Number) because of floating point calculation errors could result in a negative number which sqrt(-x) = NAN. Don't do this unless you have too for some odd reason.

## Also, you median calculation is not giving the correct answer. Consider the input below.

``````\$ ./a.out
Your median is: 14   <------------- The answer should be 7, if you are trying to use
Your standard deviation is: 0       element 7 in the array of 15 (floor(15/2) = 7)
``````
histrungalot
Posting Whiz in Training
280 posts since May 2008
Reputation Points: 76