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.
#include <iostream>
#include <conio.h>
#include <math.h>
#include <numeric>
#include <algorithm>
#include <vector>
#include <stdlib.h>
usingnamespace std;
int main()
{
int size = 0;
double mean = 0;
vector<double>movies;
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 "<<size<< " students followed by ENTER otherwise 0 to quit:\n";
movies.resize(size);
bool more = true;
while (more)
{
double s;
cin >> 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: "<<mean<<" movies\n";
cout << "The median movies watched is: "<<median<<"\n";
return 0;
}
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.
#include <iostream>
#include <conio.h>
#include <math.h>
#include <numeric>
#include <algorithm>
#include <vector>
#include <stdlib.h>
usingnamespace std;
int main()
{
int size = 0;
double mean = 0;
double mycount = 0;
vector<double>movies;
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 "<<size<< " students followed by ENTER otherwise 0 to quit:\n";
movies.resize(size);
bool more = true;
while (more)
{
double s;
cin >> 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: "<<mean<<" movies\n";
cout << "The median movies watched is: "<<median<<"\n";
cout << "The mode of the movies is:";
return 0;
}
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?
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?