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



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
59
60
#include <iostream>
#include <cmath>

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*** "<<endl;
	
	
    while(cin>>data)
    {
    
        ++count;
        sum = sum + data; // this will sum all the numbers for the mean
        cout<< "Value"<<" #"<<count<<": "<<data<<endl;

        num[i] = data;
        i++;
        
          	   
        
    }
   

    double mean= sum / count; // this will calculate the mean
    
    cout<<"The Mean found is: "<< mean <<endl;
    
    double newnum; // this will hold the numbers to be able to minus the mean

    for (int k = 0; k < count; k++)
    {
        
    newnum = pow((num[k] - mean), 2) ;
    sum4 = sum4 + newnum; // this sum the deviation. 
    
    }


    double SD= sqrt(sum4/count);
    
    cout<<"Sample standard deviation : "<< SD <<endl;
	

		
	return 0;
	
}
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




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
#include <iostream>
#include <cmath>

using namespace std;

int main()
{

	int count = 0;
	const int TRIALS = 1000000;
	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 variance = sqrt((sum2 / count) - (mean * mean));
    
    cout<<"The Mean found is: "<< mean <<endl;
    cout<<"Sample standard deviation : "<< variance <<endl;

		
	return 0;
	
}
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.

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
#include <iostream>
#include <cmath>
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.