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++)
{
cout << "Please enter your 15 numbers: ";
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?

Recommended Answers

All 2 Replies

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

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
Please enter your 15 numbers: 1
Please enter your 15 numbers: 2
Please enter your 15 numbers: 3
Please enter your 15 numbers: 4
Please enter your 15 numbers: 5
Please enter your 15 numbers: 6
Please enter your 15 numbers: 7
Please enter your 15 numbers: 8
Please enter your 15 numbers: 9
Please enter your 15 numbers: 10
Please enter your 15 numbers: 11
Please enter your 15 numbers: 12
Please enter your 15 numbers: 13
Please enter your 15 numbers: 14
Please enter your 15 numbers: 15
Your mean is: 8
Your range is: 14
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)
Your maximum is: 15
Your minimum is: 1
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.