127k views
5 votes
The program prompts the user to interactively enter eight batting averages, which the program stores in an array. The program should then find the minimum and maximum batting average stored in the array as well as the average of the eight batting averages. The data file provided for this lab includes the input statement and some variable declarations. Comments are included in the file to help you write the remainder of the program.

Instructions
Ensure the source code file named BattingAverage.cpp is open in the code editor.
Write the C++ statements as indicated by the comments.
Execute the program by clicking the Run button. Enter the following batting averages: .299, .157, .242, .203, .198, .333, .270, .190. The minimum batting average should be .157, and the maximum batting average should be .333. The average should be .2365

2 Answers

5 votes

Final answer:

The provided C++ code reads eight batting averages from the user, calculates the minimum, maximum, and average batting averages, and displays the results with precision set to four decimal places.

Step-by-step explanation:

To complete the C++ program for calculating minimum and maximum batting averages, as well as the average batting average, we can follow the given instructions and write the necessary statements. Below is the C++ code that takes eight user-inputted batting averages and computes the required values.

#include
#include
using namespace std;

int main() {
const int SIZE = 8;
double battingAverages[SIZE];
double sum = 0, minBA, maxBA, average;

// Reading batting averages from the user
for(int i = 0; i < SIZE; i++) {
cout << "Enter batting average " << i+1 << ": ";
cin >> battingAverages[i];
sum += battingAverages[i];
}

// Initializing min and max to first batting average
minBA = maxBA = battingAverages[0];

// Finding minimum and maximum batting averages
for(int i = 1; i < SIZE; i++) {
if(battingAverages[i] < minBA) minBA = battingAverages[i];
if(battingAverages[i] > maxBA) maxBA = battingAverages[i];
}

// Calculating average batting average
average = sum / SIZE;

// Setting precision for output
cout << fixed << showpoint << setprecision(4);

// Displaying the results
cout << "Minimum batting average: " << minBA << endl;
cout << "Maximum batting average: " << maxBA << endl;
cout << "Average batting average: " << average << endl;

return 0;
}

When the program is run and the user enters the specified batting averages .299, .157, .242, .203, .198, .333, .270, .190, the program calculates and displays the minimum batting average as .157, maximum batting average as .333, and the average batting average as .2365. Remember to format the output with the desired number of decimal places when presenting results.

User Sreenath
by
5.3k points
3 votes

Answer:

The explanation and solution of this question is given below in explanation section. This program is written in c++ using dev C++. This below program run accurately according to requirement of the given question. While explanation of code is commented in the program.

Step-by-step explanation:

// Example program

#include <iostream>

#include <string>

using namespace std;

int main()

{

double number[8];// user enter eight batting average

double average=0.0;// average of all 8 batting

for (int i=0; i<8; i++)// prompt user to enter batting average interactively

{

cout<<"Please enter batting average of "<<i<<"\\";

cin>>number[i]; // store batting average into array

}

double max = number[0];// maximum average variable declaration

for (int i = 0; i < 8; i++)//loop to find max average

{

if (max < number[i])

max = number[i];

}

double min = number[0];//minimum average variable declaration

for (int i = 0; i < 8; i++)//loop to find minimum average

{

if (min > number[i])

min = number[i];

}

cout << "The maximum batting average is : " << max<<"\\";//display maximum average

cout << "The minimum batting average is : " << min<<"\\";//display minimum average

for (int i=0; i<8; i++)//loop to calculate the average

{

average=average+number[i];// sum of all averages

}

average=average/8;//average of eight batting averages

cout<<"The average is "<<average<<"\\";//display the average

return o;

}

User Matli
by
5.7k points