-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3_0_findint_mode.cpp
70 lines (55 loc) · 1.1 KB
/
3_0_findint_mode.cpp
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
#include <iostream>
#include <cstring>
#define D_ARRAY_SIZE 21
#define D_RANGE_SIZE 10
using namespace std;
/*
Find mode, using arrays only.
*/
int main()
{
int data[D_ARRAY_SIZE] = {1,2,3,4,5,6,7,8,9,10,1,2,3,3,4,5,6,7,8,9,10};
int histogram[D_RANGE_SIZE] = {0};
int mode = data[0];
bool hasNoMode = true;
/*
for(int i = 0; i < D_ARRAY_SIZE; i++)
{
histogram[data[i]-1]++;
}
for(int i = 0; i < D_RANGE_SIZE; i++)
{
if (histogram[i] > histogram[mode])
{
mode = i;
}
}
mode++;
*/
for(int i = 0; i < D_ARRAY_SIZE; i++)
{
histogram[data[i]-1]++;
if (histogram[mode-1] < histogram[data[i]-1])
{
mode = data[i];
}
}
for(int i = 0; i < D_RANGE_SIZE; i++)
{
if (histogram[mode-1] > histogram[i])
{
hasNoMode = false;
break;
}
}
if (hasNoMode)
{
cout << "The set has no mode" << endl;
}
else
{
cout << "The mode is " << mode << endl;
}
cin.get();
return 0;
}