Mar 25, 2010 at 3:23am UTC
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.
Mar 25, 2010 at 3:38am UTC
why u didnt spicify the size of the array in line 16 and 17
can try !
Mar 25, 2010 at 6:33pm UTC
I changed that, but I still am getting the same syntax errors with the brackets in like 17 and 18. Thank you.
Mar 26, 2010 at 1:57am UTC
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 Mar 26, 2010 at 1:58am UTC
Mar 26, 2010 at 2:51am UTC
#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..