Mode

Feb 23, 2014 at 3:49pm
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.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <conio.h>
#include <math.h>
#include <numeric>
#include <algorithm>
#include <vector>
#include <stdlib.h>
using namespace 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;
}
Feb 23, 2014 at 3:55pm
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/
Feb 23, 2014 at 3:57pm
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 Feb 23, 2014 at 3:58pm
Feb 23, 2014 at 4:12pm
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <iostream>
#include <conio.h>
#include <math.h>
#include <numeric>
#include <algorithm>
#include <vector>
#include <stdlib.h>
using namespace 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;
}



Feb 23, 2014 at 5:24pm
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?
Feb 24, 2014 at 6:52pm
Im not really sure what you mean about std::count
Feb 24, 2014 at 6:59pm
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?

Feb 24, 2014 at 7:13pm
Im trying to get the mode of the movies if there is any when the user inputs.
Topic archived. No new replies allowed.