187k views
1 vote
3.16 (Gas Mileage) Drivers are concerned with the mileage obtained by their automobiles. One driver has kept track of several tankfuls of gasoline by recording miles driven and gallons used for each tankful. Develop a program that will input the miles driven and gallons used for each tankful. The program should calculate and display the miles per gallon obtained for each tankful. After processing all input information, the program should calculate and print the combined miles per gallon obtained for all tankfuls. Here is a sample input/output dialog:

1 Answer

0 votes

Answer:

I am writing a C program.

#include <stdio.h> // for using input output functions

#include <stdbool.h> // for using a bool value as data type

int main() { // start of the main() function body

int count=0; //count the number of entries

double gallons, miles, MilesperGallon, combined_avg, sum; //declare variables

while(true) {// takes input gallons and miles value from user and computes avg miles per gallon

printf( "Enter the gallons used (-1 to stop): \\" ); //prompts user to enter value of gallons or enter -1 to stop

scanf( "%lf", &gallons );//reads the value of gallons from user

if ( gallons == -1 ) {// if user enters -1

combined_avg = sum / count; //displays the combined average by dividing total of miles per drives to no of entries

printf( "Combined miles per gallon for all tankfuls: %lf\\", combined_avg ); //displays overall average value

break;} //ends the loop

printf( "Enter the miles driven: \\" ); //if user does not enter -1 then prompts the user to enter value of miles

scanf( "%lf", &miles ); //read the value of miles from user

MilesperGallon = miles / gallons; //compute the miles per gallon

printf( "The miles per gallon for tankful: %lf\\", MilesperGallon ); //display the computed value of miles per gallon

sum += MilesperGallon; //adds all the computed miles per gallons values

count += 1; } } //counts number of tankfuls (input entries)

Step-by-step explanation:

The program takes as input the miles driven and gallons used for each tankful. These values are stored in miles and gallons variables. The program calculates and displays the miles per gallon MilesperGallon obtained for each tankful by dividing the miles driven with the gallons used. The while loop continues to execute until the user enters -1. After user enters -1, the program calculates and prints the combined miles per gallon obtained for all tankful. At the computation of MilesperGallon for each tankful, the value of MilesperGallon are added and stored in sum variable. The count variable works as a counter which is incremented to 1 after each entry. For example if user enters values for miles and gallons and the program displays MilesperGallon then at the end of this iteration the value of count is incremented to 1. This value of incremented for each tankful and then these values are added. The program's output is attached.

3.16 (Gas Mileage) Drivers are concerned with the mileage obtained by their automobiles-example-1
User Jpeltoniemi
by
6.2k points