Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name: Nickelis Jarvis
- Date: 11.13.14
- Program Name: Sinclair's Got Talent
- Description: Average judge scores and print winning contestant along with score.*/
- #include <iostream>
- #include <iomanip>
- #include <string>
- #include <vector>
- //Use a pointer function to determine which array member has the lowest value. Returns address of lowest value in array.
- int* findLowest(int* score, int size) {
- int* lowest = score;
- for (int i = 0; i < size; i++) {
- if (*lowest > *score)
- *lowest = *score;
- score++;
- }
- return lowest;
- }
- //Use a pointer function to determine which array member has the highest value. Returns address of highest value in array.
- int* findHighest(int* score, int size) {
- int* highest = score;
- for (int i = 0; i < size; i++) {
- if (*highest < *score)
- *highest = *score;
- score++;
- }
- return highest;
- }
- //Calculate average by method (avg = (sum{array} - highest value - lowest value) / total numbers in set).
- //Uses pointer functions so no copies are made in memory.
- double calcAvgScore(int *score, int size) {
- double avg = 0;
- for (int i = 0; i < size; i++) {
- avg += score[i];
- }
- avg = (avg - *findHighest(&score[0], size) - *findLowest(&score[0], size)) / (size - 2);
- return avg;
- }
- int main() {
- std::vector<std::string> c_name; //Declare a vector for contestant name.
- std::vector<double> c_average; //Declare a vector for contestant average.
- bool newContestant; //Create a flag to check for new contestants.
- //Output header.
- std::cout << "Sinclair's Got Talent!" << std::endl;
- //Initialize a do-while loop for each contestant until flag(contestant) = false.
- do {
- std::cout << "\nEnter contestant name: "; //Ask for contestant's name, then create a scope for input.
- std::string name_temp; //Create a new string for the contestant's name.
- std::cin >> name_temp; //Use getline with a newline delimiter to put contestants name in our temp variable.
- if (name_temp != "Done"){ //Check if contestants name is "Done"
- c_name.push_back(name_temp); //If it's not, then it's a legit contestant, so push it into our vector.
- }
- int c_score[5]; //Declare an int array to keep scores in.
- for (int i = 0; i < 5; i++) { //Create a for loop to get scores.
- while (1) { //Infinite while loop with break on valid input.
- int score_temp; //Create a temporary variable for scores to get from cin.
- std::cout << "Enter judge #" << i + 1 << "\'s score: "; //Ask for judge's score.
- std::cin >> score_temp; //Dump to temporary variable from cin.
- if (score_temp > 0 && score_temp <= 10) { //Check for valid range, if valid:
- c_score[i] = score_temp; //Assign array member the score.
- break; //Break the infinite loop.
- }
- else if (name_temp != "Done")
- 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.
- else
- break;
- }
- }
- if (name_temp != "Done") //Check if the array has any value, and see if the name isn't "Done"
- c_average.push_back(calcAvgScore(c_score, 5)); //If there's value and the name isn't "Done", calculate average.
- else
- newContestant = false; //Otherwise, set flag to false so do-while loop exits.
- } while (newContestant);
- std::string* winner_name = &c_name[0]; //Declare a string pointer to first member of vector(c_name)
- double* winner_avg = &c_average[0]; //Declare a double pointer to first member of vector(c_average)
- for (int i = 0; i < c_average.size(); i++) { //For loop to scan for highest average
- if (c_average[i] > *winner_avg) { //Check if value of c_average[i] is higher than current pointer value
- *winner_avg = c_average[i]; //If so, pointer will now point to c_average[i]
- *winner_name = c_name[i]; //Winner_name pointer will point to sychronous member of c_name[i].
- }
- }
- //Output member name and average to 2 decimal places from the pointers *winner_name and *winner_avg
- std::cout
- << std::fixed << std::setprecision(2)
- << "\n...and the winner is " << *winner_name << " with a score of " << *winner_avg << std::endl;
- system("pause"); //Pause to look at input
- return 0; //Return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement