Define a getStandardDeviation() function with the specification and prototype shown below:

// Calculate the population standard deviation of the scores
// That means...
// 1) For each score i, add pow(scores[i] - mean,2) to a running sum.
// 2) divide sum by the number of scores. That's the variance.
// 3) Return the square root of the variance.
// Assume that numScores > 0
double getStandardDeviation( double scores[], int numScores, double mean) ;

Here is the driver (main()) used to test my function.

#include <iostream>
 #include <iomanip>
 #include <cmath>
 using namespace std;

const int MAX_SCORES = 10; // Maximum number of scores

double getStandardDeviation( double scores[], int numScores, double mean) ;

int main() {
    double scores[MAX_SCORES];
    int scoreCount, skip;
    cin >> scoreCount;
    cin >> skip;
    scoreCount = min(scoreCount, MAX_SCORES);
    int i;
    for (i = 0; i < scoreCount; i++)
       scores[i] = i*skip;
    double mean = (i-1)*skip/2.0; // e.g. mean of 0,1,2,3,4,5 is (6-1)/2.0 = 2.5
    cout << fixed << setprecision(2);
    cout << getStandardDeviation(scores, scoreCount,mean) << endl;
    return 0;
 } 

Here is my solution:

double getStandardDeviation( double scores[], int numScores, double mean)
{
    int sum;
    int variance;
    for(int i = 0; i < numScores; i++)
    {
        sum += pow(scores[i] - mean,2);
    }
    variance = sum / numScores;
    return sqrt(variance);
}

Here is my output:

Input: 2 1
Output: 0.00

Here is the expected output:

Input: 2 1
Output: 0.50

Am I doing the calculation correctly? What lines of code should I change?

Dani AI

Generated

The wrong result comes from two bugs in the posted function: an uninitialized accumulator and use of integer types. 's implementation leaves sum uninitialized (undefined behavior) and uses int for values that must stay fractional. was right to suggest double, but the accumulator also needs initialization and integer division must be avoided.

A correct, simple implementation (population standard deviation, as specified) looks like this:

double getStandardDeviation(double scores[], int numScores, double mean) {
    double sum = 0.0;
    for (int i = 0; i < numScores; ++i) {
        double diff = scores[i] - mean;
        sum += diff * diff;         // faster and clearer than pow(...)
    }
    double variance = sum / numScores; // population variance (divide by N)
    return std::sqrt(variance);
}

Notes and cautions:

  • The function assumes numScores > 0 as specified. If defensive code is desired, return 0.0 or throw when numScores <= 0.
  • For a sample standard deviation (unbiased estimate), divide by numScores - 1 when numScores > 1.
  • For very large datasets or values that cause cancellation, prefer a numerically stable one-pass algorithm (Welford) to avoid loss of precision.

Am I doing the calculation correctly? What lines of code should I change?

Line 3 and 4:

 int sum;
 int variance;

change them to double.

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.