Why do i get this error?

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;
}
Which particular error would you like me to address?

- aboveAverage is the name of a function. Why are you trying to return the name of a function?

- Having asked if columnsList[i] is greater than mean ... you do what? (If anything?)

- Aren't you supposed to be counting how many are above mean? If so, there's no variable to do it.

- I think it more likely that you would want to return an int than a double here.
im supposed to return all values in the columnLists that are greater than the mean
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)
Last edited on
@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).
thank you for the replies! appreciate it so much!

... 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.)

At the end:
return result;
Last edited on
Topic archived. No new replies allowed.