Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

Question

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 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 gallons per liter. Note: A liter is 0.264179 gallons. An example run of the program is shown below:


Hints:
1. What will your function do? Document that in the comment.
2. What are the parameter(s) to the function? What type? What type is the returned value?
3. What kind of a loop should you use? What’s the minimum number of times the user will go through the
loop?

How to write a function
1) Determine the type and number of parameters
2) Determine the type of the return value
3) Declare the function (usually at the top of the program)
4) Write the function (usually at the bottom of the program)

 

[mingli@polaris:~/TA]$ ./lab4_function
Please enter the number of liters and the number of miles:
10 50
Miles per gallon is : 18.9266
Continue (Y/N) ?
Y
Please enter the number of liters and the number of miles:
5.8 43
Miles per gallon is : 28.0635
Continue (Y/N) ?
Y
Please enter the number of liters and the number of miles:
3.2 36.7
Miles per gallon is : 43.4128
Continue (Y/N) ?
N
[mingli@polaris:~/TA]$ O
expand button
Transcribed Image Text:[mingli@polaris:~/TA]$ ./lab4_function Please enter the number of liters and the number of miles: 10 50 Miles per gallon is : 18.9266 Continue (Y/N) ? Y Please enter the number of liters and the number of miles: 5.8 43 Miles per gallon is : 28.0635 Continue (Y/N) ? Y Please enter the number of liters and the number of miles: 3.2 36.7 Miles per gallon is : 43.4128 Continue (Y/N) ? N [mingli@polaris:~/TA]$ O
Expert Solution
Check Mark
Step 1

Modified Program:

#include <iostream>
using namespace std;

int main(void)
{
const double GALLONS_PER_LITER = 0.264179;


int liters = 0;
double distance = 0.0;

double mpg = 0.0;
do
{
cout << "Please input how many liters of gasoline is in your vehicle: ";
cin >> liters;

cout << "Please input the distance in miles you traveled in your vehicle: ";
cin >> distance;


mpg = distance / (liters * GALLONS_PER_LITER);

 

cout << "Your vehicle's MPG is: " << mpg << endl;

}while(liters >-1);

return 0;
}

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
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education