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
|
#include "stdafx.h"
#include <iostream>
using namespace std;
void mode(int *, int);
void main()
{
const int NUMBERS = 15;
int list[NUMBERS] = {99,73,56,14,28,42,93,64,51,26,56,16,38,81,98};
mode(list, NUMBERS);
}
void mode(int *arr, int SIZE)
{
int countmode = 0;
int finalcount = 0;
int mode = 0;
for(int count = 0; count < SIZE; count++)
{
countmode = 0;
for(int i = 0; i < (SIZE); i++)
{
if(arr[count] == arr[i])
{
countmode++;
}
}
countmode = countmode - 1;
//because the arr[count] will be compared against it's own value once every
//iteration of the outer for loop which doesn't count toward the modecount
if(countmode > finalcount)
{
finalcount = countmode;
mode = arr[count];
}
}
if(mode > 0)
cout << "Final Mode " << mode << endl;
else
cout << "There is no mode." << endl;
}
|