Find the mode.

I'm writing a program that gathers statistical data. It asks for a number of students and the number of movies that each student watched. After that, the program calculates de average, finds the mean and the mode. I already wrote the part of the average and the mean. However, I don't know what to do to find the mode. Can anyone give me a bare idea on how to do this?

This is my code:

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>

using namespace std;
void getNumMovies(int [], int);
void getAverage(int [], int);
void sortArray(int [], int);
void getMean(int *, int);
void getMode(int *, int);

int main () 
{
     int number;

cout<<"How many students were surveyed?: ";
cin>>number;
cout<<endl;

     int array1[number];

getNumMovies(array1, number);


system("pause");
return 0;
}//end of main function

//getNumMovies() Definition
void getNumMovies(int array2[], int size2)
{
     for(int index = 0; index < size2; index++)
     {
     cout<<"How many movies student "<<(index + 1)<<" watched?: ";
     cin>>array2[index];
     cout<<endl;
     }//end for loop

getAverage(array2, size2);

}//end of getNumMovies()

//getAverage() Definition
void getAverage(int array3[], int size3)
{
     double average;
     double    sum = 0;
     for(int index = 0; index < size3; index++)
     {
             sum = sum + array3[index];
     }

     average = (sum)/(size3);
     cout<<"This is the average: "<<average;
     cout<<endl;

sortArray(array3, size3);

}//end of getAverage()

//sortArray() Definition
void sortArray(int array4[], int size4)
{
     int temp;
     bool swap;
     
     do
     { swap = false;
       for(int count = 0; count < (size4 - 1); count++)
       {
               if( array4[count] > array4[count + 1])
               {
                   temp = array4[count];
                   array4[count] = array4[count + 1];
                   array4[count + 1] = temp;
                   swap = true;
               }//end if
       }//end for
     }while (swap);//end do-while

getMean(array4, size4);
getMode(array4, size4);

}//end of sortArray()

//getMean() Definition
void getMean(int *array5, int size5)
{
     int remainder  = (size5)%(2);
     int number     = (size5)/(2);
     double meanAvg;
     
     if( remainder > 0 || remainder < 0 )
     {
         cout<<"This is the mean: "<<*(array5 + number)<<endl;
     }//end if

     else
     {
         double temp;
         temp = *(array5 + number) + *(array5 + (number - 1));
         meanAvg = temp/2;
         cout<<"This is the mean: "<<meanAvg<<endl;
     }//end else

}//end of getMean()

//getMode() Definition
void getMode(int *array6, int size6)
{
     int dummy  = 0;
     for(int counter = 1; counter < size6; counter++)
     {
             if(*array6 == *(array6 + counter))
             dummy++;
     }//end for loop
     
     cout<<dummy<<endl;
}//end of getMode() 
Just so you know, function parameters don't need to be different from other function parameters...i.e.:

1
2
3
4
5
6
7
getMean(int *array, int size) {
//...
}
getMode(int *array, int size) {
//still works
//...
}


Anyway, to calculate the mode, I would use an std::set to store the data...however, if that is not an option, I would suggest sorting the array then counting the values (this would make it much easier).
How does this compile? Are you not using an ISO compiler? what compiler do you use because it isn't valid c++.
1
2
3
4
5
6
7
8
int number;

cout<<"How many students were surveyed?: ";
cin>>number;
cout<<endl;

// this shouldn't compile since number isn't a const expression
int array1[number];


I do not know what a mode is in the context of this program. Am I missing something? I wonder how firedraco knew.
EDIT: Should have done a quick google search. I didn't know about the mode concept with regards to statistics. I agree with jsmith, that is a very easy problem to solve using an associative array. Simply have a key, value map where the keys are each value you are keeping track of and the value is the count that indicates the number of occurrences.
Last edited on
I dont know why, but i've been thinking about you're problem all day... The problem seems to vague to program a calculation. Unless theres some formula that you can use to calculate the mode. Seems impossible, at least to me. If it was a survey I think it should be determined in ranges instead of exact numbers don't you think?

You have to keep a list of every value entered and how many times it was entered. Then find in the list the value with the largest number of times entered.

If you can use std::map, this problem is trivially solvable.
Topic archived. No new replies allowed.