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
|
#include <iostream>
#include <iomanip>
#include <string>
const int NO_OF_STUDENTS = 5;
// function declarations prototypes
void getData( std::string first_names[], std::string last_names[], int scores[] );
void calculateGrade( char grades[], const int scores[] ); // make it const-correct
void printResult( const std::string first_names[], const std::string last_names[],
const int scores[], const char grades[] ); // make it const-correct
int main()
{
// define 4 parallel arrays
std::string studentFNames[NO_OF_STUDENTS];
std::string studentLNames[NO_OF_STUDENTS];
int testScores[NO_OF_STUDENTS] = {0} ;
char letterGrades[NO_OF_STUDENTS] = {0} ;
// call getData() to populate three of the four parallel arrays
//getData(studentLNames, studentFNames, testScores);
getData( studentFNames, studentLNames, testScores );
// call calculateGrade() to provide values for the fourth parallel array
calculateGrade( letterGrades, testScores );
// call printResult() to display report form the parralel arrays
printResult( studentFNames, studentLNames, testScores, letterGrades );
}
// function definition getData()
void getData( std::string first_names[], std::string last_names[], int scores[] )
{
// the follow arrays are used for test data (do not modify)
// string fNameTest[5] = {"Humpty", "Jack", "Mary", "Jack", "King"};
// string lNameTest[5] = {"Dumpty", "Horner", "Lamb", "Sprat", "Cole"};
// int scoresTest[5] = {59, 88, 100, 75, 60};
// do modify. (make it const-correct and avoid magic numbers.)
static const std::string fNameTest[NO_OF_STUDENTS] = {"Humpty", "Jack", "Mary", "Jack", "King"};
static const std::string lNameTest[NO_OF_STUDENTS] = {"Dumpty", "Horner", "Lamb", "Sprat", "Cole"};
static const int scoresTest[NO_OF_STUDENTS] = {59, 88, 100, 75, 60};
// use a suitable loop to populate the appropriate "empty" arrays
// with values from the three initialized test arrays
for( int index = 0; index < NO_OF_STUDENTS ; ++index )
{
first_names[index] = fNameTest[index] ;
last_names[index] = lNameTest[index] ;
scores[index] = scoresTest[index] ;
}
}
static char grade( int score ) // given a score, return corresponding grade
{
const int NGRADES = 5 ;
static const char gradeLetter[NGRADES] = { 'A', 'B', 'C', 'D', 'F' };
if( score > 89 ) return gradeLetter[0] ;
if( score > 79 ) return gradeLetter[1] ;
if( score > 69 ) return gradeLetter[2] ;
if( score > 59 ) return gradeLetter[3] ;
return gradeLetter[4] ;
}
// function definition for calculateGrade()
void calculateGrade( char grades[], const int scores[] )
{
for( int index = 0; index < NO_OF_STUDENTS ; ++index ) grades[index] = grade( scores[index] ) ;
}
static void printOneResult( std::string first_name, std::string last_name, int score, char grade )
{
using namespace std ;
cout << setw(15) << left << ( last_name + ", " + first_name )
<< setw(5) << right << score << ' ' << grade << '\n' ;
}
// function definition for sprintResults()
void printResult( const std::string first_names[], const std::string last_names[],
const int scores[], const char grades[] )
{
for( int index = 0; index < NO_OF_STUDENTS ; ++index )
printOneResult( first_names[index], last_names[index], scores[index], grades[index] ) ;
}
|