Diving:(UsongC++) With the 2016 Summer Olympics just passed last summer, let’s l
ID: 3855569 • Letter: D
Question
Diving:(UsongC++)
With the 2016 Summer Olympics just passed last summer,
let’s learn a little about the judging process in the sport of
diving. For each dive, seven judges award a score between 0and 10, where each score may be a floating point value. Thehighest and lowest scores are thrown out and the remainingscores are added together. The sum is then multiplied by thedegree of difficulty for that dive. The degree of difficultyranges from 1.2 to 3.8 points. The total is then multiplied by0.6 to determine the diver’s score.
The Assignment:
Write a program that allows a user to input the degree of dive difficulty and seven judges’ scores and
finally prints out the overall score for that dive. The program should ensure that all inputs are within theallowable data ranges.
Hint: Use an array to store the scores.
Your program must have at least 5 functions:
1. An CollectInput() function that collects numeric input with input validation “wrapped” into
it.
2. A function to collect all seven judges’ scores
o Must utilize the CollectInput() function to ensure numeric values only entered
o Must check scores are in the right range (0-10)
3. A function to find and return the position of the smallest score in the set
4. A function to find and return the position of the largest score in the set
5. A function to find and return the sum of the remaining scores
Note: Functions 2-4 should not be able to update the set of scores
Include a loop in main() that lets the user repeat this computation for new input values again and againuntil the user says he or she wants to end the program.
Note: Your program need not worry about duplicate max and min scores. In the case of duplicate maxor min scores, simply toss out only one max and/or one min, and continue with the algorithm as usual
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
double collectInput(int min, int max)
{
cout << "Enter score: " ;
double score;
cin >> score;
if (score >= min && score <= max)
{
return score;
}
else
{
cout << "Please enter a number in range " <<min << "-" << max << endl;
return collectInput(min, max);
}
}
double collectInput(double min, double max)
{
cout << "Enter difficulty: " ;
double difficulty;
cin >> difficulty;
if (difficulty >= min && difficulty <= max)
{
return difficulty;
}
else
{
cout << "Please enter a number in range " <<min << "-" << max << endl;
return collectInput(min, max);
}
}
void collect(double arr[], int n, double *difficulty)
{
*difficulty = collectInput(1.2, 3.8);
for(int i = 0; i < n; i++)
{
arr[i] = collectInput(0, 10);
}
}
int findMinPos(double *arr, int n)
{
int smallestPos = 0;
double smallest = arr[0];
for(int i = 1; i < n ; i++)
{
if (arr[i] < smallest)
{
smallest = arr[i];
smallestPos = i;
}
}
return smallestPos;
}
int findMaxPos(double *arr, int n)
{
int largestPos = 0;
double largest = arr[0];
for(int i = 1; i < n ; i++)
{
if (arr[i] > largest)
{
largest = arr[i];
largestPos = i;
}
}
return largestPos;
}
double findSum(double *arr, int n, int smallestPos, int largestPos)
{
double sum = 0;
for(int i = 0; i < n; i++)
{
if (i == smallestPos || i == largestPos)
{
continue;
}
sum += arr[i];
}
return sum;
}
int main()
{
string choice;
do
{
int n = 7;
double *scores = new double[n];
double difficulty;
collect(scores, n, &difficulty);
int smallestPos = findMinPos(scores, n);
int largestPos = findMaxPos(scores, n);
double sum = findSum(scores, n, smallestPos, largestPos);
double score = sum*difficulty*0.6;
cout << "Dive score is : " << score << endl;
delete scores;
cout << "Do you want to continue: (yes/no): ";
cin >> choice;
} while(choice != "no");
}
// Sample run
Enter difficulty: 2.5
Enter score: 10
Enter score: 9
Enter score: 9
Enter score: 9
Enter score: 9
Enter score: 9
Enter score: 0
Dive score is : 67.5
Do you want to continue: (yes/no): no