-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathA18modecalculation.cpp
92 lines (70 loc) · 1.66 KB
/
A18modecalculation.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Mode Calculation
// By Alina Corpora and Emily Dayanghirang
#include <iostream>
using namespace std;
// Function Prototypes
void getInput(int[], int);
void displayInput(int[], int);
int mode(int[], int);
int main()
{
const int SIZE = 7;
int userInt[SIZE],
displayMode;
// Get integers from user
getInput(userInt, SIZE);
// Display user’s integers
displayInput(userInt, SIZE);
// Display mode
displayMode = mode(userInt, SIZE);
cout << "The most recurring number is " << displayMode << "." << endl;
return 0;
}
void getInput(int userInt[], int size)
{
cout << "\nEnter " << size << " integers \n";
for (int i = 0; i < size; i++)
{
cout << "Enter the # for entry "
<< i + 1 << ": ";
cin >> userInt[i];
while(cin.fail())
{
if(cin.fail())
{
cin.clear();
cin.ignore(10000, '\n');
}
cout << "Please enter a valid integer: ";
cin >> userInt[i];
}
}
}
void displayInput(int userInt[], int size)
{
cout << "\nYour input: ";
for (int j = 0; j < size; j++)
{
cout << userInt[j] << " ";
}
cout << endl;
}
int mode(int userInt[], int size)
{
int maxMode = 0, number = 0;
for(int index = 0; index < size; index++)
{
int count = 1;
for (int check = index + 1; check < size; check++)
{
if(userInt[index] == userInt[check])
count ++;
}
if(maxMode < count)
{
maxMode = count;
number = userInt[index];
}
}
return number;
}