LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   error when finding the standard deviation of a vector (https://www.linuxquestions.org/questions/programming-9/error-when-finding-the-standard-deviation-of-a-vector-376928/)

mshinska 10-25-2005 09:44 PM

error when finding the standard deviation of a vector
 
I keep getting this error, which i have never seen before:

lab10.cpp: In function `int main()':
lab10.cpp:50: `sorry, not implemented: `float_expr' not supported by dump_expr
(<expression error> - average)' cannot be used as a function


my code that gets the error is:

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

int main(void)
{
vector<int> values;
int input;
cout << "Enter a number: ";
cin >> input;
while(!cin.fail())
{ values.push_back(input);
cout << "Enter a number: ";
cin >> input;
}
int f;
float stdDev;
float sum2= 0.0;
for (int f=0; f < values.size(); f = f+1)
{ sum2 = ((sum2) + (values[f]-average)(values[f]-average) );
}
sum2 = sum2/ values.size();
stdDev=sqrt((sum2));

cout << "The standard deviation is: " << stdDev << endl;

return 0;
}

mshinska 10-25-2005 09:50 PM

i forgot to add this when i copy/pasted

float average;
float sum;
for (int i = 0; i<values.size(); i = i + 1)
{ sum = sum + values[i];
average = sum/ values.size();

}

dmail 10-25-2005 09:52 PM

what does this line do?
Code:

sum2 = ((sum2) + (values[f]-average)(values[f]-average) )
whats "average" and what is "(values[f]-average)(values[f]-average)" do?

just seen you post, i still dont know what the second part does tho.

Code:

i forgot to add this when i copy/pasted

float average;
float sum;
for (int i = 0; i<values.size(); i = i + 1)
{ sum = sum + values[i];
average = sum/ values.size();

}

your calculating the average every loop here.

mshinska 10-25-2005 10:01 PM

standard deviation is the square root of (each element - mean)^2 added together divided by the number of elements

for example if you have a vector of [ 1 2 3] the standard deviation is
sqrt( ( (1-2)^2 + (2-2)^2 + (3-2)^2)/3)

i explained average in my first correction



dmail 10-25-2005 10:05 PM

you seem to be missing the point, or i am.
is this some macro magic or something
Code:

(values[f]-average)(values[f]-average)
what does this evaluate to? is there susposed to be an operator in there?

mshinska 10-25-2005 11:03 PM

oh i get what you are saying i forgot the multipication symbol (*), thank you for your help it works now!!!!


All times are GMT -5. The time now is 04:36 PM.