### Mode

I needed to compute average median and mode for numbers in a vector. Im having a few issues I have already figured out how to calculate average and median just not mode. Also the program crashes when 0 is entered instead of exiting nicely. Also i keep getting a error on line 47 saying primary expression expected before else. Any help would be appreciated.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` ``````#include #include #include #include #include #include #include using namespace std; int main() { int size = 0; double mean = 0; vectormovies; cout << "This Program will give you the average, median, and mode of a given number of movies watched by college students\n"; cout << "How many students were surveyed?: "; cin >> size; cout << "Enter the amount of movies watched by each of the "<> s; if (s == 0) more = false ; else movies.push_back(s); } int i; double sum = 0; for (i = 0; i < movies.size(); i++); { sum+=movies.at(i); } mean = sum/movies.size(); sort(movies.begin(), movies.end() ); double median = 0; double mid = 0; mid = movies.size()/2; if( (int) mid % 2 == 0); median = (movies[mid] + movies[mid+1])/2; else median = (movies[mid+0.5]); cout << "The average movies watched is: "<
 Also i keep getting a error on line 47 saying primary expression expected before else.

Delete the semi colon on Line 45.

As for mode, the "std::count()" function from the 'algorithm' header should solve that problem: http://www.cplusplus.com/reference/algorithm/count/
Pierreseoul wrote:
i keep getting a error on line 47 saying primary expression expected before else. Any help would be appreciated.

Remove the semicolon from line 45.
Last edited on
Ok good looking out Computergeek01 and Peter87. I made the change, but now when i use std count i get no matching function for call to count. Also I keep getting warnings about converting int to double, so i attempted to enclose the if statement in brackets, but now I get a error expected ( before [ on line 47.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960`` ``````#include #include #include #include #include #include #include using namespace std; int main() { int size = 0; double mean = 0; double mycount = 0; vectormovies; cout << "This Program will give you the average, median, and mode of a given number of movies watched by college students\n"; cout << "How many students were surveyed?: "; cin >> size; cout << "Enter the amount of movies watched by each of the "<> s; if (s == 0) more = false ; else movies.push_back(s); } int i; double sum = 0; for (i = 0; i < movies.size(); i++); { sum+=movies.at(i); } mean = sum/movies.size(); sort(movies.begin(), movies.end() ); double median = 0; double mid = 0; mid = movies.size()/2; if { ( (int) mid % 2 == 0) median = (movies[mid] + movies[mid+1])/2; else median = (movies[mid+0.5]); } mycount = std::count (movies.begin(), movies.end() ); cout << "The average movies watched is: "<

The function "std::count()" takes three parameters.

 Also I keep getting warnings about converting int to double, so i attempted to enclose the if statement in brackets, but now I get a error expected ( before [ on line 47.

What would make you try that? Undo the bracket enclosure. Is there any particular reason that "movies" is a double and not just an integer? Are you allowing for people to watch a fraction of a movie or something?
Im not really sure what you mean about std::count
 Im not really sure what you mean about std::count

He meant what he said. std::count requires 3 arguments be fed to it. You only feed it 2 (line 54.) What is it, exactly, that you are counting in the movies sequence?

Im trying to get the mode of the movies if there is any when the user inputs.
Topic archived. No new replies allowed.