CIS Logo SVC Logo

   Computing & Information Systems


Schoology Facebook        Search CIS Site      Tutorials

Software Design Using C++

Review of Decision Statements

Answering the Question

First, we repeat the description of the problem: Write a C++ function that is given an integer grade (such as 90, 84, 66) and sends back the corresponding letter grade (such as A, B, D) in the function name. Return the letter I for any invalid grade number.

You selected Answer C, shown below. This is incorrect. Consider, for example, the value of 77 for NumGrade. The section of code below first assigns the value 'C' to Ch, then assigns 'D' to Ch, then assigns 'F' to Ch. Thus, when Ch is returned, it is the 'F' that is returned. In fact, this function always returns an 'F', except in the case of negative values (for which it returns an 'I'). Go back and see if you can find the correct answer.

/* 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)
   char Ch;

   if (NumGrade > 100)
      Ch = 'I';
   if (NumGrade >= 90)
      Ch = 'A';
   if (NumGrade >= 80)
      Ch = 'B';
   if (NumGrade >= 70)
      Ch = 'C';
   if (NumGrade >= 60)
      Ch = 'D';
   if (NumGrade >= 0)
      Ch = 'F';
   if (NumGrade < 0)
      Ch = 'I';

   return Ch;

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