Thursday, January 5, 2017

Programming Challenge 6.12 - Star Search

/* Star Search - A particular competition has five judges, each of whom
   awards a score between 0 and 10 to each performer. Fractional scores,
   such as 8.3, are allowed. A performer's final score is determined by
   dropping the highest and lowest score received, then, averaging the
   three remaining scores.
 
   This program uses this method to calculate a contestant's score. It
   includes the following functions:
 
   * void getJudgeData()
   * void calcScore()

   The last two functions are called by calcScore, which uses the returned
   information to determine which of the top scores to drop.

   * int findLowest()
   * int findHighest()

   Input Validation: No judge scores lower than 0 or higher than 10 are
   accepted. */

#include "Utility.h"

/* Prototypes: Get judge data, Calculate score,
               Find lowest, Find highest */
void getJudgeData(double &, string);
void calcScore(double, double, double, double, double, double);
int findLowest(double, double, double, double, double, double);
int findHighest(double, double, double, double, double, double);

int main()
{
   /* Variables: Scores one through five */
   double refScoreOne = 0,
          refScoreTwo = 0,
          refScoreThree = 0,
          refScoreFour = 0,
          refScoreFive = 0,
          calcAvgScore = 0;

   /* Variable: Judge name */
   string judgeName = " ";

   /* Display: Welcome message
      Get: Judge scores */
   cout << "\t\tYAMAHA ANNUAL POPULAR CONTEST FINALS\n\n"
        << "Welcome to the annual YAMAHA Popular Contest!\n"
        << "Our judges are eager to vote on the hottest new talents\n"
        << "and already famous Stars in the Popular Music Scene of\n"
        << "today.\n\n"
        << "Please try hard!\n\n";

   /* Call: getJudgeData */
   getJudgeData(refScoreOne, "N. Chino");
   getJudgeData(refScoreTwo, "A. Nakano");
   getJudgeData(refScoreThree, "Y. Ono");
   getJudgeData(refScoreFour, "K. Watanabe");
   getJudgeData(refScoreFive, "H. Yasutake");

   /* Call: calcScore */
   calcScore(refScoreOne, refScoreTwo, refScoreThree, refScoreFour,
             refScoreFive, calcAvgScore);

   pauseSystem();
   return 0;
}

/* **********************************************************
   Definition: getJudgeData

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

void getJudgeData(double &judgeScore, string judgeName)
{
   /* Get: Judge score */
   cout << "" << judgeName << "'s Vote: ";
   cin >> judgeScore;

   /* Validate: Input */
   while (judgeScore <= 0 || judgeScore > 10)
   {
      cout << "\nJudge " << judgeName << " score could not be\n"
           << "evaluated. It was either below 0 or higher than 10.\n"
           << "Please repeat your vote: ";
      cin >> judgeScore;

      /* Catch: Infinite loop */
      cin.clear();
      cin.ignore();
   }
}

/* **********************************************************
   Definition: calcScore

   This function calculates and displays the average of the
   three test scores that remain after dropping the highest
   and lowest scores the performer received. This function is
   called once by main and passes the five scores.
   ********************************************************** */

void calcScore(double avgScoreOne, double avgScoreTwo,
               double avgScoreThree, double avgScoreFour,
               double avgScoreFive, double avgScore)
{
   /* Variables: Lowest score, Highest score */
   double highestScore = 0.0,
          lowestScore = 0.0;

   /* Call: findLowest */
   lowestScore = findLowest(avgScoreOne, avgScoreTwo, avgScoreThree,
      avgScoreFour, avgScoreFive, lowestScore);

   /* Call: findHighest */
   highestScore = findHighest(avgScoreOne, avgScoreTwo, avgScoreThree,
      avgScoreFour, avgScoreFive, highestScore);

   /* Calculate: The average score */
   avgScore = (avgScoreOne + avgScoreTwo + avgScoreThree +
               avgScoreFour + avgScoreFive - highestScore - lowestScore) / 3;

   /* Set up: Numeric output formatting */
   cout << fixed << showpoint << setprecision(1);

   /* Display: The average of the remaining three scores */
   cout << "\nAfter calculating the scores of our judges,\n"
        << "we can finally announce the winners of this years\n"
        << "YAMAHA Popular Contest: 'Hide and Rosanna'\n"
        << "They won with an average score of: " << avgScore
        << "\n\nCongratulations!\n";
}

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

   This function finds and returns the lowest of five scores
   passed to it.
   ********************************************************** */

int findLowest(double lowestRefOne, double lowestRefTwo,
               double lowestRefThree, double lowestRefFour,
               double lowestRefFive, double lowestRefScore)
{
   /* lowestRefScore is initialized to lowestRefOne to have a
      starting value for comparing the other scores */
   lowestRefScore = lowestRefOne;

   /* These conditional statements determine the lowest of the
      five scores to be dropped */
   lowestRefScore = lowestRefTwo < lowestRefScore ?
                    lowestRefTwo : lowestRefScore;
   lowestRefScore = lowestRefThree < lowestRefScore ?
                    lowestRefThree : lowestRefScore;
   lowestRefScore = lowestRefFour < lowestRefScore ?
                    lowestRefFour : lowestRefScore;
   lowestRefScore = lowestRefFive < lowestRefScore ?
                    lowestRefFive : lowestRefScore;

   /* Display: The lowest score that will be dropped */
   cout << "\nThe lowest score was " << lowestRefScore
        << " and has not been taken into account.\n";

   /* Return: lowestRefScore is demoted before it is returned
              to calcScore */
   return (int)lowestRefScore;
}

/* **********************************************************
   Definition: findHighest

   This function finds and returns the highest of five scores
   passed to it.                                            
   ********************************************************** */

int findHighest(double highestRefOne, double highestRefTwo,
                double highestRefThree, double highestRefFour,
                double highestRefFive, double highestRefScore)
{
   /* Highest score is initialized to highestRefOne as an
      initial value for determining the highest score that
      will be dropped */
   highestRefScore = highestRefOne;
 
   /* These conditional statements determine the highest
       of the five scores to be dropped */
   highestRefScore = highestRefTwo > highestRefOne ?
                     highestRefTwo : highestRefScore;
   highestRefScore = highestRefThree > highestRefScore ?
                     highestRefThree : highestRefScore;
   highestRefScore = highestRefFour > highestRefScore ?
                     highestRefFour : highestRefScore;
   highestRefScore = highestRefFive > highestRefScore ?
                     highestRefFive : highestRefScore;

   /* Display: The highest score that will be dropped */
   cout << "The highest score was " << highestRefScore
      << " and has not been taken into account.\n";

   /* Return: highestRefScore is demoted to int before returning
              it to calcscore to calcScore */
   return (int)highestRefScore;
}

No comments:

Post a Comment