Standard Deviation with Arrays

I have a problem with this C++ program. It is to computer the standard deviation of the ten numbers that the user enters.
Here is the program:


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

double deviation(const double , int);
double mean (const double , int);

int main ()
{

int size=10;
double x[10];

cout << "Enter ten numbers:";
cin>>size;
mean(x[], size);
deviation (x[], size);
cout<<"The mean is:"<<mean;
cout<<"The standard deviation is:"<<deviation;
return 0;

}

double mean(const double x[], int size)
{
double sum;
double mean;
size=10;
for ( int i = 0; i <=size; i++ )
{
sum += x[i];
}
mean=sum/10;
return mean;
}
double deviation (const double x[], int size)
{
double deviation;
double sum2;
double mean;
size=10;
for ( int i = 0; i <=size; i++ )
{
sum2 += pow((x[i]-mean),2);
}
deviation= sqrt(sum2/(size-1));
return deviation;
}


And here are the errors:

(17) : error C2059: syntax error : ']'
(18) : error C2059: syntax error : ']'

I have tried multiple things, but I dont know how to fix those errors. Thanks for the help.
first let me make the prog more clear:
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
#include <iostream>
#include <cmath>
using namespace std;

double deviation(const double , int);
double mean (const double , int);

int main ()
{

int size=10;
double x[10];

cout << "Enter ten numbers:";
cin>>size;
mean(x[], size);
deviation (x[], size);
cout<<"The mean is:"<<mean;
cout<<"The standard deviation is:"<<deviation;
return 0;

}

double mean(const double x[], int size)
{
double sum;
double mean;
size=10;
for ( int i = 0; i <=size; i++ )
{
sum += x[i];
}
mean=sum/10;
return mean;
}
double deviation (const double x[], int size)
{
double deviation;
double sum2;
double mean;
size=10;
for ( int i = 0; i <=size; i++ )
{
sum2 += pow((x[i]-mean),2);
}
deviation= sqrt(sum2/(size-1));
return deviation;
} 


And here are the errors:

(17) : error C2059: syntax error : ']'
(18) : error C2059: syntax error : ']'
why u didnt spicify the size of the array in line 16 and 17
can try !
ah, see here :
1
2
double deviation(const double , int);
double mean (const double , int);

in the prototype u must say that you will take an argument of an array like this:
1
2
3
double deviation(const double[] , int);
double mean (const double[] , int);

try this also^__^
I changed that, but I still am getting the same syntax errors with the brackets in like 17 and 18. Thank you.
an array is a pointer and when you pass it as an argument into a function you do not need the brackets but when you define/declare the function you need to have the brackets. there are a lot of other errors in your code but im assuming that its still a work in progress. however you have an off by one error in your for loops where the second state should be "i < size" not i <= size"
Last edited on
#include <iostream>
#include <cmath>
using namespace std;

double deviation(double[] , int);
double mean (double[] , int);
double mean2;

int main ()
{

int size=10;
double x[10];
mean2=0;

cout << "Enter ten numbers:";


for (int i=0; i<10; i++)
{
cin>>x[i];
}

cout << "The mean is:"<<mean(x,10);
cout << endl;
cout << "The standard deviation is:"<<deviation (x,10);
return 0;

}

double mean(double x[], int size)
{
double sum=0;

for ( int i = 0; i <=size; i++ )
{
sum += x[i];
}
mean2=sum/10;
return mean2;
}

double deviation (double x[], int size)
{
double deviation;
double sum2=0;

for ( int i = 0; i <=size; i++ )
{
sum2 += pow((x[i]-mean2),2);
}
deviation= sqrt(sum2/(size-1));
return deviation;
}

try this..
Topic archived. No new replies allowed.