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...
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.
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...
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,
A programmer that you work with, Peter, is a jerk. He is responsible for an array...
A programmer that you work with, Peter, is a jerk. He is responsible for an array that is a key part of an important program and he maintains a sum of the array value.He won't give you access to this array; however, your boss has told you that you need to get input from the user and then place it into the array.Each evening Peter will scan the code and remove any illegal references to his array. Using pointers, access...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics...
Topics Arrays Accessing Arrays Description Write a C++ program that will display a number of statistics relating to data supplied by the user. The program will ask the user to enter the number of items making up the data. It will then ask the user to enter data items one by one. It will store the data items in a double array. Then it will perform a number of statistical operations on the data. Finally, it will display a report...
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...
Assignment #4 – Student Ranking : In this assignment you are going to write a program...
Assignment #4 – Student Ranking : In this assignment you are going to write a program that ask user number of students in a class and their names. Number of students are limited to 100 maximum. Then, it will ask for 3 test scores of each student. The program will calculate the average of test scores for each student and display with their names. Then, it will sort the averages in descending order and display the sorted list with students’...
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...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT