SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,kenrick Mock
Chapter:
Arrays
Exercise:
Programming Projects
Question:6 | ISBN:9780132846813 | Edition: 5

Question

In the sport of diving, seven judges award a score between 0 and 10, where each score may be a floating-point value. The highest and lowest scores are thrown out and the remaining scores are added together. The sum is then multiplied by the degree of difficulty for that dive. The degree of difficulty ranges from 1.2 to 3.8 points. The total is then multiplied by 0.6 to determine the diver’s score.

Write a computer program that inputs a degree of difficulty and seven judges’ scores, and outputs the overall score for that dive. The program should ensure that all inputs are within the allowable data ranges.


TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

PROGRAM CODE: 

//Header section
#include <iostream>
#include <limits>
using namespace std;

// Function to validate if a value is within a specified range
bool validateRange(float value, float minRange, float maxRange)
{
    return (value >= minRange && value <= maxRange);
}

// Function to calculate the overall score for a dive
float calculateDiveScore(float degreeOfDifficulty, float scores[]) {
    // Constants for data ranges
    
    const float MIN_SCORE = 0.0;
    const float MAX_SCORE = 10.0;
    const float MIN_DIFFICULTY = 1.2;
    const float MAX_DIFFICULTY = 3.8;

    // Validate degree of difficulty
    if (!validateRange(degreeOfDifficulty, MIN_DIFFICULTY, MAX_DIFFICULTY)) {
        cout << "Invalid degree of difficulty. Please enter a value between " << MIN_DIFFICULTY << " and " << MAX_DIFFICULTY << endl;
        return 0.0;
    }

    // Variables to store highest and lowest scores
    float highestScore = numeric_limits<float>::lowest();
    float lowestScore = numeric_limits<float>::max();

    // Variables to calculate the sum of scores
    float sumOfScores = 0.0;

    // Find the highest and lowest scores, and calculate the sum of the remaining scores
    for (int i = 0; i < 7; i++) {
        // Validate individual scores
        if (!validateRange(scores[i], MIN_SCORE, MAX_SCORE)) {
            cout << "Invalid score. Please enter a value between " << MIN_SCORE << " and " << MAX_SCORE << endl;
            return 0.0;
        }

        if (scores[i] > highestScore) {
            highestScore = scores[i];
        }
        if (scores[i] < lowestScore) {
            lowestScore = scores[i];
        }

        sumOfScores += scores[i];
    }

    // Subtract the highest and lowest scores from the sum
    sumOfScores -= highestScore;
    sumOfScores -= lowestScore;

    // Calculate the overall score
    float overallScore = sumOfScores * degreeOfDifficulty * 0.6;

    return overallScore;
}
//Program starts with a main method
int main() 
{
    //Declare variables
    float degreeOfDifficulty;
    float scores[7];

    // Get the degree of difficulty from the user
    cout << "Enter the degree of difficulty (between 1.2 and 3.8): ";
    cin >> degreeOfDifficulty;

    // Get the scores from the user
    cout << "Enter the scores for the seven judges (between 0 and 10): " << endl;
    for (int i = 0; i < 7; i++) {
        cout << "Judge " << i + 1 << ": ";
        cin >> scores[i];
    }

    // Calculate the overall score for the dive
    float overallScore = calculateDiveScore(degreeOfDifficulty, scores);

    // Display the overall score
    cout << "The overall score for the dive is: " << overallScore << endl;

    return 0;
}

OUTPUT OF THE PROGRAM CODE:

Enter the degree of difficulty (between 1.2 and 3.8): 3.6
Enter the scores for the seven judges (between 0 and 10): 
Judge 1: 9
Judge 2: 10
Judge 3: 8.5
Judge 4: 6.5
Judge 5: 3
Judge 6: 8
Judge 7: 9
The overall score for the dive is: 88.56

 

0 0

Discussions

Post the discussion to improve the above solution.