Question
Problem: A fuel-economy study was carried out for five models of cars. Each car was driven 100 miles. Then the car model and number of
Problem: A fuel-economy study was carried out for five models of cars. Each car was driven 100 miles. Then the car model and number of gallons used were entered in the file pgm7.txt. Write a C++ program to analyze this data. Create a project using your last name, first initial and pgm7 for the project folder and .cpp file names (e.g., FordH_pgm7 and FordH_pgm7.cpp). Your output may be displayed on the screen.
Input: The model of each car tested and the gallons of gas used to go 100 miles is stored in the file pgm7.txt. Copy this file to your project folder. Your project should read in each model name and gallons used and store and store the data in parallel arrays. Then using the data stored in the arrays, perform the steps described below.
Program Design: Your program should define three arrays: a string array to hold the model names, a double array to hold the gallons used and another double array that will be used to store the miles per gallon for each vehicle. These arrays must be declared inside the main function and passed as arguments to the functions that need to use them. Your program should handle data for any number of vehicles up to a maximum of twenty. Your main function should call the functions described below. Each function should begin with comments describing the purpose of the function.
1. Call a function to read the model names and gallons used from the file pgm7.txt, storing each model name in an element in an array of type string and the gallons in the same position in an array of type double. For example, the first model name and gallons read in should be stored in element 0 of the two arrays, the next model and gallons read in should be stored in element 1 of the two arrays, etc. Count the number of vehicles read in and pass this count back to the main function. It will be passed on to other functions and used to determine how many elements of the array actually contain data. Open and close the data file in this function. All further processing must be done using the data values stored in the arrays. The first few lines in the file look like this: Prius 2.1 Camry 4.1 . . .
2. Call a function to find the miles per gallon for each vehicle and store the value in the corresponding element in the second array of type double. For example, Prius and 2.1 should be stored in element 0 of the string and first double arrays. Calculate the miles per gallon for this vehicle (100/2.1) and store the result in element 0 of the second array of type double. Do the same for each vehicle.
3. Call a function to display each vehicle's model, gallons used and miles per gallon. Include a heading with a title, your name and column headings (see sample on next page). Vehicle Miles Per Gallon Your Name Model Gallons used/100 miles MPG ----------------------------------------------------- Prius 2.1 48 Camry 4.1 24 . . . . . . .
4. Call a function that prompts the user to enter a model name and displays the average number of gallons used by vehicles with that model name. For example, if the user enters "Prius", the function should display the message: The average number of gallons used to travel 100 miles by Prius vehicles was 2.2 gallons. If the user enters "Corolla", the function should display the message: No Corolla vehicles tested.
5. Call a function to sort the arrays in descending order by the miles per gallon. Be sure that whenever you switch two elements in the miles per gallons array to also switch the same pair of elements in the model name and gallons used arrays.
6. Call the function from step 3 again after the sort to display each vehicle, the gallons used and the miles per gallon. Include a heading with a title, your name and column headings. Vehicles Sorted by MPG Your Name Model Gallons used/100 miles MPG ------------------------------------------------------ Prius 2.1 48 Prius 2.3 43 . . . . . . .
Program Documentation & Style: 1. Declare all constants and variables that your program uses at the beginning of your program. 2. Your program should include three types of comments. BE SPECIFIC! a. Header comments at the beginning of your program including lines with: - Your name, course name, and class time - Program assignment number, program file name and due date - Purpose: a sentence or two explaining the purpose of the program - Input: a description of the input data needed by the program when you run it - Processing: a description of the processing (calculations) done by the program - Output: a description of the results (output) produced by the program b. In-line comments: There should be an in-line comment for each main step in your program. In general, this means at least one comment with each group of C++ statements that handles the input, the processing and the output steps of your program. c. Function comments: Include a sentence or two before each function definition explaining the purpose of the function. 3. Use meaningful identifier names 4. Include clear prompts for the user about entering the data. 5. Include clear descriptions of the results when you display them.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started