Question: Problem: Write a program to analyze data about the gas mileage of cars. A fuel-economy study was carried out for various models of cars. Each

Problem: Write a program to analyze data about the gas mileage of cars. A fuel-economy study was carried out for various models of cars. Each car was driven 100 miles and the number of gallons used were recorded. The car model names and gallons used are stored 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., ChevyC_pgm7 and ChevyC_pgm7.cpp). Display the output on the screen.

Program Design: Your program should define three arrays. Two of the arrays will hold the data read from the pgm7.txt file - the car model names and the gallons used to go 100 miles. The third array will store the miles per gallon for each vehicle as calculated by your program. These three parallel 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. Choose a meaningful name for each function based on what it does. Each function should begin with comments describing the purpose of the function.

1. The model of each car tested and the gallons of gas used to go 100 miles are stored in the file pgm7.txt. Copy this file to your project folder. Write a function to open the file, read in the data and store it in two parallel arrays and close the file. Store 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.

As the data for each car model is read, this function should count it and pass the final count back to the main function. The count of the number of cars read will be passed on to other functions and used to determine how many elements of the array actually contain data (i.e., you will be working with partially-filled arrays). Close the file at the end of 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:

Camry

3.5

Accord

4.4

etc.

2. Write 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, Camry and 3.5 should be stored in element 0 of the string and first double arrays. Calculate the miles per gallon for this vehicle (100/3.5) and store the result in element 0 of the second array of type double. Do the same for each vehicle.

3. Write a function to display each vehicle's model, gallons used and miles per gallon (displayed as a whole number). Include a heading with a title, your name and column headings (see sample on next page).

Vehicle Miles Per Gallon

Your Name, CS2010, Class time

Model Gallons used/100 miles MPG

--------------------------------------

Camry 3.5 29

Accord 4.4 23

etc.

4. Call a function that prompts the user to enter a model name and displays the average miles per gallon for vehicles with that model name. For example, if the user enters "Camry", the function should display the message:

The average miles per gallon for Camry vehicles is xx MPG.

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 (highest to lowest) order by the gallons used/100 miles. Be sure that whenever you switch two elements in the gallons used array to also switch the same pair of elements in the model name and MPG 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 Gallons/100 Miles

Your Name

Model Gallons used/100 miles MPG

--------------------------------------

Mustang 5.3 19

Mustang 5.2 19

etc.

Program Documentation & Style:

1. Declare all constants and variables that your program uses at the beginning of your main function.

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. Before the beginning of each function definition, include a sentence or two explaining the purpose of the function.

3. Use meaningful identifier names. No global variable declarations may be used.

4. Include clear prompts for the user about entering the data.

5. Include clear descriptions of the results when you display them

6. Format your output neatly.

The data in the .txt file:

Camry

3.5

Accord

4.4

Prius

2.1

Kia

2.8

Camry

3.8

Sebring

4.2

Mustang

5.3

Accord

4.1

Prius

2.0

Camry

3.5

Camry

3.7

Mustang

5.2

Accord

4.3

Kia

2.7

Accord

4.2

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!