How would i go about finding the mode (the number that appears most frequently) in a list of numbers i put in an array? I'm in a comp. science class right now, and i just can't think of how i would do it.

Printable View

- 05-14-2003ShnakepupFinding the mode of an array
How would i go about finding the mode (the number that appears most frequently) in a list of numbers i put in an array? I'm in a comp. science class right now, and i just can't think of how i would do it.

- 05-14-2003hk_mp5kpdw
One way would be to loop through the numbers in the array and put them into a

**map<int,int>**container where the key stores the number from the input array and the value represents the number of times that value has occured so far. Each time you add a key to the map, you increment its associated value. What results is essentially a sorted array (sorted by key) that stores the frequency, i.e. number of occurances, that a particular value has occured in the input array. You could then search through this map looking for the key that had the highest value.

Declaring the map variable and initializing it with the input array will take 2-3 lines of code. Performing the search and returning the key with the highest found value will take another half-dozen lines of code. - 05-14-2003Mister C
You do realize there is a much easier way to solve this problem rather than using containers?

- 05-14-2003volk
Yeah, you can just use pointers...I think. I wish I knew how. :(

- 05-14-2003alpha
You could also just use another array. but it could take up a bit more memory than needed. but for learning purposes, memory concerns are unnecessary at this point.

- 05-14-2003Jamsan
Just have an array the size of the highest possible numbers....and then for each cell in the array, just increment array2[array1[i]], then find the max of the 2nd array

- 05-14-2003the Wookie
hmm, now i have something to do..im gonna see if i can do this.

wouldn't sorting it be a good idea? - 05-14-2003the Wookie
alright, it uses 2 vectors, this was the best i can come up with for now, im kinda tired lol.

it just makes 2 parallel vectors, 1 holding the numbers, and one holding the counter. the main loop just searches the first list against itself, finding duplicates..

so if the list is

Code:`5 2`

0 1

1 1

5 2

i was also originally going to use arrays but i couldnt get them to cooperate, silly VC

edit: ignore the "bool found;" you can just delete that, i was trying out a different method before i came up with this one - 05-14-2003Mister C
If any of you have the Deitel C++ How to Program text: Pages 278-282 has a program that finds the mean median and mode.

Code:`void mode(int freq[], int ans[], int size)`

- 05-14-2003tetra
or.... he could just do a switch statement.....

if the mode is all your looking for....

you could make counters for each number.

and a switch something like this.

Code:

for(int i = 0; i < numOfEntries; i++)

{

switch (array[i])

{

case 1:

1ctr++;

break;

case 2:

1ctr++

break;

}

// etc. etc. just have them enter a fixed range so you arent killing yourslef over switch statments...

you could say.....

class operation

{

int virtual sortNum(int []) = 0;

}

thats a long way and the switch serves a WAY easier solution but if you wanna impress the teacher do one of those :) - 05-14-2003Mister C
What if you had a lot of numbers - you could have a very large switch statement. The loop will do it with short code.

- 05-15-2003hk_mp5kpdw
This is what I was talking about:

Code:`#include <map>`

#include <iostream>

using namespace std;

int mode(int array[], int size)

{

// Create and load map container from input array

map<int,int> IntMap;

for( int a = 0; a < size; ++a ) IntMap[array[a]]++;

// Find maximum value in map.

map<int,int>::iterator it = IntMap.begin();

int max = it->first;

for( ; it != IntMap.end(); ++it )

if( IntMap[it->first] > IntMap[max] ) max = it->first;

return max;

}

int main()

{

int data[] = { 3, 4, 7, 2, 6, 9, 4, 8, 4 };

cout << "Mode is " << mode(data,sizeof(data)/sizeof(data[0])) << endl;

return 0;

}

The**map**serves the same function as the arrays and**vector**containers mentioned by*the Wookie*and*Jamsan*. What's**not**easy about that? - 05-15-2003Shnakepup
Here's what i have so far. Please forgive any errors, unused methods, or just general idiocy. All i care about is functionality. After i get it to work,

*then*i will go for finesse.

Code:`#include <iostream.h>`

void getnumbers(int *array);

void sort(int *array);

void modemedian(int *array, int &mode, int &median);

void print(int *array, int &mode, int &median);

int main()

{

int array[21];

int mode, median;

getnumbers(array);

sort(array);

modemedian(array, mode, median);

print(array, mode, median);

return 0;

}

void getnumbers(int *array)

{

int i;

cout << "Enter 21 numbers. Hit enter for each one: ";

for(i=0;i<21;i++)

cin >> array[i];

}

void sort(int *array)

{

int temp, i, j;

for(int i=0;i<20;i++)

{

for (int j=i + 1;j<21;j++)

{

if (array[i] > array[j])

{

temp = array[i];

array[i]=array[j];

array[j]=temp;

}

}

}

}

void modemedian(int *array, int &mode, int &median)

{

int temp;

int tempb = 0;

int tempc = 0;

int counters[21];

median = array[10];

//Mode section

for(int i=0;i<21;i++)

{

for(int j=0;j<21;j++)

{

if (array[i]==array[j])

counters[i]++;

}

}

cout << "Number of hits per number: "; //I put this here just so i could see what the counter array was getting

for(int i=0;i<21;i++)

cout << counters[i] << " ";

cout << endl;

for(int i=0;i<21;i++)

{

if (counters[i]>tempb)

{

tempb=counters[i];

tempc = i;

}

}

mode=array[tempc];

//end mode section

}

void print(int *array, int &mode, int &median)

{

cout << "You typed: ";

for(int i=0;i<21;i++)

cout << array[i] << " ";

cout<< endl << "Mode: " << mode;

cout<< endl << "Median: " << median;

}

- 05-15-2003hk_mp5kpdw
The basic logic looks OK but I do see a potential issue with the

**counters**array not being initialized.

Code:`void modemedian(int *array, int &mode, int &median)`

{

int temp;

int tempb = 0;

int tempc = 0; // Extra variable, not needed, see below

int counters[21];

median = array[10];

//Mode section

for(int i=0;i<21;i++)

{

counters[i] = 0; // Make sure you initialize the counter

// before attemtping to increment unknown values

for(int j=0;j<21;j++)

{

if (array[i]==array[j])

counters[i]++;

}

}

cout << "Number of hits per number: "; //I put this here just so i could see what the counter array was getting

//My compiler would have "issues" with using the same variable "i"

// at this spot so I changed it

for(int k=0;k<21;k++)

cout << counters[k] << " ";

cout << endl;

// Again, use of "i" variable here would cause me "issues"

// with my compiler so I changed it.

for(int l=0;l<21;l++)

{

if (counters[l]>tempb)

{

tempb=counters[l];

//tempc = i; // Not needed...

mode = array[l]; // Use this instead

}

}

//mode=array[tempc]; // Not needed, replaced above

//end mode section

}

- 05-15-2003Eibro
This seems like the easiest solution:

Code:`int mode(int* array, int size)`

{

int largestNum = 0, largestCount = 0;

int* upperLimit = std::max_element(&array[0], &array[size]);

int* lowerLimit = std::min_element(&array[0], &array[size]);

for (int i = *lowerLimit; i <= *upperLimit; ++i)

{

int currentCount = std::count(&array[0], &array[size], i);

if (currentCount > largestCount)

{

largestCount = currentCount;

largestNum = i;

}

}

return largestNum;

}