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?

Dani AI

Generated

A few focused fixes and a safer way to compute standard deviation.

The posted program has three immediate bugs that affect the median and stdev outputs: stDev is printed without ever being computed, the selection-sort inner loop only scans part of the array (so the array never fully sorts), and the median print uses the temporary min value instead of the middle element of the sorted array. ’s point about readable, well-formatted code is useful here, and ’s two-pass variance idea is fine in principle but can suffer from floating-point cancellation. Using double and a numerically stable, single-pass algorithm avoids those pitfalls and removes the need for a separate variance pass.

A compact, robust approach (Welford’s one-pass algorithm) while still keeping the values for median/min/max:

#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>

int main() {
    const int N = 15;
    std::vector<double> numbers; numbers.reserve(N);

    double mean = 0.0, M2 = 0.0;
    int count = 0;
    for (int i = 0; i < N; ++i) {
        double x; std::cout << "Please enter your 15 numbers: ";
        if (!(std::cin >> x)) return 1;
        numbers.push_back(x);

        ++count;
        double delta = x - mean;
        mean += delta / count;
        M2 += delta * (x - mean);
    }

    double var_pop = M2 / count;
    double var_sample = (count > 1) ? (M2 / (count - 1)) : 0.0;
    double sd_pop = std::sqrt(var_pop), sd_sample = std::sqrt(var_sample);

    std::sort(numbers.begin(), numbers.end());
    double median = numbers[N/2]; // N odd

    std::cout << std::fixed << std::setprecision(6)
              << "Mean: " << mean << '\n'
              << "Median: " << median << '\n'
              << "Std dev (pop): " << sd_pop << '\n'
              << "Std dev (sample): " << sd_sample << '\n'
              << "Min: " << numbers.front() << "  Max: " << numbers.back() << '\n';
}

Notes and cautions: Welford’s updates (mean, M2) are numerically stable and avoid sqrt of a tiny negative due to cancellation. Choose population (N) or sample (N-1) denominator depending on whether the 15 values are the whole population or a sample. For median without fully sorting in larger datasets, std::nth_element gives O(N) selection. Replacing the handmade selection sort (or fixing its inner bound) and initializing variables resolves the incorrect median and printed garbage values seen in ’s output.

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.