To many errors in statistical calculation

Hello guys, I was given an assignment by my prof to code statistical calculation which are average, variance, and standard deviation. Some says it is easier to use pointer but to this date, my prof only covered up to array. I'm a total beginner to begin with. My output of this code should be:

How many numbers do you wish to enter? 5
Enter 5 numbers:
3 4 2 1 7
The average is 3.4
The variance is 5.3
The standard deviation is 2.30217

or

How many numbers do you wish to enter? 6
Enter 6 numbers:
1
10.4
-6.2
5.55
0.03
-17
The average is -1.03667
The variance is 92.1911
The standard deviation is 9.60162

This is my code as far and they give to many errors. Please clarify what I'm doing wrong.

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

using namespace std;

int main()
{   
  	int num;
	double sum =0,variance = 0,deviation =0, value;
    
	cout << "How many numbers do you wish to enter? ";
    cin >> num;
    
    const int n = num;
  	double	a[n];
    
	cout << "Enter " << num << " numbers: " << endl;    
    
	for(int i=0; i<n; i++)
    {
        
	cin >> a[i]+i;
	sum = sum + (a[i]+i);
        
	}
    
	double mean = sum / n;

	for(int i=0; i<n; i++)
    {
        
	variance = variance + ((a[i]+i) - mean) * ((a[i]+i) - mean);
        
	}
    
	variance = variance / (n-1);
	deviation = sqrt(variance);
    
	cout<<"The average is "<<mean<<endl;
	cout<<"The variance is "<<variance<<endl;
	cout<<"The standard deviation is "<<deviation<<endl;
    
	return 0;
}
First thing: line 15 is not legal C++.

Add to preamble: #include <vector>
Replace line 15 with std::vector<double> a( n );
Till now, prof only taught iostream, cmath, iomanip, and math.h. So, I haven't learn vector either, but I'll give it a try.

Edit:

It still gives many errors.

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>
#include <vector>

using namespace std;

int main()
{   
  	int num;
	double sum =0,variance = 0,deviation =0, value;
    
	cout << "How many numbers do you wish to enter? ";
    cin >> num;
    
    const int n = num;
  	vector<double>	a(n);
    
	cout << "Enter " << num << " numbers: " << endl;    
    
	for(int i=0; i<n; i++)
    {
        
	cin >> a[i]+i;
	sum = sum + (a[i]+i);
        
	}
    
	double mean = sum / n;

	for(int i=0; i<n; i++)
    {
        
	variance = variance + ((a[i]+i) - mean) * ((a[i]+i) - mean);
        
	}
    
	variance = variance / (n-1);
	deviation = sqrt(variance);
    
	cout<<"The average is "<<mean<<endl;
	cout<<"The variance is "<<variance<<endl;
	cout<<"The standard deviation is "<<deviation<<endl;
    
	return 0;
}
Last edited on
You aren't too far off, just remove the '+i' in line 23 rewrite line 24 as

sum += a[i]

and the program should be fine. A few general points otherwise:

- variable value is unused and should be removed if so
- why do you need variables n and num
- you may also wish to give your vector a more memorable name just as you've done for the other variables
- I haven't double-checked the variance formula, assume that wasn't the problem
- since you've been introduced to iomanip you may also wish to round off the results to 2 or 3 decimal places
Last edited on
Thank you so much. You guys solved my problems. But I'm still curious about #include <vector>.

If I just want to use cmath and iostream, how can I change my code to give the asked ouput?
#include<vector> is the header file required when you work with vectors, if you'd used the array instead as in the OP you wouldn't need it. The change would be instead of vector<double> a(n) it'd be double a[n] just as you had previously
If I just want to use cmath and iostream, how can I change my code to give the asked ouput?

There are at least two approaches:

1. Use a "large enough" static array:
1
2
3
4
5
6
7
8
9
10
11
12
const int MaxN = 42; // something reasonably big
double a[MaxN];

int n;
std::cin > n; // get user's choice
if ( MaxN < n ) {
  // Our array is too small. We can either
  // quit program: return 1;
  // or
  // use less than user wants: n = MaxN;
  // either way: tell to the user about the problem
}


2. Manage dynamic memory manually:
1
2
3
4
5
6
7
8
9
10
11
int n;
std::cin > n; // get user's choice
if ( n < 1 ) {
 // handle this error;
}

double * a = new double [n]; // dynamic memory allocation

// use a

delete [] a; // deallocation. Must be done properly 

The deallocation can be non-trivial in complex branched code. To make it more automatic, the standard library offers smart pointers, for example std::unique_ptr.
Topic archived. No new replies allowed.