-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathBubbleSorting.cpp
More file actions
199 lines (176 loc) · 6.24 KB
/
Copy pathBubbleSorting.cpp
File metadata and controls
199 lines (176 loc) · 6.24 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
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
// This program prompts the user for 5 test scores as integers greater than or equal to 0 and less than or equal to 100.
// It ensures that the user entries are valid and stores the 5 test scores in an array.
// It uses the bubble sort to sort the scores in ascending order and calculates the letter grade for each test.
// It displays the letter grade of each scsore as well as the overall average of the 5 test scores.
//
// Grade scale is: Score Letter Grade
// >= 90 A
// >= 80 and < 90 B
// >= 70 and < 80 C
// >= 60 and < 70 D
// >= 0 and < 60 F
#include <iostream> // Must be included in any program that uses the cout object
#include <iomanip> // Library for manipulating output
#include <string> // Required for the string class
#include <stdlib.h> // atoi and atof
using namespace std;
// Function Prototypes
double calcAverage(int testScore[], int& sum, double& average);
void validateUserInput(string& userEntry);
void determineGrade(int testScore[], char grade[]);
void bubbleSort (int testScore[]);
void displayTestScores(int testScore[], char grade[], int sum, double average);
int main()
{
// declare Array to hold test scores
int testScore[5];
// declare Array to hold grades
char grade[5];
// Initializations and declarations
string userEntry;
int i = 0;
double average;
int sum;
while(i < 5)
{
// Lets the user input each test score one by one
cout << "Enter score " << (i + 1) << ": " << endl;
getline(cin, userEntry);
validateUserInput(userEntry); // Calls validateUserInput function
int userEntryInt = atoi(userEntry.c_str()); // Converts userEntry string to userEntryInt integer
testScore[i] = userEntryInt; // Scores each score in an array called testScore
i++;
}
bubbleSort(testScore); // Calls bubbleSort function
calcAverage(testScore, sum, average); // Calls calcAverage function
determineGrade(testScore, grade); // Calls determineGrade function
displayTestScores(testScore, grade, sum, average); // Calls displayTestScores function
system("pause");
return 0;
} // End of main
// ********************************************************
// The calcAverage function computes the average of the *
// five test scores and takes an array of type int as a *
// parameter and returns the average. *
// ********************************************************
double calcAverage(int testScore[], int& sum, double& average)
{
for(int i = 0; i < 5; i++)
{
// Adds all the test scores
sum = sum + testScore[i];
}
average = sum / 5.0;
// cout << "The average is ";
// cout << setprecision(2) << fixed;
// cout << average << endl;
return average;
} // end of calcAverage
// ********************************************************
// The validateUserInput function ensures user entries *
// are integers greater than or equal to zero and less *
// than or equal to 100. *
// ********************************************************
void validateUserInput(string& userEntry)
{
int counter = 0;
int userEntryLength = userEntry.length();
while(counter < userEntryLength || userEntryLength == 0)
{
int userEntryInt = atoi(userEntry.c_str());
if(!isdigit(userEntry[counter]))
{
cout << "That is not an integer X such that : 0 <= X <= 100, try again." << endl;
getline(cin, userEntry);
userEntryLength = userEntry.length();
counter = 0;
}
else if(userEntryInt < 0 || userEntryInt > 100)
{
cout << "That is not an integer X such that : 0 <= X <= 100, try again." << endl;
getline(cin, userEntry);
userEntryLength = userEntry.length();
counter = 0;
}
else
{
counter++;
}
}
} // end of validateUserInput function
// ********************************************************
// The determineGrade function computes the average *
// of the five test scores and takes an int array with *
// the test scores and a char array to store the *
// corresponding letter grades. *
// ********************************************************
void determineGrade (int testScore[], char grade[])
{
for(int i = 0; i < 5; i++)
{
// When the test score is 90 or above, the letter grade is an A
if(testScore[i] >= 90)
{
grade[i] = 'A';
}
// When the test score is 80 or above but less than 90, the letter grade is a B
if(testScore[i] >= 80 && testScore[i] < 90)
{
grade[i] = 'B';
}
// when the test score is 70 or above but less than 80, the letter grade is a C
if(testScore[i] >= 70 && testScore[i] < 80)
{
grade[i] = 'C';
}
// When the test score is 60 or above, but less than 70, the letter grade is a D
if(testScore[i] >= 60 && testScore[i] < 70)
{
grade[i] = 'D';
}
// When the test score is less 60, the letter grade is a D
if(testScore[i] < 60)
{
grade[i] = 'F';
}
}
} // End of determineGraade function
// ********************************************************
// The bubbleSort function sorts the values (test scores) *
// stored in the array in ascending order. *
// ********************************************************
void bubbleSort (int testScore[])
{
int temp = 0;
for(int i = 0; i < 5; i++)
{
for(int j = 1; j < 5; j++)
{
// Swaps numbers to display them in ascending order
if(testScore[j-1] > testScore[j])
{
temp = testScore[j-1];
testScore[j-1] = testScore[j];
testScore[j] = temp;
}
}
}
} // End of bubbleSort
// ********************************************************
// The displayTestScores function displays each test *
// score and its corresponding letter as well as the *
// overall average. *
// ********************************************************
void displayTestScores(int testScore[], char grade[], int sum, double average)
{
// Displays scores and letter grades
cout << "Score LetterGrade" << endl;
for(int i = 0; i < 5; i++)
{
// setw(4) gives 4 spaces between test scores and grades
cout << setw(4) << testScore[i] << setw(4) << grade[i] << endl;
}
cout << "The average is ";
cout << setprecision(2) << fixed; // displays two digits after decimal point
cout << average << endl;
} // End of displayTestScores function