Question

In this lab, you complete a partially prewritten C++ program that uses an array. The program...

In this lab, you complete a partially prewritten C++ program that uses an array.

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

  1. Ensure the source code file named BattingAverage.cpp is open in the code editor.

  2. Write the C++ statements as indicated by the comments.

  3. 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.

GIVEN CODE

#include <iostream>

#include <string>

using namespace std;

int main()

{

   // Declare a named constant for array size here

   

   // Declare array here

   

   // Use this integer variable as your loop index

   int loopIndex;

   // Use this variable to store the batting average input by user

   double battingAverage;

   // Use these variables to store the minimim and maximum values

   double min, max;

   // Use these variables to store the total and the average

   double total, average;

   // Write a loop to get batting averages from user and assign to array

   

      cout << "Enter a batting average: ";

      cin >> battingAverage;

      // Assign value to array

      

   // Assign the first element in the array to be the minimum and the maximum

   min = averages[0];

   max = averages[0];

   // Start out your total with the value of the first element in the array

   total = averages[0];

   // Write a loop here to access array values starting with averages[1]

   

      // Within the loop test for minimum and maximum averages.

      

      // Also accumulate a total of all averages

      

   

       

   // Calculate the average of the 8 batting averages

   

   // Print the batting averages stored in the averages array

   

   // Print the maximum batting average, minimum batting average, and average batting average

   

   return 0;

}

Homework Answers

Answer #1

Hi,

Hope you are doing fine. I have written the code as per the requiremnts mentioned and have executed it. The code is clearly explained using comments that have been highlighted in bold. The logic is pretty straightforward and also have a look the executable snippet from code editor and the sample output. You may modify the output as per your requirement (some print stements of output).

Program:

#include <iostream>
#include <string>

using namespace std;

int main()
{

// Declare a named constant for array size here
const int size=8;
// Declare array here
double averages[size];
// Use this integer variable as your loop index
int loopIndex;
// Use this variable to store the batting average input by user
double battingAverage;
// Use these variables to store the minimim and maximum values
double min, max;
// Use these variables to store the total and the average
double total, average;
// Write a loop to get batting averages from user and assign to array
   for(loopIndex=0;loopIndex<size;loopIndex++)
   {
cout << "Enter a batting average: ";
cin >> battingAverage;
// Assign value to array
   averages[loopIndex]=battingAverage;
   }
// Assign the first element in the array to be the minimum and the maximum
min = averages[0];
max = averages[0];
// Start out your total with the value of the first element in the array
total = averages[0];
// Write a loop here to access array values starting with averages[1]
   for(loopIndex=1;loopIndex<size;loopIndex++)
   {
// Within the loop test for minimum and maximum averages.
//checking for maximum

if(averages[loopIndex]>max)
max=averages[loopIndex];
//checking for minimum
if(averages[loopIndex]<min)
min=averages[loopIndex];
// Also accumulate a total of all averages
total=total+averages[loopIndex];
    }
// Calculate the average of the 8 batting averages
//average is given by total/size

average=total/size;
// Print the batting averages stored in the averages array
   cout << "Batting averages stored in averages: \n";
    for(loopIndex=0;loopIndex<size;loopIndex++)
    cout << averages[loopIndex] << "\n";
// Print the maximum batting average, minimum batting average, and average batting average
cout << "Maximum batting average: " << max <<"\n";
cout << "Minimum batting average: " << min <<"\n";
cout << "Average batting average: " << average <<"\n";

return 0;
}

Executable code snippet:

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
n this lab, you use what you have learned about parallel arrays to complete a partially...
n this lab, you use what you have learned about parallel arrays to complete a partially completed C++ program. The program should: Either print the name and price for a coffee add-in from the Jumpin’ Jive Coffee Shop Or it should print the message Sorry, we do not carry that. Read the problem description carefully before you begin. The file provided for this lab includes the necessary variable declarations and input statements. You need to write the part of the...
Write a C program Design a program that uses an array to store 10 randomly generated...
Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...
Question 5: Recommend / Explain a C++ program which uses an array of 20 integers whose...
Question 5: Recommend / Explain a C++ program which uses an array of 20 integers whose input is taken by user, the array is passed to a functions named i.e. smallest(int A[20) and largest(int B[20]) to determine minimum and maximum values respectively. Also create a function named modify(int *p) which modifies the value at the index given by user.
PLEASE COMMENT CODE AND TEST THAT IT WORKS: Write a assembly program to find the largest...
PLEASE COMMENT CODE AND TEST THAT IT WORKS: Write a assembly program to find the largest item in an array and store it in a AX. Hint: Use both Jump and loop instruction to write the program. logic: Assume that the first item of the array is the minimum and store it in AX Write a loop. Inside the loop, compare the each array item with the AX If the array item is less than the AX, update AX with...
In c++ Write a program that creates a dynamically allocated array to store students’ grades. The...
In c++ Write a program that creates a dynamically allocated array to store students’ grades. The user should be able to decide the size of the array during run time. Write code to fill this array from the keyboard and then print out the values of your array,
I need this before the end of the day please :) In Java 10.13 Lab 10...
I need this before the end of the day please :) In Java 10.13 Lab 10 Lab 10 This program reads times of runners in a race from a file and puts them into an array. It then displays how many people ran the race, it lists all of the times, and if finds the average time and the fastest time. In BlueJ create a project called Lab10 Create a class called Main Delete what is in the class you...
Phone number lookup Design a program that has two parallel arrays: a string array named people...
Phone number lookup Design a program that has two parallel arrays: a string array named people that is initialized with the names of seven of your friends, and a string array named phoneNumbers that is initialized with your friends phone numbers. The program should allow the user to enter a persons name (or part of a persons name). it should then search for that person in the people array. If the person is found, it should get that persons phjone...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat...
C++ Goals:Practicing arrays Create a program that will read whole numbers from a file called Labs4-7Mu.dat (posted on Canvas)and store it into an array. The number of values in the file is less than 300 and all the values are whole numbers. The actual number of values stored in the file should be determined. Your program should then prompt the user to enter another whole number between 2 and 20 (you may assume the user enters a valid value) and...
COMPLETE IN C++ Declare and initialize a global constant named SIZE with the value 50. Write...
COMPLETE IN C++ Declare and initialize a global constant named SIZE with the value 50. Write a void function called count that accepts two parameters-- a C-string called str representing a C-string passed to the function and an array of integers called alphabets to store the count of the letters of the C-string. Note that the alphabets array stores 26 counters – one for each letter of the alphabet. Use the same counter for lowercase and uppercase characters. The first...
Programing lanugaue is C++ Plan and code a menu-driven modular program utilizing an array An input...
Programing lanugaue is C++ Plan and code a menu-driven modular program utilizing an array An input file has an unknown number of numeric values(could be empty too).Read the numbers from the input fileand store even numbers in one arrayand odd numbers in another array.Create menu options to Display count of even numbers, count of odd numbersand the sum values in each array Determine the average of each array Determine the median of each array Sort each array in ascending order(use...