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.
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.
#include <iostream>
#include <cmath>
usingnamespace 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);
}
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.
#include <iostream>
#include <iomanip>
#include <cmath>
usingnamespace std; // <--- Best not to use.
constexprint 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;
elsereturn 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.
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.
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.)
//
// 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;
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:
#include <iostream>
#include <cmath>
usingnamespace 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