a program that computes a diver’s score based on the degree of difficulty and 5
ID: 3807423 • Letter: A
Question
a program that computes a diver’s score based on the degree of difficulty and 5 judge’s scores. In diving, the low and the high score are dropped. The middle three scores are added and then multiplied by the degree of difficulty. Unfortunately, this program doesn’t work. There are three logical errors in it.
Correct Output (Inputs in Bold)
Please enter the degree of difficulty: 2.3
Please enter the score from Judge #1: 6.0
Please enter the score from Judge #2: 5.5
Please enter the score from Judge #3: 6.5
Please enter the score from Judge #4: 7.0
Please enter the score from Judge #5: 6.0
The score for the dive is: 42.55
*********************************************************************************************************
#include <iostream>
#include <iomanip>
#include <vector>
using namespace std;
// Function Prototypes
void inputData(double[]);
int lowMonth(double[]);
int highMonth(double[]);
double averageSales(double[]);
const char * monthArray[] = { "1:" , "2:" , "3:", "4:", "5:"};
const int NUM_MONTHS = 5;
int main()
{
double salesArray[NUM_MONTHS];
int low, high;
double average;
// Input the monthly sales data
inputData(salesArray);
// Find the month with the highest sales
high = highMonth(salesArray);
// Find the month with the lowest sales
low = lowMonth(salesArray);
// Calculate the average monthly sales
average = averageSales(salesArray);
// Display results
cout << "The score for the dive is: " << setprecision(2) << fixed << average << endl;
cin.get();
cin.get();
}
double difficulty;
// This functions requests the monthly sales data from the user
void inputData(double sales[])
{
cout << "Please enter the degree of difficulty:" << endl;
cin >> difficulty;
for (int i = 0; i < NUM_MONTHS; i++)
{
cout << "Please enter the score from Judge # " << monthArray[i] << " ";
cin >> sales[i];
}
return;
}
// This function determines which month had the highest sales and
// returns the number for that month, 0 = January, 1 = February, etc.
int highMonth(double sales[])
{
double highest = 1;
int highIndex = 1;
for (int i = 0; i < NUM_MONTHS; i++)
{
if (sales[i] > highest)
{
highest = sales[i];
highIndex = i;
}
}
return highIndex;
}
// This function determines which month had the lowest sales and
// returns the number for that month, 0 = January, 1 = February, etc.
int lowMonth(double sales[])
{
double lowest = 999999;
int lowIndex = -1;
for (int i = 0; i < NUM_MONTHS; i++)
{
if (sales[i] < lowest)
{
lowest = sales[i];
lowIndex = i;
}
}
return lowIndex;
}
// This function computes the average monthly sales
double averageSales(double sales[])
{
double sum = 0;
for (int i =0 ; i < NUM_MONTHS; i++)
{
sum += sales[i];
}
return sum * difficulty;
}
Explanation / Answer
// The averageSales function has the errors it can be modifed . Below is the desired version
double averageSales(double sales[])
{
double sum = 0;
int highIndex = highMonth(sales);
int lowIndex = lowMonth(sales);
for (int i =0 ; i < NUM_MONTHS; i++)
{
if(i!=highIndex&&i!=lowIndex)
sum += sales[i];
}
return sum * difficulty;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.