Thursday, January 5, 2017

Programming Challenge 6.11 - Lowest Score Drop

/* Lowest Score Drop - This program calculates the average of a group of
   test scores, where the lowest score in the group is dropped. It uses
   the following functions:
  
   * void getScore()
   * void calcAverage()
   * int findLowest()
  
   Input Validation: No test scores lower than 0 or higher than 100 are
   accepted. */

#include "Utility.h"

/* Prototypes: Get score, Calculate average, Find lowest */
void getScore(int &);
void calcAverage (int, int, int, int, int);
int findLowest (int, int, int, int, int);

int main()
{
   /* Variables: Scores 1 through 5 */
   int scoreOne = 0,
       scoreTwo = 0,
       scoreThree = 0,
       scoreFour = 0,
       scoreFive = 0;

   /* Display: Information */
   cout << "\t\tAverage Test Score Calculator\n\n"
        << "This application allows you to enter five test scores,\n"
        << "to find the lowest score, which will be dropped, before\n"
        << "the average is calculated and displayed.\n\n";

   /* Call: getScore, the values are stored in scoreOne through scoreFive */
   getScore(scoreOne);
   getScore(scoreTwo);
   getScore(scoreThree);
   getScore(scoreFour);
   getScore(scoreFive);

   /* Call: calcAverage */
   calcAverage(scoreOne, scoreTwo, scoreThree, scoreFour, scoreFive);

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: getScore

   This function asks the user for a test score, stores it in
   a reference parameter variable, and validates it. It is
   called by main once for each of the five scores entered.
   ********************************************************** */

void getScore(int &scores)
{
   static int cnt = 1;

      /* Get: Scores */
      cout << "Please Enter Test Score " << (cnt++) << ": ";
       cin >> scores;

      /* Validation: If the user enters a number lower than 1
         or greater than 100, he or she will receive a message
         and be asked to enter the score again */
      while (scores < 0 || scores > 100)
      {
         cout << "\nIt seems that you have entered a number that\n"
              << "was below 1 or above 100. Please enter a valid\n"
              << "test score " << (cnt - 1) << ": ";
         cin >> scores;
      }
}

/* **********************************************************
   Definition: calcAverage

   This function calculates and displays the average of the
   four highest scores. It is called once by main and gets
   passed the five test scores.
   ********************************************************** */

void calcAverage(int avgScoreOne, int avgScoreTwo, int avgScoreThree,
                 int avgScoreFour, int avgScoreFive)
{
   /* Variables: Average score, Drop lowest */
   int avgScore = 0,
       dropLowest = 0;

   /* Call: findLowest, pass the test scores as arguments, and get
            the lowest test score */
   dropLowest = findLowest(avgScoreOne, avgScoreTwo, avgScoreThree,
                            avgScoreFour, avgScoreFive);

   /* Calculate: The average test score */
   avgScore = (avgScoreOne + avgScoreTwo + avgScoreThree +
               avgScoreFour + avgScoreFive - dropLowest) / 4;

   /* Display: The formatted average test score */
   cout << "\nYour average test score is: " << avgScore << endl;
}

/* **********************************************************
   Definition: findLowest

   This function finds and returns the lowest of the five
   scores passed to it. It is called by calcAverage, which
   uses the function to determine which of the five scores
   to drop.
   ********************************************************** */

int findLowest(int lowestScoreOne, int lowestScoreTwo,
               int lowestScoreThree, int lowestScoreFour,
               int lowestScoreFive)
{
   /* Variable: Lowest score initialized to lowestScoreOne */
   int lowestScore = lowestScoreOne;
 
   /* These conditional statements determine the lowest test score */
   lowestScore = lowestScoreTwo < lowestScore ? lowestScoreTwo : lowestScore;
   lowestScore = lowestScoreThree < lowestScore ? lowestScoreThree : lowestScore;
   lowestScore = lowestScoreFour < lowestScore ? lowestScoreFour : lowestScore;
   lowestScore = lowestScoreFive < lowestScore ? lowestScoreFive : lowestScore;
  
   /* Display: The lowest score that will be dropped */
   cout << "\nThe lowest test score out of five was " << lowestScore
        << "\nand will be dropped from the calculation.\n";
  
   /* Return: The lowest test score */
   return lowestScore;
}

No comments:

Post a Comment