Test Scores and Letter Grade

Discussion of grades
Creative Commons License photo credit: quinn.anya

This programming challenge is from an initial C++ programming course I took in Fall 2007. The Chapter (4) was titled “Making Decisions”.

I post these old academic challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it.

4.5: Test Scores and Letter Grade:
Write a program that has variables to hold three test scores. The program should ask the user to enter three test scores and then assign the values entered to the variables. The program should display the average of the test scores and the letter grade that is assigned for the test score average. Use the following grade scheme: 90 or greater = A, 80-89 = B, 70-79 = C, 60-69 = D, Below 60 = F. Assume valid user inputs.

My solution:

/* Chapter 4 Challenge 5
   Written by Kirk Hingsberger
   October 2, 2007
   Test scroes and grade
*/

#include <iostream>
using namespace std;

int main()
{
  float test1, test2, test3, avg;

  cout << "Please enter your three test scores each separated by a space.\n";
  cin >> test1 >> test2 >> test3;

  avg = (test1 + test2 + test3) / 3;

  cout << "Your test average " << avg << " earned you an ";

  if (avg > 90)
    {
      cout << "A." << endl;
    }
  else if (avg > 80)
    {
      cout << "B." << endl;
    }
  else if (avg > 70)
    {
      cout << "C." << endl;
    }
  else if (avg > 60)
    {
      cout << "D." << endl;
    }
  else
    {
      cout << "F." << endl;
    }

  return 0;
}

Here is the capture script I turned in for grading:

Script started on Tue 02 Oct 2007 11:21:02 AM MDT
$ cat chap4chlg5.cc
/* Chapter 4 Challenge 5
   Written by Kirk Hingsberger
   October 2, 2007
   Test scroes and grade
*/

#include <iostream>
using namespace std;

int main()
{
  float test1, test2, test3, avg;

  cout << "Please enter your three test scores each separated by a space.\n";
  cin >> test1 >> test2 >> test3;

  avg = (test1 + test2 + test3) / 3;

  cout << "Your test average " << avg << " earned you ";

  if (avg > 90)
    {
      cout << "an A." << endl;
    }
  else if (avg > 80)
    {
      cout << "a B." << endl;
    }
  else if (avg > 70)
    {
      cout << "a C." << endl;
    }
  else if (avg > 60)
    {
      cout << "a D." << endl;
    }
  else
    {
      cout << "an F." << endl;
    }

  return 0;
}
$ g++ -o chap4chlg5 chap4chlg5.cc -s
$ chap4chlg5
Please enter your three test scores each separated by a space.
44.5 84.5 99.9
Your test average 76.3 earned you a C.
$ exit
exit

Script done on Tue 02 Oct 2007 11:21:41 AM MDT
Share




Leave a Reply