Advertisement
hfxdesign

Sinclair's Got Talent!

Nov 15th, 2014
834
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.25 KB | None | 0 0
  1. /*
  2. Name: Nickelis Jarvis
  3. Date: 11.13.14
  4. Program Name: Sinclair's Got Talent
  5. Description: Average judge scores and print winning contestant along with score.*/
  6.  
  7. #include <iostream>
  8. #include <iomanip>
  9. #include <string>
  10. #include <vector>
  11.  
  12. //Use a pointer function to determine which array member has the lowest value. Returns address of lowest value in array.
  13. int* findLowest(int* score, int size) {
  14.     int* lowest = score;
  15.     for (int i = 0; i < size; i++) {
  16.         if (*lowest > *score)
  17.             *lowest = *score;
  18.         score++;
  19.     }
  20.     return lowest;
  21. }
  22.  
  23. //Use a pointer function to determine which array member has the highest value. Returns address of highest value in array.
  24. int* findHighest(int* score, int size) {
  25.     int* highest = score;
  26.     for (int i = 0; i < size; i++) {
  27.         if (*highest < *score)
  28.             *highest = *score;
  29.         score++;
  30.     }
  31.     return highest;
  32. }
  33.  
  34. //Calculate average by method (avg = (sum{array} - highest value - lowest value) / total numbers in set).
  35. //Uses pointer functions so no copies are made in memory.
  36. double calcAvgScore(int *score, int size) {
  37.     double avg = 0;
  38.     for (int i = 0; i < size; i++) {
  39.         avg += score[i];
  40.     }
  41.     avg = (avg - *findHighest(&score[0], size)  -  *findLowest(&score[0], size)) / (size - 2);
  42.     return avg;
  43. }
  44.  
  45.  
  46. int main() {
  47.     std::vector<std::string> c_name;            //Declare a vector for contestant name.
  48.     std::vector<double> c_average;              //Declare a vector for contestant average.
  49.     bool newContestant;                 //Create a flag to check for new contestants.
  50.  
  51.                                 //Output header.
  52.     std::cout << "Sinclair's Got Talent!" << std::endl;
  53.  
  54.                                 //Initialize a do-while loop for each contestant until flag(contestant) = false.
  55.     do {
  56.         std::cout << "\nEnter contestant name: ";   //Ask for contestant's name, then create a scope for input.
  57.             std::string name_temp;          //Create a new string for the contestant's name.
  58.             std::cin >> name_temp;          //Use getline with a newline delimiter to put contestants name in our temp variable.
  59.             if (name_temp != "Done"){       //Check if contestants name is "Done"
  60.                 c_name.push_back(name_temp);    //If it's not, then it's a legit contestant, so push it into our vector.
  61.             }
  62.  
  63.         int c_score[5];                 //Declare an int array to keep scores in.
  64.         for (int i = 0; i < 5; i++) {           //Create a for loop to get scores.
  65.             while (1) {             //Infinite while loop with break on valid input.
  66.                 int score_temp;         //Create a temporary variable for scores to get from cin.
  67.  
  68.                 std::cout << "Enter judge #" << i + 1 << "\'s score: ";     //Ask for judge's score.
  69.                 std::cin >> score_temp;                     //Dump to temporary variable from cin.
  70.  
  71.                 if (score_temp > 0 && score_temp <= 10) {           //Check for valid range, if valid:
  72.                     c_score[i] = score_temp;                //Assign array member the score.
  73.                     break;                          //Break the infinite loop.
  74.                 }
  75.                 else if (name_temp != "Done")
  76.                     std::cout << "Invalid score, please enter a value between 1 and 10.\n" << std::endl;    //Otherwise, display a try again error and the loop will repeat.
  77.                 else
  78.                     break;
  79.             }
  80.         }
  81.         if (name_temp != "Done")                            //Check if the array has any value, and see if the name isn't "Done"
  82.             c_average.push_back(calcAvgScore(c_score, 5));              //If there's value and the name isn't "Done", calculate average.
  83.         else
  84.             newContestant = false;                          //Otherwise, set flag to false so do-while loop exits.
  85.     } while (newContestant);
  86.    
  87.  
  88.     std::string* winner_name = &c_name[0];                          //Declare a string pointer to first member of vector(c_name)
  89.     double* winner_avg = &c_average[0];                         //Declare a double pointer to first member of vector(c_average)
  90.     for (int i = 0; i < c_average.size(); i++) {                        //For loop to scan for highest average
  91.         if (c_average[i] > *winner_avg) {                       //Check if value of c_average[i] is higher than current pointer value
  92.             *winner_avg = c_average[i];                     //If so, pointer will now point to c_average[i]
  93.             *winner_name = c_name[i];                       //Winner_name pointer will point to sychronous member of c_name[i].
  94.         }
  95.     }
  96.  
  97.     //Output member name and average to 2 decimal places from the pointers *winner_name and *winner_avg
  98.     std::cout
  99.         << std::fixed << std::setprecision(2)
  100.         << "\n...and the winner is " << *winner_name << " with a score of " << *winner_avg << std::endl;
  101.     system("pause");    //Pause to look at input
  102.     return 0;           //Return EXIT_SUCCESS;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement