Problem Solving with C++ (10th Edition)
Problem Solving with C++ (10th Edition)
10th Edition
ISBN: 9780134448282
Author: Walter Savitch, Kenrick Mock
Publisher: PEARSON
bartleby

Videos

Textbook Question
Book Icon
Chapter 4, Problem 1P

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
  • Include required header files.
  • Declare and initialize a constant value “lpg = 0.264179”.
  • Define a function named “calc()” to calculate “milage”.
    • Declare a variable “gal”.
    • Compute “gal” and “milage”
    • Function to return “milage”.
  • Define a “main()” function.
    • Declare the variables “lit” and “miles”.
    • Declare a variable “ch”.
    • “do… while” loop to get the user input repeatedly.
      • Get the “lit” and “miles” from the user.
      • Call “calc()” with an arguments “lit” and “miles” values and print the “milage”.
      • Get the user input to repeat the program or not.
    • The input is checked with the condition and repeat or exit the program.

Explanation of Solution

Program to compute number of miles per gallon the car delivered:

// Include required header files

#include <iostream>

using namespace std;

// Assign const value

float const lpg=0.264179;

// Function definition of calc()

float calc(float liters, float miles)

{

  // Declare gal

  float gal;

  // Compute gal

  gal = lpg * liters;

  // Compute milage

  float milage = miles/gal;

  // Return milage

  return(milage);

}

// Function definition of main()

int main()

{

  // Declare lit and miles

  float lit, miles;

  // Declare ch

  char ch;

  // do... while loop

  do{

    // Get the liters

cout<<"\nEnter the number of liters of gasoline: ";

    // Assign the user input to lit

    cin>>lit;

    // Get the miles

cout<<"\nEnter the number of miles Travelled: ";

    // Assign the user input to miles

    cin>>miles;

// Display the miles per gallon the car delivered

cout<<"\nNumber of miles per gallon the car delivered: ";

// Call the function cal() and return the result

    cout<< calc(lit, miles) << endl;

    // Get the user input

    cout<<"\nDo you want to repeat(y/n)??: ";

    // Assign the user input to ch

    cin>>ch;

  // While loop condition to check ch is equal to y

  }while(ch=='y' || ch=='Y');

  // Return 0

  return 0;

}

Sample Output

Enter the number of liters of gasoline:  5

Enter the number of miles Travelled:  30

Number of miles per gallon the car delivered: 22.7119

Do you want to repeat(y/n)??:  n

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
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.
// // Write a function that calculates the amount of money a person would earn over // a period of years if his or her salary is one penny the first day, two pennies // the second day, and continues to double each day. The program should ask the // user for the number of years and call the function which will return the total // money earned in dollars and cents, not pennies. Assume there are 365 days // in a year. // function totalEarned (years) { // Insert your code between here and the next comment block. Do not alter // // any code in any other part of this file. // Insert your code between here and the previous comment block. // any code in any other part of this file. Do not alter // // var years = parseInt(prompt('How many years will you work for pennies a day? ')); var totalDollarsEarned = totalEarned (years); alert('Over a total of ' + years + ', you will have earned $' + totalDollarsEarned);
Write a function to determine the cost of an automobile  insurance premium, based on driver's age and the number of accidents that the driver has had. The basic insurance charge is $500.  There is a surcharge of $100 if  the driver is under 25 and an additional surcharge for accidents: \# of accidents      Accident Surcharge                                     1                           50                                     2                          125                                     3                          225                                     4                          375                                     5                          575                                     6 or more            700 Then call the function to print the result. example (underline denotes input values): ```Age? 26``` ```Accidents? 3``` ```$725```

Chapter 4 Solutions

Problem Solving with C++ (10th Edition)

Ch. 4.3 - Write a function definition for a function called...Ch. 4.3 - Write a function definition for a function called...Ch. 4.3 - Write a function definition for a function isDigit...Ch. 4.3 - Write a function definition for a function...Ch. 4.4 - What is the purpose of the comment that...Ch. 4.4 - Prob. 16STECh. 4.4 - Prob. 17STECh. 4.4 - Carefully describe the process of program testing.Ch. 4.4 - Prob. 19STECh. 4.5 - If you use a variable in a function definition,...Ch. 4.5 - Suppose a function named Function1 has a variable...Ch. 4.5 - The following function is supposed to take as...Ch. 4.5 - Prob. 23STECh. 4.6 - Prob. 24STECh. 4.6 - Prob. 25STECh. 4.6 - Prob. 26STECh. 4.6 - Suppose you have two function definitions with the...Ch. 4.6 - This question has to do with the Programming...Ch. 4.6 - Prob. 29STECh. 4 - A liter is 0.264179 gallons. Write a program that...Ch. 4 - Modify your program from Practice Program 1 so...Ch. 4 - The price of stocks is sometimes given to the...Ch. 4 - Write a program to gauge the rate of inflation for...Ch. 4 - Enhance your program from the previous Practice...Ch. 4 - Write a function declaration for a function that...Ch. 4 - The gravitational attractive force between two...Ch. 4 - Prob. 8PCh. 4 - Prob. 9PCh. 4 - Write a program that computes the annual after-tax...Ch. 4 - Write a program that asks for the users height,...Ch. 4 - Modify your program from Programming Project 2 so...Ch. 4 - Write a program that outputs the lyrics for the...Ch. 4 - To maintain ones body weight, an adult human needs...Ch. 4 - You have invented a vending machine capable of...Ch. 4 - Your time machine is capable of going forward in...Ch. 4 - Do Programming Project 11 from Chapter 3 except...Ch. 4 - Your sports league uses the following lottery...Ch. 4 - Do Programming Project 14 from Chapter 3, the Edoc...

Additional Engineering Textbook Solutions

Find more solutions based on key concepts
When a program is finished using a file, it should do this. a. erase the file b. open the file c. close the fil...

Starting Out with Programming Logic and Design (5th Edition) (What's New in Computer Science)

Assume that the following enumerated data type has been declared: enum Creatures{ HOBBIT, ELF, DRAGON } What wi...

Starting Out with Java: From Control Structures through Data Structures (4th Edition) (What's New in Computer Science)

Employee Updater Write a GUI application that allows the user to look up an employee in the Personnel database ...

Starting Out with Java: From Control Structures through Objects (7th Edition) (What's New in Computer Science)

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
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