-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWILJ1A.cpp
75 lines (74 loc) · 2.02 KB
/
WILJ1A.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
#include <iostream>
#include <cstdlib>
#include <ctime>
void Mode(int[], int);
void read_array(int[], int);
void print_array(int[], int);
int main()
{
srand(time(0));
const int SIZE = 101;
int numbers[SIZE];
read_array(numbers, SIZE);
print_array(numbers, SIZE);
Mode(numbers, SIZE);
system("pause");
return 0;
}
void read_array(int numArray[], int SIZE)
{
for (int counter = 1; counter < SIZE; counter++)
{
numArray[counter] = (rand() % 30) + 1;
}
}
void print_array(int numArray[], int SIZE)
{
for (int counter = 1; counter < SIZE; counter++)
{
std::cout << numArray[counter] << " ";
if (counter%10 == 0)
std::cout << std::endl;
}
}
void Mode(int numArray[], int SIZE)
{
int possibleModes[31];
// setting modes elements to 0
for (int counter = 1; counter <= 30; counter++)
possibleModes[counter] = 0;
/*creating the list that says how many of each number there are, the index of
the number being the number itself and the value of the array at this index/number
being the amount of times said number appears in the array*/
for (int value = 1; value <= 30; value++)
for (int index = 1; index < SIZE; index++)
if (numArray[index] == value)
possibleModes[value]++;
//array to store the index of the modes
int modes[31];
//setting modesArray elements to 0
for (int counter = 0; counter <= 30; counter++)
modes[counter] = 0;
//setting the original element for the modes checking for loop
int mode = possibleModes[1];
//the modes checking for loop
for (int counter = 2; counter <= 30; counter++)
{
if (possibleModes[counter] > mode)
{
mode = possibleModes[counter];
}
}
for (int counter = 1; counter <= 30; counter++)
{
if (possibleModes[counter] == mode)
modes[counter] = 1;
}
/* Displays whether or not the number is a mode, the first number
being true/false and the second being the corresponding number
for (int counter = 1; counter <= 30; counter++)
{
std::cout << modes[counter] << " " << counter << std::endl;
}
*/
}