# Grading Case Study in C++

### By Nicholas Duchon, September 30, 2014

From Venit, Section 6.8, pp 332-342, in C++

// Date: Sep 29, 2014
// Author: Nicholas Duchon
// Purpose: show parallel arrays with strings, and string i/o
// Environment/language: C++
// Sample input and output after program

#include <stdio.h>  // for printf
#include <string>   // for string class
#include <iostream> // for cin and cout
using namespace std;

if (score >= 90) return 'A';
if (score >= 80) return 'B';
if (score >= 70) return 'C';
if (score >= 60) return 'D';
return 'F';

int initArrays (int m, string n [], int id [], double sc [], char g []) {
int count = 0;
while (count < m) {
cout << "Enter name id score [* to end]: ";
cin >> n[count];
if (n[count].compare ("*") == 0) break;
cin >> id[count] >> sc[count];
count ++;
} // end for each input line
return count;
} // end function initArrays

void printAll (string n [], int id [], double sc [], char g [], int m) {
printf ("\n%-20s%10s%6s%9s\n",
"Student Name", "ID number", "Score", "Grade");
for (int i = 0; i < m; i++)
printf ("%-20s%10d%6.1f%7c\n", n[i].c_str(), id[i], sc[i], g[i]);
// c_str() is used to convert string class kind of object to printf kind of string
} // end function printAll

double getAverage (double sc [], int m) {
if (m <= 0) return 0; // make sure we don't divide by 0
double sum = 0;
for (int i = 0; i < m; i++)
sum = sum + sc[i];
return sum/m;
} // end getAverage

int main () {
const int max = 50; // needs to be a constant to use in array declarations
int used;           // the number of student entries actually active
string names  [max];
int    ids    [max];
double scores [max];

used = initArrays (max, names, ids, scores, grades);
printAll (names, ids, scores, grades, used);
cout << "Average is: " << getAverage (scores, used) << endl;
cout << "Bye" << endl;
// system ("Pause"); // Visual C++
} // end main

```/* Sample input - * on last line indicates end of input
fred 44 94
jane 234 87
harry 43243 60
john 57483 50
elizabeth 47732 78*
*/
```

## Input and Output:

Enter name id score [* to end]: fred 44 94
Enter name id score [* to end]: jane 234 87
Enter name id score [* to end]: harry 43234 60
Enter name id score [* to end]: john 57483 50
Enter name id score [* to end]: elizabeth 47732 78
Enter name id score [* to end]: *

Student Name         ID number Score    Grade
fred                        44  94.0      A
jane                       234  87.0      B
harry                    43234  60.0      D
john                     57483  50.0      F
elizabeth                47732  78.0      C
Average is: 73.8
Bye