I'm trying to write a program that calculates the mean and standard deviation for a set of numbers. I broke the calculation down into a couple parts, and for some reason when I try to sum the differences squared I keep getting the wrong answer. Not sure why though. For example if I enter 1,2,3,4,5 the sum of the differences squared should be 10 but I get 80.

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

// PROTOTYPES
double meanStandDev(double, int); // sum & userArraySize
double standardDeviation(double sumTwo, int userArraySize); // sumTwo, userArray

// MAIN
int main(void) {

    // declare variables
    double mean = 0.0; 
    double standDev = 0.0;
    int i;
    double sum = 0.0;
    double sumTwo = 0.0;
    double radicand = 0.0;
    double differenceSquared = 0.0;

    // ARRAY, create variable for the array
    int arraySize = 50; 

    // declare an array and fill it with 0.0
    double zeroArray[50] = {0.0}; 

    // PROMPT user to fill array
    int userArraySize = 0;
    double userArray[50]; // maximum size of 50

    do {
        cout << "Enter a value for the array (negative value to quit): ";
        cin >> userArray[userArraySize];

        // increment our counter
        userArraySize++;
    } while (userArray[userArraySize -1] >= 0.0);

    // fix to make to eliminate the negative value
    userArraySize--;

    // print new array
    for (int i=0; i < userArraySize; i++) {

        cout << userArray[i] << endl;
    }

    // sum of all elements in the array
    for (i=0; i<userArraySize; i++)
    {
        sum = sum + userArray[i];
    }

    // sum of differences squared
    for (i=0; i<sum; i++) {

        differenceSquared = pow(userArray[i] - mean, 2);
        sumTwo = sumTwo + differenceSquared;
    }


    // call function to calculate mean
    mean = meanStandDev(sum, userArraySize);
    // call function to calculate standard deviation
    standDev = standardDeviation(sumTwo, userArraySize);

    // output
    cout << "The mean is " << mean << "." << endl;
    cout << "Array total is " << sum << endl;
    cout << "Array size is " << userArraySize << endl;

    cout << "The sum of the differences squared is " << sumTwo << endl;
    cout << "The radicand is " << radicand << endl;
    cout << "The standard deviation is " << standDev << endl;

    system("pause");
    return 0;
} // end of main

// function definitions for the mean
double meanStandDev(double sum, int userArraySize) {

    // declare a return value for the mean
    double mean = 0.0;
    //calculate the mean
    mean = (sum / userArraySize);
    // delcare a return value for the mean
    return mean;
}

// function definitions for standard deviation
double standardDeviation(double sumTwo, int userArraySize) {

    double radicand = 0.0;
    double standDev = 0.0;

    // divide by n-1
    radicand = sumTwo / (userArraySize - 1);
cout << radicand << endl;
    // square root
    standDev = sqrt(radicand);

    //declare a return value
    return standDev;
}

I haven't tested, but check where I've commented.

for (i=0; i<sum; i++) {
differenceSquared = pow(userArray[i] - mean, 2); // at this point mean is still 0.0
sumTwo = sumTwo + differenceSquared;
}
// call function to calculate mean
mean = meanStandDev(sum, userArraySize);         // mean calculated after it's needed
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.