Finding Mode in an Array

I seem to keep running into problems trying to figure out how to get the mode... Any help please!!

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
    #include <ctime>
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;


int Number_Generat();
int sum(int temp_Array[50]);
int mean(int temp_Array[50]);
int largest(int temp_Array[50]);
int smallest(int temp_Array[50]);


int main() {


srand (time(NULL));

int my_Array[50];
int i;

for(i=0;i<50;i++)
{
my_Array[i]= Number_Generat();
cout<<my_Array[i]<<" ";

}


int total = sum(my_Array);
int nmean = mean(my_Array);
float nmode = mode(my_Array);
int large = largest(my_Array);
int small = smallest(my_Array);



cout<<"\nSum is: "<<total<<endl; 
cout<<"Mean is: "<<nmean<<endl;
cout<<"Mode is: "<<nmode<<endl;
cout<<"Largest is: "<<large<<endl;
cout<<"Smallest is: "<<small<<endl;


return 0;
}


int Number_Generat()
{
int num = 0;
num = rand()%26;
return num;
}


int sum(int temp_Array[50])
{
int total=0;
for(int i=0; i<50;i++)
{
total = total+temp_Array[i];
}
return total;
}


int mean(int temp_Array[50])
{
int mean = sum(temp_Array)/50;
return mean;
}

int mode(int temp_Array[50])
{


}

int largest(int temp_Array[50])
{
int tmp=temp_Array[0];
for(int i =1;i<50;i++)
{
if(temp_Array[i] >tmp)
tmp = temp_Array[i];
}

return tmp;
}


int smallest(int temp_Array[50])
{
int tmp=temp_Array[0];
for(int i =1;i<50;i++)
{
if(temp_Array[i] < tmp)
tmp = temp_Array[i];
}

return tmp;
}
closed account (j3Rz8vqX)
The mode is the number that is repeated more often than any other

http://www.purplemath.com/modules/meanmode.htm

Loop through the array.
Have an array to manage unique values.
Have another array to manage how often they were present.

If value present in the array, managing the number of counted, is 1 or less, and original array was greater than 1 then there was no mode? (Not a disciple of math here, but every number would've been unique if that happened).

Have fun.
you can try to sort values in the array and then go through the array counting how many times the same value appears...
1. sort
1 2 8 2 8 3 1 1 8 8 ---> 1 1 1 2 2 3 8 8 8 8

2. counting

1 1 1 --- > 3x *(this could be the mode)
2 2 --- > 2x (2 < 3, so this is not the mode)
3 --- > 1x (1 < 3, so this is not the mode)
8 8 8 8 -> 4x *(4 > 3, we found the mode)

* you can make a check if the number of how many times the same value occurs is not bigger then the number of values left
Last edited on
Do you have like an example of some code I could see... Im sorry but Im really having a lot of trouble with this..
Thanks...
Hi @cluterbug1,
this could be
a start

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
//Mode.cpp
//##

#include <iostream>
using std::cout;
using std::endl;

bool isNotRepeated(int const key,int array[],int const SIZE,int position);
void printArray(int array[],int const SIZE);


int main(){

	int const SIZE=10;
	int setOfNumbers[SIZE]={0,5,3,0,5,6,7,7,8,0};
	int ocurrences[SIZE]={}; //all of them start at 0 -zero-


	//finding ocurrences
	for(int i=0;i<SIZE;i++){
		for(int j=0;j<SIZE;j++){
			if(setOfNumbers[i]==setOfNumbers[j])
				ocurrences[i]++;
		}//end inner for
	}//end outer for

	//print array
	cout<<"\nSet of numbers:\n";
	printArray(setOfNumbers,SIZE);

	cout<<endl;

	cout<<"Ocurrences\n"<<endl;
	for(int i=0;i<SIZE;i++){
		if(ocurrences[i]>1&&isNotRepeated(setOfNumbers[i],setOfNumbers,SIZE,i))
		cout<<"Number "<<setOfNumbers[i]<<" - "<<ocurrences[i]<<endl; 
	}//end for
	

return 0; //indicates success
}//end of main

bool isNotRepeated(int const key,int array[],int const SIZE,int position){

	int counter=0;
	for(int i=0;i<position;i++){
		if(key==array[i])
			++counter;
	}//end for
	if(counter>=1)
		return false;

return true;
}//end function isNotRepeated

void printArray(int array[],int const SIZE){
	for(int i=0;i<SIZE;i++){
		cout<<array[i]<<((i+1)%5==0?'\n':' ');
	}//end for

}//end function printArray 



Set of numbers:
0 5 3 0 5
6 7 7 8 0

Ocurrences

Number 0 - 3
Number 5 - 2
Number 7 - 2
Topic archived. No new replies allowed.