-
Notifications
You must be signed in to change notification settings - Fork 0
/
WILJ1A
166 lines (91 loc) · 3.24 KB
/
WILJ1A
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
Script started on Mon Feb 24 12:01:11 2014
admiral% cat WILJ1A.cpp
#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;
}
*/
}
admiral% g++ wilj WILJ1A.cpp
admiral% a.out IWL^?^? WILJ1A.cpp
11 4 2 14 14 8 22 15 29 11
14 2 10 12 26 25 16 2 5 19
5 14 10 2 12 12 30 10 14 7
25 9 9 29 18 28 21 2 18 20
22 7 9 25 28 25 6 5 28 29
27 6 17 22 30 5 5 25 1 23
30 20 16 21 20 11 23 26 15 29
26 16 4 28 7 24 3 22 22 9
13 10 27 27 5 26 10 30 11 10
30 18 11 1 7 14 21 19 18 17
sh: pause: not found
admiral% script ^D
script done on Mon Feb 24 12:01:56 2014