no arrays allowed help for standard deviation

Hello,

Totally new at this and im not sure how to re build my code without the use of arrays or vectors?

how do i properly add a cin.clear or cin.seekg(0) to have my while loop restart and perform the standard deviation calculation.

per Teacher

You are not allowed to write your own functions and, even if you know about them, you are not allowed to
use arrays. You may assume the file contains at least two values and only consists of floating-point
values, whitespace, newline characters, and the end of file character.
As you are not allowed to use arrays, you need to read all the values once to calculate the average then
use that average along with each value to calculate the standard deviation

here is the test data.

3.58 62
44 17
2.68 3.44 8.01
6.22
83
7

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960`` ``````#include #include using namespace std; int main() { int count = 0; double data; double sum = 0; double sum4 = 0; double num[100]; int i = 0; cout<<"***Standard deviation calculation*** "<>data) { ++count; sum = sum + data; // this will sum all the numbers for the mean cout<< "Value"<<" #"<
Last edited on
@sr2cute702,
It appears that you should be reading your data from file, not from the console via cin.

Your teacher is incorrect if he/she is telling you to read the data twice to calculate the standard deviation.

Read the data (from file) once, updating sum(data) and sum(data*data) as you read each new value. You don't need to store these data points in arrays just to compute those two sums.
At the end:
mean = sum(data) / count
variance = sum(data*data)/count - mean*mean
(That formula for the variance is equivalent to the fundamental definition of average squared deviation from the mean).
The standard deviation is the square root of the variance.

Note that this is the variance of your given data and not an unbiased estimate of the population variance: many textbooks will use the latter, which has a divisor of (count-1) rather than count in the variance.
Last edited on
thank you for the updated formula, ill update my code and use what you provided. I dont think im brave enought or knowlegeble enough to argue that his wrong. He has a PHD and specialized Game Theory
@lastchance,

here is my updated code, i just found out that my output is not matching my teachers for the standard derivation.

did you happen to know why i'm a bit off?

my out put

 ``` ***Standard deviation calculation*** Value #1: 3.58 Value #2: 62 Value #3: 44 Value #4: 17 Value #5: 2.68 Value #6: 3.44 Value #7: 8.01 Value #8: 6.22 Value #9: 83 Value #10: 7 The Mean found is: 23.693 Sample standard deviation : 27.4374 ```

teachers output

 ``` ***Standard deviation calculation*** Value #1: 3.58 Value #2: 62 Value #3: 44 Value #4: 17 Value #5: 2.68 Value #6: 3.44 Value #7: 8.01 Value #8: 6.22 Value #9: 83 Value #10: 7 Sample standard deviation: 28.9215 ```

 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````#include #include using namespace std; int main() { int count = 0; const int TRIALS = 1000000; double data; double sum = 0; double sum2 = 0; cout<<"***Standard deviation calculation*** "<>data) { ++count; sum = sum + data; sum2 = sum2 + data * data; cout<< "Value"<<" #"<
Last edited on
Your answer is strictly "correct". You have found the standard deviation (although you call it variance) of the data given.

Your teacher is using a slightly different definition - that of an unbiased estimate of a larger population of which your data is only a sample. If you multiply the variance formula by count/(count-1.0) BEFORE you take the square root then you would get the teacher's answer. Be careful of integer division.

Incidentally, the standard deviation is the square root of the variance. Please use variables accordingly.
This rewrite might help to explain.

 ``1234567891011121314151617181920212223242526272829`` ``````#include #include using namespace std; int main() { int count = 0; double data; double sum = 0; double sum2 = 0; cout << "***Standard deviation calculation***" << endl; while ( cin >> data ) { ++count; sum = sum + data; sum2 = sum2 + data * data; cout << "Value" << "#" << count << ": " << data << endl; } double mean= sum / count; double variance1 = sum2 / count - mean * mean; double variance2 = variance1 * count / ( count - 1.0 ); cout << "The Mean is: " << mean << endl; cout << "Standard deviation of given data: "<< sqrt( variance1 ) << endl; cout << "Standard deviation (unbiased estimator): "<< sqrt( variance2 ) << endl; return 0; }``````

 ```***Standard deviation calculation*** Value#1: 3.58 Value#2: 62 Value#3: 44 Value#4: 17 Value#5: 2.68 Value#6: 3.44 Value#7: 8.01 Value#8: 6.22 Value#9: 83 Value#10: 7 The Mean is: 23.693 Standard deviation of given data: 27.4374 Standard deviation (unbiased estimator): 28.9215```

The two standard deviations are often called (in the same order):
"population standard deviation" - i.e. your data WAS the whole population
"sample standard deviation" - i.e. your data was a small sample from a much larger population for which you are trying to ESTIMATE a standard deviation.

Personally, I don't like either of those terms.

The reason for the count-1 in the second case is that you are also estimating a larger-population mean, so that by using the mean from your data you have one less degree of freedom.

Which definition your teacher wants here is, I'm afraid, something you must negotiate with him/her.
Topic archived. No new replies allowed.