The error i get
cannot convert 'std::vector<double> to double in return
at return aboveAverage; line
1 2 3 4 5 6 7 8 9 10
double aboveAverage(vector<double> columnsList)
{
int n = columnsList.size();
double mean = getMean(columnsList);
for (int i = 0; i < n; i++) {
if (columnsList[i] > mean)
}
return aboveAverage;
}
You get that error because you are trying to return aboveAverage, which is the same function you are already working on. You have to return a variable, or something that is also a double like how you defined the function.
im supposed to return all values in the columnLists that are greater than the mean
To do that, I think a better approach would be to save each value that passes that if condition to a vector or array, then return that vector at the end of the function. Or, save each value that passes the if condition to a double variable and return that variable. Since you are counting things, you wouldn't need decimals, so I would change the types from double to int, unless you explicitly need it to be a double.
It's possible to return other functions as long as they are not void because they have to return an answer. (for ex. a function like your getMean() gives an answer so that's a possible return statement, but you can't return void functions bc they are void of a value)
@hnfirt
Are you trying to return the NUMBER of values that are greater than the mean (in which case you would need to count them and return that int) ...
... or are you trying to return the actual VALUES that are greater than the mean (in which case you could put them in a new vector and return that vector).
At the moment you are returning a double (which doesn't make much sense either way) and you are trying to make it the name of the function (which makes even less sense).
... or are you trying to return the actual VALUES that are greater than the mean (in which case you could put them in a new vector and return that vector).
this im trying to do this!
this is my first time coding.. i still do not understand much.
so how do i put them in a new vector?
Declare a new vector at the start of the function; something like vector<double> result;
Where a value is greater than the mean, use the vector push_back() function to add to this vector: http://www.cplusplus.com/reference/vector/vector/push_back/
(This is probably how you put things in your original vector in the first place.)