Computing & Information Systems    Department Search CIS Site      Tutorials

## Software Design Using C++

### Review of Functions and Parameters

First, we repeat the description of the problem: Write a reasonable test program for the C++ function shown below.

 `````` /* Given: NumGrade A numeric grade (whole number). Task: To find the corresponding letter grade. Return: This letter grade in the function name. */ char LetterGrade(int NumGrade) { if (NumGrade > 100) return 'I'; else if (NumGrade >= 90) return 'A'; else if (NumGrade >= 80) return 'B'; else if (NumGrade >= 70) return 'C'; else if (NumGrade >= 60) return 'D'; else if (NumGrade >= 0) return 'F'; else return 'I'; } ``````

We began with an outline that contained no code for the main function. The following shows one possible way to write some of the code, but may not be the best answer. See the link at the bottom for a better method.

 `````` /* Filename: decision.cpp Author: Br. David Carlson Date: December 20,2003 This is a test program that takes several numeric grades and converts them into letter grades. The numeric grades are hard-coded into the program. The numeric grades and corresponding letter grades are displayed on the screen. */ #include using namespace std; // Function prototypes: char LetterGrade(int NumGrade); int main(void) { cout << "Numeric grade " << 88 << " corresponds to the letter grade " << LetterGrade(88) << endl << endl; cout << "Numeric grade " << 65 << " corresponds to the letter grade " << LetterGrade(65) << endl << endl; cout << "Numeric grade " << 96 << " corresponds to the letter grade " << LetterGrade(96) << endl << endl; return 0; } `````` `````` /* Given: NumGrade A numeric grade (whole number). Task: To find the corresponding letter grade. Return: This letter grade in the function name. */ char LetterGrade(int NumGrade) { if (NumGrade > 100) return 'I'; else if (NumGrade >= 90) return 'A'; else if (NumGrade >= 80) return 'B'; else if (NumGrade >= 70) return 'C'; else if (NumGrade >= 60) return 'D'; else if (NumGrade >= 0) return 'F'; else return 'I'; } ``````

Note that the above method of coding the main function is awkward, especially if we want to test a longer list of numeric grades. We could package up the code that does one test as a function and just call this new function repeatedly. Try this yourself. Also think about what numbers should be tested to get a reasonable test program (sometimes called a driver program).

Click on NEXT to see one possible answer.

You can go back to the overall review listing: Review of Introductory Topics.

Back to the main page for Software Design Using C++

Author: Br. David Carlson with contributions by Br. Isidore Minerd
Last updated: January 15, 2013
Disclaimer