# How to do standard deviation and find the median in code C++?

Page 1 of 1

## 3 Replies - 17399 Views - Last Post: 11 April 2012 - 10:49 AMRate 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=274548&amp;s=c68db5c44b1631b7033c244c6e11bacb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 hay123

Reputation: 0
• Posts: 13
• Joined: 13-March 12

# How to do standard deviation and find the median in code C++?

Posted 09 April 2012 - 05:30 PM

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;
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];
}

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

return 0;
}

```

I am also supposed to find and print out the median and standard deviation, but how would i do that? The user can enter any order and any kind of numbers, so how can i separate them to find a median with code? I also dont know how i would code the standard deviation... Any help would be greatly appreciated!

Is This A Good Question/Topic? 0

## Replies To: How to do standard deviation and find the median in code C++?

### #2 jimblumberg

Reputation: 5494
• Posts: 17,070
• Joined: 25-December 09

## Re: How to do standard deviation and find the median in code C++?

Posted 09 April 2012 - 06:12 PM

This link may help with Standard Deviation. And maybe this for the mean: Mean or median.

Jim

### #3 superkb10

• D.I.C Regular

Reputation: 29
• Posts: 298
• Joined: 27-November 11

## Re: How to do standard deviation and find the median in code C++?

Posted 09 April 2012 - 06:14 PM

So let's walk through what you do for finding Standard Deviation. First, you find the mean, which you've already done. Subtract the mean from each number, so you could create a second array identical to the first if you don't want tot affect the original. Then, subtract mean from each element in the array. The next step, is to square each number, so you should just multiply each element by itself. Then, you find the mean of all those numbers, which you have already done before, just do it again (try making a function that calculates mean). Last step is to take the square root of the final result, so just use the sqrt() function in the math.h header file.

As for the median, you could try devising a function to first put them all in order, by doing something like this: Use a for loop inside of another for loop, each going 15 times, then have a number that is the max number which will represent the element that is the largest, and in the inner for loop, check each element of the array against another to get a final result for the largest value. Repeat this process and put this all back in a separate array, sorry if my explanation is a bit bad, I'll give a code example. Then, divide the number of elements in the array in half, and take that element, or if odd, take the two around center, and find the mean. Here's what it would look like (sorry for no indenting):

```int number[15]
int orderednumbers[15];

for(int i = 0; i < 15; i++)
{
int maxelement = 0;
for(int j = 1; i < 15; i++)
{
if(numbers[j] > numbers[maxelement])
{
maxelement = j;
}
}
orderednumbers[i] = numbers[maxelement];
numbers[maxelement]  = -9999;
}

int median = (orderednumbers[7] + orderednumbers[8]) / 2;

```

Sorry again there's no indenting, and it might not be the best way to do it, but it is a way. Also, there might be a few mistakes, wrote this very quickly with no trying it out and I'm more used to using C# so sorry if I use some C# syntax in here. Anyway, lemme know if it works!

### #4 hay123

Reputation: 0
• Posts: 13
• Joined: 13-March 12

## Re: How to do standard deviation and find the median in code C++?

Posted 11 April 2012 - 10:49 AM

superkb10, on 09 April 2012 - 06:14 PM, said:

So let's walk through what you do for finding Standard Deviation. First, you find the mean, which you've already done. Subtract the mean from each number, so you could create a second array identical to the first if you don't want tot affect the original. Then, subtract mean from each element in the array. The next step, is to square each number, so you should just multiply each element by itself. Then, you find the mean of all those numbers, which you have already done before, just do it again (try making a function that calculates mean). Last step is to take the square root of the final result, so just use the sqrt() function in the math.h header file.

As for the median, you could try devising a function to first put them all in order, by doing something like this: Use a for loop inside of another for loop, each going 15 times, then have a number that is the max number which will represent the element that is the largest, and in the inner for loop, check each element of the array against another to get a final result for the largest value. Repeat this process and put this all back in a separate array, sorry if my explanation is a bit bad, I'll give a code example. Then, divide the number of elements in the array in half, and take that element, or if odd, take the two around center, and find the mean. Here's what it would look like (sorry for no indenting):

```int number[15]
int orderednumbers[15];

for(int i = 0; i < 15; i++)
{
int maxelement = 0;
for(int j = 1; i < 15; i++)
{
if(numbers[j] > numbers[maxelement])
{
maxelement = j;
}
}
orderednumbers[i] = numbers[maxelement];
numbers[maxelement]  = -9999;
}

int median = (orderednumbers[7] + orderednumbers[8]) / 2;

```

Sorry again there's no indenting, and it might not be the best way to do it, but it is a way. Also, there might be a few mistakes, wrote this very quickly with no trying it out and I'm more used to using C# so sorry if I use some C# syntax in here. Anyway, lemme know if it works!

You were very helpful! I have completed the median, my code is slightly different from yours, but I got a little lost in your explanation of how to do the standard deviation (LOL).
New code:
```#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;

}

for()

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;
}
```