CIS Logo SVC Logo

   Computing & Information Systems
   Department

 

Schoology Facebook        Search CIS Site      Tutorials

Software Design Using C++



Review of Functions and Parameters



Answering the Question


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 already had a simple test program but decided that we should package up the code to run one test as a separate function. This is shown below. Note, too, the various categories of test numbers used.


/* 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 <iostream>
using namespace std;


// Function prototypes:
char LetterGrade(int NumGrade);
void DisplayGrade(int NumGrade);


int main(void)
   {
   // Try some ordinary numeric grades:
   DisplayGrade(88);
   DisplayGrade(99);
   DisplayGrade(63);
   DisplayGrade(75);
   DisplayGrade(50);

   // Try borderline numeric grades:
   DisplayGrade(89);
   DisplayGrade(90);
   DisplayGrade(59);
   DisplayGrade(60);
   DisplayGrade(69);
   DisplayGrade(70);
   DisplayGrade(79);
   DisplayGrade(80);
   DisplayGrade(100);
   DisplayGrade(0);

   // Try some invalid grades:
   DisplayGrade(101);
   DisplayGrade(200);
   DisplayGrade(-1);

   return 0;
   }

/* Given:  NumGrade   A numeric grade (whole number).
   Task:   To find the corresponding letter grade and print both
           NumGrade and this corresponding letter grade.
   Return: Nothing.
*/
void DisplayGrade(int NumGrade)
   {
   cout << "Numeric grade " << NumGrade << " corresponds to the letter grade "
      << LetterGrade(NumGrade) << endl << endl;
   }

/* 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';
   }

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