CFORBEGINNERS.COM

C++ Algorithms

C Algorithms

Algorithm To Find The Mode

Here is program coded in C++ that demonstrates how to find the mode in a given data set using a particular method. This program will also find multiple modes if there are more than one. Also this tutorial is very detailed in order to be beginner friendly. If you are having trouble understanding certain techniques, break down each line very slowly and ask yourself "why is that there," this always helps.

// Finding the mode or modes in a data set
#include <iostream>
#include <stdlib.h>
using namespace std;
#define N 100
void mode (double x[],int n);//prototype for mode function
int main (void)
{
double x[N];
int n=0,i=0;
cout<<"Enter in as many as 100 values, when finished enter in -99"<<endl;
while(1)
{
n++;
cin>>x[i];
if (x[i]==-99)
{
break;
}
i++;
}
mode(x,n);//send the values of x[] and n to mode function
system ("PAUSE");
return (0);
}
//function defintion
void mode (double x[],int n)
{
int y[N]={0};//Sets all arrays equal to 0
int i,j,k,m,cnt,max=0,no_mode=0,mode_cnt=0;
double num;
for(k=0; k<n; k++)//Loop to count an array from left to right
{
cnt=0;
num=x[k];//Num will equal the value of array x[k]
for(i=k; i<n; i++)//Nested loop to search for a value equal to x[k]
{
if(num==x[i])
cnt++;//if a number is found that is equal to x[k] count will go up by one
}
y[k]=cnt;//The array y[k] is initialized the value of whatever count is after the nested loop
if(cnt>=2)//If cnt is greater or equal to two then there must be atleast one mode, so no_mode goes up by one
{
no_mode++;
}
}
if(no_mode==0)//after the for loops have excuted and still no_mode hasn't been incremented, there mustn't be a mode
{
//Print there in no mode and return control to main
cout<<"This data set has no modes\n"<<endl;
return;
}
for(j=0; j<n; j++)
//A loop to find the highest number in the array
{
if(y[j]>max)
max=y[j];
}
for(m=0; m<n; m++)//This loop finds how many modes there are in the data set
{
//If the max is equal to y[m] then that is a mode and mode_cnt is incremeted by one
if(max==y[m])
mode_cnt++;
}
cout<<"This data set has "<<mode_cnt<<" mode(s)"<<endl;//Prints out how many modes there are
for(m=0; m<n; m++)
{
if(max==y[m])//If max is equal to y[m] then the same sub set of array x[] is the actual mode
{
cout<<"The value "<<x[m]<<" appeared "<<y[m]<<" times in the data set\n"<<endl;
}
}
return;
}

Here is the only part of main, the rest of the program is written inside of a function. We first start by declaring an array 'x' that will hold as many 100 values and also two integers; 'n' and 'i'. The integer 'n' is immediately initialized to 0 to avoid garbage because 'n' will be a counting array. 'n' will be incremented by 1 each time the user enters in another value. Remember there is no fixed number of times a user is supposed to scan in a number. The user can input 5, 10 , 38 or any number of values in the array so 'n' will keep track on exactly how many. 'i' is immediately intialized to 0 once again to avoid garbage. The user will input values into array 'x' but if the value of array 'x' equals -99 the loop will break out and send the values of array 'x' and how many values there are 'n' to the function mode. Also notice how 'i' is incremented after the if statement, if it was incremented before "cin>>x[i];" the [i] would have a different value from the [i] in "x[i]==-99" because 'i' would have already been incremented.

Step 2

int y[N]={0};//Sets all arrays equal to 0 to avoid garbage
int i,j,k,m,cnt,max=0,no_mode=0,mode_cnt=0;
double num;
for(k=0; k<n; k++)//Loop to count an array from left to right
{
cnt=0;
num=x[k];//Num will equal the value of array x[k]
for(i=k; i<n; i++)//Nested loop to search for a value equal to x[k]
{
if(num==x[i])
cnt++;//if a number is found that is equal to x[k] count will go up by one
}
y[k]=cnt;//The array y[k] is initialized the value of whatever count is after the nested loop
if(cnt>=2)//If cnt is greater or equal to two then there must be atleast one mode, so no_mode goes up by one
{
no_mode++;
}
}

The first thing to notice is how another array 'y' is intialized an all values are set to 0, this will be explained in a second. This method uses a nested for loop. (a for loop within a for loop) A variable 'k' is used as the control variable in the first for loop. The first loop will continue until 'n' equals 'k', remember the variable 'n' was the one that was keeping track on how many value are in array 'x'. A variable 'cnt' is initialized to 0 because it will count how many times a given number is repeated in the array 'x'. A variable called 'num' is set equal to x[k]. In the case of the first round of the loop num=x[0], the very first array value. Now say that x[0] is equal to 5. The program then jumps into the second for loop using a control variable 'i'. It then checks to see if 'num' (which equals 5 right now) is equal to x[0] which if you remember is 5, so 'cnt' is incremented by one. The second for loop then starts over. 'num' which still equals 5 is checked if it is equal to x[1] if it is then 'cnt' will be incremented again if not the 'cnt' will stay where it is. Once the second for loop has check all the values in array 'x' it breaks out and the program jumps back into the first loops. y[k] which right now is y[0] is set equal to 'cnt'. So say the second loop found that 5 appeared 3 times in array 'x' then 'cnt' would equal 3 which means that y[0] is equal to 3. Because of the nature of this program after each nested loop 'cnt' will always atleast equal 1 so the condition then checks to see if 'cnt' is greater than or equal to 2. This means a number occured atlease twice and there is atlease 1 mode so the variable 'no_mode' is incremented. In this program 'no_mode' is a method that is used to check for a data set with no modes. The first for loop then begins again, 'cnt' is once again intialized to 0 and the process starts over.

Step 3

if(no_mode==0)//after the for loops have excuted and still mode hasn't been incremented then there mustn't be a mode
{
//Print there in no mode and return control to main
printf("This data set has no modes\n\n");
return;
}
for(j=0; j<n; j++)
{//A loop to find the highest number in the array y[]
if(y[j]>max)
max=y[j];
}

In the beginning of the function mode 'no_mode' was intialized to 0. So if 'no_mode' was never incremented because 'cnt' never was greater than or equal to 2 than 'no_mode' will still equal 0. If this is true than there mustn't be a mode, the program simply prints that there isn't a mode and returns control to main. If not then the program is then put into another for loop where variable 'j' is the control variable. This loop does one thing only search for the highest value in the array 'y'. Remember each value of array 'y' is holding the value of 'cnt' after every nested loop. The array 'y' with the highest number is how many times the mode occured. A variable named 'max' will hold the highest number in array 'y' therefore max will hold the number of times the mode occurred. The algorithm to search for the maximum number in a data set is explained in more detail here.

Step 4

for(m=0; m<n; m++)//This loop finds how many modes there are in the data set
{
//If the max is equal to y[m] then that is a mode and mode_cnt is incremeted by one
if(max==y[m])
mode_cnt++;
}
printf("This data set has %i mode(s)\n",mode_cnt);//Prints out how many modes there are
for(m=0; m<n; m++)
{
if(max==y[m])//If max is equal to y[m] then the same sub set of array x[] is the actual mode
{
printf("The value %.1f appeared %i times in the data set\n\n",x[m],y[m]);
}
}
return;

Remember that there is a possibility that there could be more than 1 mode. The next for loop finds how many modes there are using a variable 'm' as the control variable. The variable 'max' now holds the most times a mode or modes occured. If the mode occured 3 times 'max' will equal 3 so everytime the array y[m] equals 3 then that is a mode and 'mode_cnt' goes up by one. The printf statement then prints out how many modes there are. The for loop is the same as the previous for loop only this time it prints the results to the screen only if max==y[m]. Remember if 'max' equals 'y[m]' then that is a mode and how many times it appeared. Since the array 'x' and array 'y' were both being used with the control variable 'k' in the first for loop of the program, the highest values of y[m] hold how many times the mode occurred and at the same instance x[m] holds the actual mode! Happy coding!

Step 1

Ok so lets walk through a hypothetical situation. Say a user enters 5 values...5,3,7,5,2. The array 'x' will have these values

x[0]

5

x[1]

3

x[2]

7

x[3]

5

x[4]

2

Step 2

The first loop executes 5 times (since there are 5 values) each time the array 'y' was intialized what 'cnt' was. Remember 'cnt' keeps track of how many times a number appeared. The very first value was 5 and it appeared twice so after the first loop executed once 'cnt' was equal to two and initialized y[0] to 2. The next value was 3 and only appeared once so 'cnt' was equal to 1 and y[1] was initialized to 1..etc. If 'cnt' isn't greater than or equal to 2 than there isn't a mode. After the loops have finshed the array 'y' will look like this.

y[0]

2

y[1]

1

y[2]

1

y[3]

1

y[4]

1

Step 3

If 'cnt' was never greater than or equal to 2 than 'no_mode' was never incremented so there isn't a mode and the program ends. We can easily see that y[0] holds the highest value but the program doesn't know that yet. This is the part where the algorithm to find the highest value in array 'y' is used. Once the program finds the highest value it intializes 'max' to that number, in this case 'max' will equal 2. The program now knows that 2 is the highest value in array 'y' but it doesn't yet know how many times 2 occurred.

Step 4

Now that the programs knows 'max' is 2 it will search the same array 'y' to see how many times that the value 2 occurred. In other words it will find out how many modes there are each time a mode is found 'mode_cnt' is incremented. In the end of the program all the informations is printed but only when 'max' (which is 2) equals 'y[m]' since this is the index number when the mode occurred.