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)

Np Ms Office 365/Excel 2016 I Ntermed
1st Edition
ISBN:9781337508841
Author:Carey
Publisher:Carey
Chapter8: Working With Advanced Functions
Section8.2: Lookup Tables And The Iferror Function
Problem 6QC
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
Np Ms Office 365/Excel 2016 I Ntermed
Np Ms Office 365/Excel 2016 I Ntermed
Computer Science
ISBN:
9781337508841
Author:
Carey
Publisher:
Cengage
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
COMPREHENSIVE MICROSOFT OFFICE 365 EXCE
Computer Science
ISBN:
9780357392676
Author:
FREUND, Steven
Publisher:
CENGAGE L