1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Need help with standard deviation C++ program

  1. Jul 6, 2011 #1
    1. The problem statement, all variables and given/known data

    Write a C++ program that accepts 10 floating-point values from a user and determines and displays the average and the standard deviation. All values more than four standard deviations away from the average should be dropped and then the new average and standard deviation should computed and displayed

    2. Relevant equations


    3. The attempt at a solution

    I got the average and the standard deviation to work just fine but the second part of the problem I am completely stuck on. Some one suggested to make another array to store the values in but I am definitely not doing it right. Any guidance on where to go from here would be greatly appreciated.

    This is my program so far:

    #include <iostream>
    #include <iomanip>
    #include <cmath>


    using namespace std;

    float stdDev(int arr[], int size, float avg);
    float calcAverage(int arr[], int size);
    float devDrop(int arr[], int adj[], int size, float avg, float dev);



    int main()
    {
    const int size = 10;
    int number[size];
    int adj[size];
    float avg, dev, dropAvg;

    for (int i=0; i<size; i++)
    {
    cout << "Enter Number #" << i+1 << ":";
    cin >> number;
    }

    avg = calcAverage(number, size);
    cout << avg;

    cout << endl;

    dev = stdDev(number, size, avg);
    cout << dev;

    cout << endl;

    dropAvg = devDrop(number, adj, size, avg, dev);
    cout << dropAvg;


    return 0;
    }


    float calcAverage(int arr[], int size)
    {
    float avg;
    int total = 0;
    for (int i=0; i<size; i++)
    total += arr;
    avg = float(total) / size;
    return avg;
    }

    float stdDev(int arr[], int size, float avg)
    {
    float dev;
    float total = 0;
    for (int i=0; i<size; i++)

    total += pow(avg - arr, 2);
    dev = sqrt(float(total) / size);

    return dev;
    }

    float devDrop(int arr[], int adj[], int size, float avg, float dev)
    {
    float dropAvg;

    float total = 0;

    for (int i=0; i<size; i++)
    {
    if (arr < (avg + dev * 4) || arr > (avg - dev * 4))
    total += adj;
    }
    dropAvg = total / size;
    return dropAvg;
    }
     
  2. jcsd
  3. Jul 6, 2011 #2
    Please put your code between CODE tags to help format it correctly. It's darn hard to read that way with all the indentation gone. Just put [ CODE] in front of your code, and [ /CODE] after it, remembering to remove the spaces after the square brackets.

    The first one is free, as they say.

    Code (Text):

    #include <iostream>
    #include <iomanip>
    #include <cmath>


    using namespace std;

    float stdDev(int arr[], int size, float avg);
    float calcAverage(int arr[], int size);
    float devDrop(int arr[], int adj[], int size, float avg, float dev);



    int main()
    {
     const int size = 10;
     int number[size];
     int adj[size];
     float avg, dev, dropAvg;
     
     for (int i=0; i<size; i++)
     {
         cout << "Enter Number #" << i+1 << ":";
         cin >> number[i];
     }

        avg = calcAverage(number, size);
        cout << avg;

        cout << endl;

        dev = stdDev(number, size, avg);
        cout << dev;

        cout << endl;

        dropAvg = devDrop(number, adj, size, avg, dev);
        cout << dropAvg;
       

     return 0;
    }


    float calcAverage(int arr[], int size)
    {
        float avg;
        int total = 0;
        for (int i=0; i<size; i++)
            total += arr[i];
        avg = float(total) / size;
        return avg;
    }

    float stdDev(int arr[], int size, float avg)
    {
        float dev;
        float total = 0;
        for (int i=0; i<size; i++)
       
            total += pow(avg - arr[i], 2);
            dev = sqrt(float(total) / size);
       
        return dev;
    }

    float devDrop(int arr[], int adj[], int size, float avg, float dev)
    {
        float dropAvg;

        float total = 0;
       
        for (int i=0; i<size; i++)
        {
            if (arr[i] < (avg + dev * 4) || arr[i] > (avg - dev * 4))
            total += adj[i];
        }
        dropAvg = total / size;
        return dropAvg;
    }
    I've only taken a very quick look, so there may be other mistakes. However, remember that after not counting some elements, you no longer have size elements added together. The new average is the new total divided by the number of elements you added to the sum. So count each entry you add to the sum, and divide by that number.

    Let us know if you get stuck anywhere else, of course.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Need help with standard deviation C++ program
  1. C program help needed (Replies: 3)

Loading...