Absolute C++
Absolute C++
6th Edition
ISBN: 9780133970784
Author: Walter Savitch, Kenrick Mock
Publisher: Addison-Wesley
bartleby

Videos

Textbook Question
Book Icon
Chapter 3, Problem 1PP

A liter is 0.264179 gallons. Write a program that will read in the number of liters of gasoline consumed by the user’s car and the number of miles traveled by the car and will then output the number of miles per gallon the car delivered. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the number of miles per gallon. Your program should use a globally defined constant for the number of liters per gallon.

Expert Solution & Answer
Check Mark
Program Plan Intro

List of variables:

number_of_miles: Store the number of miles travelled.

liter_of_gas: Store the quantity of gasoline consumed.

List of functions used:

cout.setf(ios::showpoint): For controlling the floating and trailing zeros.

cout.precision(2): Dislay the values up to two digits.

cin(): To take input from input streams like keyboard, files etc.

cout(): To display the output.

Summary Introduction:

Program will use the Main () method to prompt the user to enter the number of miles traveled and the number of gasoline consumed to find the number of miles per gallon car delivered. A liter is equal to 0.264179 gallons.

Program Description:

The purpose of the program is to find the number of miles per gallon car delivered.

Explanation of Solution

Program:

Following is the C++ program to find the number of miles per gallon car delivered.

/*

* Program: The program asks user to provide the number of miles car travelled and the amount of gasoline consumed to find the number of miles per gallon car delivered.

*/

#include <iostream>
Usingnamespacestd;
constdouble LITRE =0.264179;
// A liter is equal to 0.264179 gallons
doublemiles_per_gallons(float,float);
intmain(){
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(2);
charans;
do{
// declare the variables.
Floatnumber_of_miles,liter_of_gas;
cout<<"Enter the number of miles traveled: ";
cin>>number_of_miles;
cout<<"\nEnter the number of liters of gasoline consumed: ";
cin>>liter_of_gas;
cout<<endl<<miles_per_gallons(number_of_miles,liter_of_gas);
cout<<endl<<"Do you want to calculate again? ";
cin>>ans;
}while(ans=='Y'||ans=='y');
cout<<"\nThank You!";
return0;
}
// function to calculate the miles per gallon car delivered.
doublemiles_per_gallons(floatnumber_of_miles,floatliter_of_gasoline){
returnnumber_of_miles/(liter_of_gasoline*LITRE);
}

Explanation:

The above program prompts the user to enter the number of miles traveled and the number of gasoline consumed and after that function miles_per_gallons() is defined to find the number of miles traveled by gasoline consumed in liter. Then cout() function is used to show the output of the program.

Sample Output:

Following is the sample output for the given program:

Enter the number of coupons: 25 
The number of Candy bars: 2 
The number of Gum Balls: 1 
The remaining coupons are: 2

Want to see more full solutions like this?

Subscribe now to access step-by-step solutions to millions of textbook problems written by subject matter experts!
Students have asked these similar questions
Modular Program Structure. Analysis of Structured Programming Examples. Ways to Reduce Coupling. Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsin⁡x Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.
Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsin⁡x Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.
Based on the given problem, create an algorithm and a block diagram, and write the program code: Function: y=xsin⁡x Interval: [0,π] Requirements: Create a graph of the function. Show the coordinates (x and y). Choose your own scale and show it in the block diagram. Create a block diagram based on the algorithm. Write the program code in Python. Requirements: Each step in the block diagram must be clearly shown. The graph of the function must be drawn and saved (in PNG format). Write the code in a modular way (functions and the main part should be separate). Please explain and describe the results in detail.

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
Knowledge Booster
Background pattern image
Computer Science
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
C++ for Engineers and Scientists
Computer Science
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Course Technology Ptr
Text book image
C++ Programming: From Problem Analysis to Program...
Computer Science
ISBN:9781337102087
Author:D. S. Malik
Publisher:Cengage Learning
Text book image
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:9780357392676
Author:FREUND, Steven
Publisher:CENGAGE L
Text book image
Programming Logic & Design Comprehensive
Computer Science
ISBN:9781337669405
Author:FARRELL
Publisher:Cengage
Text book image
Microsoft Visual C#
Computer Science
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Cengage Learning,
Computer Programming for Beginners | Functions, Parameters & Arguments | Ep24; Author: Programming With Avelx;https://www.youtube.com/watch?v=VXlh-qJpfw0;License: Standard YouTube License, CC-BY