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
|
#include <iostream>
using namespace std;
//******************************************************************
// Arrays for the scores to be stored in by grade category.
// Not strictly necessary for this assignment, but adds flexibility for code reuse.
// Plus it was good practice for me.
// Array elements are initialized with -1 to identify "empty" elements when iterating through the array.
//******************************************************************
int A[30] = {-1};
int B[30] = {-1};
int C[30] = {-1};
int D[30] = {-1};
int F[30] = {-1};
// void input grades ()
// Purpose: User IO for inputing each students score then categorizing them by grade.
// Output: (to arrays and function 'computePercentages') scores are stored in global arrays by grade.
// & numbers of students, total and by category, are sent to function 'computePercentages'.
// Precondition: none.
// Postcondition: Data has been output to global arrays and as arguments to function.
void inputGrades(){
int score = 0; // user IO for each student's score
int count_A = 0; // number of A students
int count_B = 0; // number of B students
int count_C = 0; // number of C students
int count_D = 0; // number of D students
int count_F = 0; // number of F students
int count_total = 0; // number of total students
while (true);{ // Assumes there is at least one entry. Uses a break condition in lines 45-46 to exit.
cout << "Please enter the score for student number" << count_total << " (or enter -1 to stop): " << endl;
cin >> score;
if (score == -1)
break; // User IO break condition
else
++count_total;
while (score < -1 || score > 100);{
cout << "Student score must be between 0 and 100, please re-enter score or enter -1 to stop: ";
cin >> score;
} // while (score < -1 || score > 100)
if (score >= 90){
A[count_A] = score;
++count_A;
} // if (score >= 90)
else if (score >= 80){
B[count_B] = score;
++count_B;
} // else if (score >= 80)
else if (score >= 70){
C[count_C] = score;
++count_C;
} // else if (score >= 70)
else if (score >= 60){
D[count_D] = score;
++count_D;
} // else if (score >= 60)
else if (score >= 0){
F[count_F] = score;
++count_F;
} // else if (score >= 0)
} // while (true)
cout << "Total number of scores entered: " << count_total << endl;
void computePercentages (count_A, count_B, count_C, count_D, count_F, count_total);
} // inputGrades()
//******************************************************************
// void computePercentages (a, b, c, d, f, count_total)
// Purpose: Calculates and outputs percentage of each grade category with respect to the total.
// Output: (to standard out) Number and percentage of students receiving each grade.
// Precondition: number of students by grade type and total have been computed.
// Postcondition: Data has been output to standard out.
//******************************************************************
void computePercentages (int a, int b, int c, int d, int f, int count_total){ // arguments are integers from inputGrades()
double total = count_total; // changed to double for accuracy in division
cout << "Grade A: " << a << "students, " << a / total << "%" << endl;
cout << "Grade B: " << b << "students, " << b / total << "%" << endl;
cout << "Grade C: " << c << "students, " << c / total << "%" << endl;
cout << "Grade D: " << d << "students, " << d / total << "%" << endl;
cout << "Grade F: " << f << "students, " << f / total << "%" << endl;
} // computePercentages (a, b, c, d, f, count_total)
int main (){ // int main exists only to call the first function of the program.
inputGrades();
} // main ()
|