Need Help With Programming Assignment

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

float CalstdDeviation(float StoreArr[]);

int main()
{
int i;
float StoreArr[5];
cout << "Enter five(5) elements: ";
for(i = 0; i < 5; ++i)
cin >> StoreArr[i];

cout << endl << "average :" << CalstdDeviation(StoreArr) << endl;

cout << endl <<"The Sample Standard Deviation is = " << CalstdDeviation(StoreArr) << endl;

return 0;
}

float CalstdDeviation(float StoreArr[])
{

float sum = 0.0, mean, average, standardDeviation = 0.0;

int i;

for(i = 0; i < 5; ++i)

sum += StoreArr[i];


mean = sum / 5;
cout << mean << endl;

standardDeviation += pow(StoreArr[i] - mean, 2);
return sqrt(4 - 1 / sum);


return(average = sum / 5);
}

// main




The five numbers entered should be 1, 6, 9, 5, 0. I suppose to have 4.2 as an average and the sample standard deviation is 3.70135. Can some help please?
Currently I am getting the average and the standard deviation twice.
Hello jwilson2,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



First prefer to use "double" over "float".

As a start until I can test the program:
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
#include <iostream>
#include <cmath>

using namespace std;  // <--- Best not to use.

double CalstdDeviation(double StoreArr[]);

int main()
{
    //int i;  // <--- Better defined in the for loop.
    double StoreArr[5]{};

    cout << "Enter five(5) elements: ";

    for (int i = 0; i < 5; ++i)
        cin >> StoreArr[i];

    cout << endl << "average :" << CalstdDeviation(StoreArr) << endl;

    cout << endl << "The Sample Standard Deviation is = " << CalstdDeviation(StoreArr) << endl;

    return 0;
}

double CalstdDeviation(double StoreArr[])
{

    double sum{}, mean{}, average{}, standardDeviation{};

    //int i;

    for (int i = 0; i < 5; ++i)
        sum += StoreArr[i];

    mean = sum / 5;

    cout << mean << endl;

    standardDeviation += pow(StoreArr[i] - mean, 2);  // <--- Problem here. "i" is local to the for loop.
                                                      // Also "i" would end with value of 5, but the array does not have an element 5.

    return sqrt(4 - 1 / sum);

    return(average = sum / 5);
}


Andy
Hello jwilson2,

Your function is trying to do 2 different calculations. The function normally returns only 1 item. Which one do you want?

In my above code line 42 returns a value which you first expect to be an average, but that is not what you return and line 44 is never reached.

If you want the function to return 1 of 2 calculations you will either need to write another function or add a second parameter to determine which return statement to return. This second parameter could be a simple "bool".

I could not use line 39 yet because I am not sure which element(s) of the array you want to use.

Line 15 is good to figure the average, but I do not believe that is how you figure the "mean". I believe there is a difference between even number of elements and odd.

For the deviation I would have to look that part up.

Andy
The output should show

Enter five(5) elements: 1
Enter five(5) elements: 6
Enter five(5) elements: 9
Enter five(5) elements: 5
Enter five(5) elements: 0

the average is 4.2

the sample standard deviation is 3.70135
Hello jwilson2,

You say:
the sample standard deviation is 3.70135
but I keep getting 3.310589 using the numbers 1, 6, 9, 5, 0.

This is what I came up with for the function:
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
49
50
51
52
53
54
55
56
57
58
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;  // <--- Best not to use.

constexpr int MAXSIZE{ 5 };

double CalstdDeviation(double StoreArr[], char type);

int main()
{
    //int i;  // <--- Better defined in the for loop.
    double StoreArr[MAXSIZE]{ 1.0, 6.0, 9.0, 5.0, 0.0 };

    std::cout << std::fixed << std::showpoint << std::setprecision(6);

    cout << "Enter five("<<MAXSIZE<<") elements: ";

    for (int i = 0; i < MAXSIZE; ++i)
    {
        // <--- Needs a prompt.
        cin >> StoreArr[i];
    }

    cout << "\n The average is = " << CalstdDeviation(StoreArr, 'A') << '\n';

    cout << "\n The Sample Standard Deviation is = " << CalstdDeviation(StoreArr, 'D') << '\n';

    return 0;  // <--- Not required, but makes a good break point.
}

double CalstdDeviation(double StoreArr[], char type)
{
    double sum{}, mean{}, average{}, deviationTotal{},standardDeviation{};

    //int i;  // <--- Best defined in for loop.

    for (int i = 0; i < MAXSIZE; ++i)
        sum += StoreArr[i];

    average = mean = sum / MAXSIZE;

    //cout << "\n The mean is: " << mean << '\n';

    for (int i = 0; i < MAXSIZE; i++)
    {
        deviationTotal += pow(StoreArr[i] - mean, 2);
    }

    standardDeviation = sqrt(deviationTotal / MAXSIZE);
    
    if (type == 'A')
        return average;
    else
        return standardDeviation;

}


I do not guarantee that my calculation for "standard devation" is correct, but I believe it is.

Earlier when I saw "mean" I was thinking of "median" because I am not use to using "mean" as another name for "average". Sorry about that.

Andy
Handy Andy,
Your standard deviation is strictly correct.

The OP wants the unbiased estimate of the standard deviation of a larger population. If you multiply your answer by sqrt(N/(N-1)), or sqrt(5/4) in this instance, you will get the answer that the OP has been told to get.

Alternatively, divide by (MAXSIZE-1) rather than MAXSIZE at the appropriate point when calculating the variance. The reason is that you are estimating the mean of the larger population by the mean of your data, so you have one less degree of freedom.

It's always an ambiguous point when asked for "the standard deviation". Without more information either answer would fit the bill.
Last edited on
@lastchance,

Thank you for the input.

I do not recall ever getting that deep into that type of math and if I did I naever had much of a chance to use it.

I will reread you post a couple of times until it sinks in better.

Andy
Handy Andy,

You can read more about it here:
https://www.mathsisfun.com/data/standard-deviation-formulas.html
(You have to go quite a long way down to get to what they call the "sample standard deviation" - which in my opinion is completely contradictory. The reason for the N-1 formula is to get a statistic whose expectation is the whole-population standard deviation and the -1 comes from the loss of a degree of freedom in estimating the population mean by that from your data.)
Last edited on
//
// ELEN 1301 Programming Assignment #8.
// Name : Jermaine Wilson.
// Student ID : L20264155.
// Due date : March 27, 2021
// Purpose of the program :
// Creating a program that usus an array and calculate an unbiased standard
//deviation.
// Section 1 : Receive a number and store it in an array. We assume there
//will be always 5 input numbers.
// Section 2 : Repeat Section 1 five times.
// Section 3 : Find the mean of the five numbers.
// Section 4 : Subtract mean from each input that is stored in the array.
// Square it.
// Section 5 : Repeat Section 4 for all the stored input, so that the
// summention is done.
// Section 6 : Divide the sum by (N – 1), in this case it would be 4, and
// take a square root of the value to show the Standard Deviation.
//

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

int main()
{
double ary[5], sum{}, mean, average{}, deviationTotal, standardDeviation;
int i;

for(i = 0; i < 5; i++)
{
cout << "Please enter a number " << i + 1 << " : ";
cin >> ary[i];
}

for(i = 0; i < 5; ++i)
sum += ary[i];
average = mean = sum / 5;

cout << "\n The average is: " << mean << '\n';

for(i = 0; i < 5; i++)
{
deviationTotal = pow(ary[i] - mean, 2);
standardDeviation = sqrt(deviationTotal / 'n' - 1);
}

cout << endl << "The Sample Standard Deviation is " << standardDeviation << endl;
}// main

something is still incorrect I keep getting nan for The sample standard deviation is.
Please use code tags - Handy Andy has shown you how to.

You are trying to divide by a character, not a number.

deviationTotal should add the extra deviation on each pass through the loop. You aren't adding anything.

You are also trying to calculate standard deviation in the middle of a loop. Do it after the loop has finished.

Last edited on
If you compute the sum of x2 along with the sum of x, then you don't need to store the values. With a little algebra, you find that the standard deviation is sqrt(M/(n*(n-1))) where M is n*sum(x2) - sum(x)2. This is how my handy hand-held calculator from 1977 computes it. Modifying your last program:
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
#include <iostream>
#include <cmath>
using namespace std;

int
main()
{
    double value;		// the value entered
    double sumX{}, sumX2{};	// sum of x and sum of x^2
    int n{};			// number of items entered

    double average, standardDeviation;

    while (true) {
	cout << "Please enter a number " << n + 1 << " (or ctrl-Z to exit) : ";
	if (!(cin >> value)) break;
	++n;
	sumX += value;
	sumX2 += value * value;
    }

    average = sumX / n;
    cout << "\n The average is: " << average << '\n';

    standardDeviation = sqrt( (n*sumX2 - sumX*sumX) / n / (n-1));
    cout << endl << "The Sample Standard Deviation is " << standardDeviation << endl;
}

$ ./foo
Please enter a number 1 (or ctrl-Z to exit) : 1
Please enter a number 2 (or ctrl-Z to exit) : 6
Please enter a number 3 (or ctrl-Z to exit) : 9
Please enter a number 4 (or ctrl-Z to exit) : 5
Please enter a number 5 (or ctrl-Z to exit) : 0
Please enter a number 6 (or ctrl-Z to exit) :
 The average is: 4.2

The Sample Standard Deviation is 3.70135

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <cmath>
#include <valarray>
using namespace std;

int main()
{
   int n;
   cout << "Input n: ";   cin >> n;
   cout << "Enter " << n << " numbers: ";
   valarray<double> X(n);
   for ( double &x : X ) cin >> x;
   double Xbar = X.sum() / n;
   cout << "Mean = " << Xbar << '\n';
   cout << "Standard deviation = " << sqrt( ( ( X - Xbar ) * ( X - Xbar ) ).sum() / ( n - 1 ) );
}


Input n: 5
Enter 5 numbers: 1 6 9 5 0
Mean = 4.2
Standard deviation = 3.70135 
Last edited on
Topic archived. No new replies allowed.