-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestScoresTwo.cpp
More file actions
110 lines (96 loc) · 2.63 KB
/
TestScoresTwo.cpp
File metadata and controls
110 lines (96 loc) · 2.63 KB
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
#include <iostream>
using namespace std;
short getStudents();
void getNamesAndScores(string*, float*, short);
void Sorter(string*, float*, short);
void Printer(string*, float*, short);
int main(){
short size = getStudents();
string* names = nullptr;
float* scores = nullptr;
if(size > 0){
names = new string[size];
scores = new float[size];
if(names && scores){
getNamesAndScores(names, scores, size);
Sorter(names, scores, size);
Printer(names, scores, size);
delete [] names;
names = nullptr;
delete [] scores;
scores = nullptr;
}
}
return 0;
}
/**
* @brief Get the Students object
*
* @return short
*/
short getStudents(){
short students;
cout << "\nHow many students are in the class: ";
cin >> students;
return students;
}
/**
* @brief Get the Names And Scores object
*
* @param names
* @param scores
* @param size
*/
void getNamesAndScores(string* names, float* scores, short size){
cout << "\nEnter: \n";
for(short index = 0; index < size; index++){
cin.ignore();
cout << "\nStudent " << (index + 1) << "'s name: ";
getline(cin, *(names + index));
do{
cout << *(names + index) << "'s score: ";
cin >> *(scores + index);
if(*(scores + index) < 0)
cout << "\nINVALID INPUTn\n\n";
}while(*(scores + index) < 0);
}
}
/**
* @brief Sorts the scores array in ascending order and makes modifications to the names array so that values coincide
*
* @param names
* @param scores
* @param size
*/
void Sorter(string* names, float* scores, short size){
short maxScore, maxIndex;
string MaxName;
for(short startScan = 0; startScan < size - 1; startScan++){
maxIndex = startScan;
maxScore = *(scores + startScan);
for(short index = startScan + 1; index < size; index++){
if(*(scores + index) > maxScore){
maxScore = *(scores + index);
maxIndex = index;
MaxName = *(names + index);
}
}
*(scores + maxIndex) = *(scores + startScan);
*(names + maxIndex) = *(names + startScan);
*(scores + startScan) = maxScore;
*(names + startScan) = MaxName;
}
}
/**
* @brief Prints the contents of arrays
*
* @param names
* @param scores
* @param size
*/
void Printer(string* names, float* scores, short size){
for(short index = 0; index < size; index++){
cout << "\n" << *(names + index) << " " << *(scores + index);
}
cout << "\n\n";
}