-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathdynamic.cpp
233 lines (176 loc) · 7.79 KB
/
dynamic.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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
//*****************************************************************************************************
// Cricket Matches Survey Analysis
//
// This program prompts the user to input data on cricket matches played by college students,
// dynamically allocates memory for the data, and performs various operations such as displaying
// the data, finding the student who played the most matches, calculating the mean number of
// matches played, and sorting the data in ascending order by student names.
//
//*****************************************************************************************************
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
//*****************************************************************************************************
void input(string names[], int matches[], int size);
void display(const string names[], const int matches[], int size);
void displayMostMatches(const string names[], const int matches[], int size);
double mean(const int matches[], int num);
void sortStudents(string names[], int matches[], int size);
//*****************************************************************************************************
int main() {
int size; // size of the array to be dynamically allocated
int *matches = nullptr;
double average;
string *names = nullptr; // pointer to dynamically allocated array and set to nullptr
cout << "-------------------------------------------------\n"
<< " Cricket Matches College Students Play in a Year\n"
<< "-------------------------------------------------" << endl;
do {
cout << "How many students were surveyed? (1-5000): ";
cin >> size;
if (size <= 0 || size > 5000)
cerr << "\nError: Invalid Entry\n";
cin.ignore();
} while (size <= 0 || size > 5000);
names = new string[size]; // dynamically allocate memory
matches = new int[size];
input(names, matches, size);
cout << "\n-------------------------------------------------\n"
<< "\tStudent Names & Matches Played\n"
<< "-------------------------------------------------" << endl;
display(names, matches, size);
cout << "\n-------------------------------------------------\n"
<< "\tStudent Who Played Most Matches\n"
<< "-------------------------------------------------" << endl;
displayMostMatches(names, matches, size);
cout << "\n-------------------------------------------------\n"
<< "\tAverage Matches Played for Students\n"
<< "-------------------------------------------------" << endl;
average = mean(matches, size);
cout << "Average: " << average << endl;
cout << "\n-------------------------------------------------\n"
<< "Student Names & Matches Played ~ Ascending Order\n"
<< "-------------------------------------------------" << endl;
sortStudents(names, matches, size);
display(names, matches, size);
delete[] names; // deallocate memory
names = nullptr; // set pointer to nullptr to prevent dangling pointers
delete[] matches;
matches = nullptr;
return 0;
}
//*****************************************************************************************************
void input(string names[], int matches[], int size) {
for (int i = 0; i < size; ++i) {
cout << "\nEnter name of student " << i + 1 << ": ";
getline(cin, names[i]);
while (true) {
cout << "Enter number of matches for " << names[i] << ": ";
cin >> matches[i];
cin.ignore();
if (matches[i] >= 0)
break;
else
cerr << "\nError: Invalid Entry\n";
}
}
}
//*****************************************************************************************************
void display(const string names[], const int matches[], int size) {
for (int i = 0; i < size; ++i)
cout << i + 1 << " " << setfill('.') << setw(30) << left << names[i]
<< " Matches: " << matches[i] << endl;
}
//*****************************************************************************************************
void displayMostMatches(const string names[], const int matches[], int size) {
int max = matches[0];
int temp = 0;
for (int i = 0; i < size; ++i) {
if (matches[i] > max) {
max = matches[i];
temp = i;
}
}
cout << temp + 1 << " " << setfill('.') << setw(30) << left << names[temp]
<< " Matches: " << matches[temp] << endl;
}
//*****************************************************************************************************
double mean(const int matches[], int size) {
double sum = 0;
for (int i = 0; i < size; ++i)
sum += matches[i];
return (sum / size);
}
//*****************************************************************************************************
// selection sort algorithm (time complexity: O(n^2)
void sortStudents(string names[], int matches[], int size) {
int minIndex,
minValueMatches;
string minValueNames;
for (int startScan = 0; startScan < (size - 1); ++startScan) {
minIndex = startScan;
minValueNames = names[minIndex];
minValueMatches = matches[minIndex];
for (int index = startScan + 1; index < size; ++index) {
if (names[index] < minValueNames) {
minValueNames = names[index];
minValueMatches = matches[index];
minIndex = index;
}
}
names[minIndex] = names[startScan];
names[startScan] = minValueNames;
matches[minIndex] = matches[startScan];
matches[startScan] = minValueMatches;
}
}
//*****************************************************************************************************
/*
-------------------------------------------------
Cricket Matches College Students Play in a Year
-------------------------------------------------
How many students were surveyed? (1-5000): 0
Error: Invalid Entry
How many students were surveyed? (1-5000): -1
Error: Invalid Entry
How many students were surveyed? (1-5000): 10000
Error: Invalid Entry
How many students were surveyed? (1-5000): 5
Enter name of student 1: Ragland, Nicholas
Enter number of matches for Ragland, Nicholas: -1
Error: Invalid Entry
Enter number of matches for Ragland, Nicholas: 23
Enter name of student 2: Smith, John
Enter number of matches for Smith, John: 28
Enter name of student 3: Zhang, Xiu Ying
Enter number of matches for Zhang, Xiu Ying: 21
Enter name of student 4: Evans, Olivia
Enter number of matches for Evans, Olivia: 30
Enter name of student 5: Song, Mona
Enter number of matches for Song, Mona: 26
-------------------------------------------------
Student Names & Matches Played
-------------------------------------------------
1 Ragland, Nicholas............. Matches: 23
2 Smith, John................... Matches: 28
3 Zhang, Xiu Ying............... Matches: 21
4 Evans, Olivia................. Matches: 30
5 Song, Mona.................... Matches: 26
-------------------------------------------------
Student Who Played Most Matches
-------------------------------------------------
4 Evans, Olivia................. Matches: 30
-------------------------------------------------
Average Matches Played for Students
-------------------------------------------------
Average: 25.6
-------------------------------------------------
Student Names & Matches Played ~ Ascending Order
-------------------------------------------------
1 Evans, Olivia................. Matches: 30
2 Ragland, Nicholas............. Matches: 23
3 Smith, John................... Matches: 28
4 Song, Mona.................... Matches: 26
5 Zhang, Xiu Ying............... Matches: 21
*/