Is there any way to make this code simpler??

This exercise displays the average of n numbers and the standard deviation of those numbers. To be frank, this program was a brain buster and took me forever but I finally got it so my question is...is there any way I could have written this program a little more condensed than what it currently is? I have rewritten this many times and this is the smallest I could get it. The program is not too long and I appreciate any help. Thanks.

Also, for anyone who does not know about standard deviation: It assesses n numbers and outputs a number based on far the user went from the average.
eg: The standard dev would be 0 for numbers 5, 5, and 5. The standard dev would be a little higher for numbers 5, 10, and 0....but the average would remain the same.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//Getting the average and the standard deviation of n numbers
//Standard Deviation is pow((x1 - average), 2) <!--x1 stands for one of the numbers-->
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int numInput;
    int OriginalAmountOfNumbers, amountOfNumbers;
    int num = 0, devNum = 0, counter = 1, devNumMiddleMan = 0;
    double average, devNumAverageTimesTwo, standardDeviation;


    cout << "This program will determine the average and standard deviation of n numbers\n\n!";
    cout << "How many numbers will you be entering? ";
    cin >> OriginalAmountOfNumbers; // Need this to maintain count of numbers
    cout << "Enter " << OriginalAmountOfNumbers << " numbers: ";

    amountOfNumbers = OriginalAmountOfNumbers;

    while (amountOfNumbers != 0) // Average loop
    {
        cin >> numInput;
        while (counter > 0) // Standard Deviation loop
        {
            devNumMiddleMan += numInput; // Used middle man beacause it is only way I can add powers
            devNum += pow(devNumMiddleMan, 2);
            devNumMiddleMan = 0;
            counter--;
        }
        num += numInput;
        amountOfNumbers--;
        counter++;
    }
    //devNum is every number, squared, and added together
    //num is every number added together

    average = num / OriginalAmountOfNumbers;
    devNumAverageTimesTwo = pow(average, 2) * OriginalAmountOfNumbers;
    standardDeviation = sqrt((devNum - devNumAverageTimesTwo) / OriginalAmountOfNumbers);

    cout << "\nThe average of the " << OriginalAmountOfNumbers << " numbers is " << average << endl;
    cout << "\nThe standard deviation is " << standardDeviation << endl;
    return 0;
}

//Comment Section 

I used this code for population std dev. calculation
For Sample divide by Amount-1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream.h>
#include <math.h>

int main()
{
    int Amount,x,number;
    long double u=0,std_dev=0;
    cout << "This program will determine the average and standard deviation of n numbers\n\n!";
    cout << "How many numbers will you be entering? ";
    cin >> Amount;
    cout << "Enter " << Amount << " numbers: ";

    for (x=Amount;x != 0;x--)
    {
	cin >> number;
	u+=number;
	std_dev+=pow(number,2);
    }

    u = u / Amount;
    std_dev=sqrt((std_dev/Amount)-pow(u,2));

    cout << "\nThe average of the " << Amount << " numbers is " << u << endl;
    cout << "\nThe standard deviation is " << std_dev;
    return 0;
}
Last edited on
closed account (D80DSL3A)
I have it down to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
    int N = 0;// size of the sample
    double x = 0.0, sum = 0.0, sumSq = 0.0;

    cout << "How many numbers in the sample? ";
    cin >> N;
    cout << endl << "Enter " << N << " numbers." << endl;

    for(int i=1; i<=N; ++i)
    {
        cin >> x;
        sum += x;
        sumSq += x*x;
    }

    cout << "the average of the " << N << " numbers = " << sum/N << endl;
    cout << "stdDev = " << sqrt( (sumSq - sum*sum/N)/N ) << endl;

    cout << endl;
    return 0;
}

Unfortunately, our programs give different results.
For this sample of 7 numbers: 2, 8, 15, 6, 25, 4, 20
Your program gives: avg = 11, stdDev = 8.64374
Mine gives: avg = 11.4286, stdDev = 8.06858

You're getting the wrong value for average because both num and OriginalAmountOfNumbers are integers. When you calculate average on line 39:
average = num / OriginalAmountOfNumbers; this performs integer division and the decimal part gets truncated.

OK. That's where the problem is. I changed your line 39 to:
average = (double)num / OriginalAmountOfNumbers;
and now the results agree.
You telling this to dtaqee88?
Unfortunately, our programs give different results.


Both of our code is same.The only dif is that I updated u and std_dev also.
closed account (D80DSL3A)
Yes. I was telling that to dtaqee88. I didn't even know you had posted.
Almost 2-1/2 hours had passed without any reply. I thought I was 1st respondent.

It's amazing how often that happens!!
Sorry for the late reply but thanks so much. I would have never caught that error on line 39. Will study this. Thanks again.
Topic archived. No new replies allowed.