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)

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
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
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;
}

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps with 1 images

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