SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
Procedural Abstraction And Functions That Return A Value
Exercise:
Programming Projects
Question:1 | ISBN:9780321531346 | Edition: 7

Question

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.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Program code:

#include <iostream>
using namespace std;
double const LITER = 0.264172;
double milesPerGallon(int ml, int lt);

int main ()
{
	char options;
	int lt, ml;
	do 
	{
	  cout << "Enter the number of Liters of gasoline:";
	  cin >> lt;
  cout <<"Enter the number of miles traveled by the car: ";
       cin >> ml;
	   cout << "Number of miles per gallon:"  
            << milesPerGallon(ml, lt) << endl;
		cout << "To continue, then enter 'Y':";
		cin >> options;
	} while (options == 'y' || options == 'Y');
	return 0;
}
double milesPerGallon(int m, int l)
{
	double gallons;
	gallons = LITER * l;
	return (m/gallons);
}

 

Output:

 

Enter the number of Liters of gasoline:100
Enter the number of miles traveled by the car: 1000
Number of miles per gallon:37.8541
To continue, then enter 'Y':n
0 0

Discussions

Post the discussion to improve the above solution.