Skip to content

Instantly share code, notes, and snippets.

@dillmo
Created March 7, 2014 19:59
Show Gist options
  • Save dillmo/9418783 to your computer and use it in GitHub Desktop.
Save dillmo/9418783 to your computer and use it in GitHub Desktop.
Introduction to C++ Lab 4
/* Lab 4 Solution by Dillon Morse
* ------------------------------
* Generate four random number grades between 1 and 100. Then, drop the lowest
* grade and issue a letter grade, rounding up.
*/
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
int rnd;
int grade;
int scores[4];
int final_scores[3];
int i;
int min = 100;
int sum = 0;
string letter_grade;
bool reached_min;
srand(time(NULL));
for(i = 0; i < 4; i++) {
scores[i] = rand() % 99 + 1;
if(scores[i] < min) min = scores[i];
}
for(i = 0; i < 4; i++) {
if(scores[i] > min) {
// Makes it possible to record numbers to the proper point in the array
// after the minimum value is reached
if(reached_min) {
final_scores[i - 1] = scores[i];
} else {
final_scores[i] = scores[i];
}
} else {
reached_min = true;
}
}
for(i = 0; i < 3; i++) {
sum = sum + final_scores[i];
}
grade = static_cast<int>(sum / 3);
// Since the greater than operator is supplied by the previous if statement,
// I can get away without any or operators.
if(grade < 60) {
letter_grade = "F";
} else if(grade < 70) {
letter_grade = "D";
} else if(grade < 80) {
letter_grade = "C";
} else if(grade < 90) {
letter_grade = "B";
} else {
letter_grade = "A";
}
// Useful for debugging
// --------------------
// for(i = 0; i < 3; i++) {
// cout << final_scores[i] << '\n';
// }
cout << "Your grade is " << letter_grade << '\n';
// Necessary to avoid errors using some compilers
return 0;
// Necessary if you aren't using a CLI for output
// ----------------------------------------------
// system("PAUSE")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment