Problem Solving with C++ (9th Edition)
Problem Solving with C++ (9th Edition)
9th Edition
ISBN: 9780133591743
Author: Walter Savitch
Publisher: PEARSON
bartleby

Videos

Textbook Question
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
Write a program to gauge the rate of an employee’s raise from the previous year. The program asks for their annual salary in this year and the previous year. It estimates the hike percentage as the difference in their salary from the previous year divided by the previous year’s salary. Your program should allow the user to repeat this calculation as often as the user wishes. Define a function to compute the hike percentage for an employee. The hike rate should be a value of type double giving the rate as a percentage, for example 5.3 for 5.3%.
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 asks the user how many Fibonacci numbers to generate and then generates them. Make sure to ask the user to enter the number of numbers in the sequence to generate. The Fibonacci sequence is a sequence of numbers where the next number in the sequence is the sum of the previous two numbers in the sequence. The sequence looks like this: 1, 1, 2, 3, 5, 8, 13, § You could assign your input integer to a variable num by num = int(input("How many fib. numbers you want to generate?:")). § You have to consider exceptions, such as the input integer is zero, negative numbers or floats. § Please submit your code and console screenshots to Blackboard. Code containing syntax error will be graded zero. § Example: The prompt asks, “How many fib. numbers you want to generate?

Chapter 4 Solutions

Problem Solving with C++ (9th 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...
Knowledge Booster
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
  • Write a program that asks the user to input a grade that he or she received on an exam. The grade is an integer between 0 and 100 inclusive. The program should convert the numeric grade into the equivalent letter grade. Do the conversion by using a function Letter_Grade () that converts a numeric grade in the range 0 to 100 to the equivalent letter grade. The function should have one argument, the integer grade. The return value of the function should be A if the grade is 90 to 100; B if the grade is 80 to 89; C if the grade is 70 to 79; D if the grade is 65 to 69; and F if the grade is 64 or lower. After converting the grade, the program should display the nu¬meric grade and the equivalent letter grade.
    Write a function that takes a gross salary and calculates the net salary by deducting:      • 7.65% for FICA      • x for federal taxes      • y for state taxes      • z for local taxes Also, deduct $75 for parking fees and $22.50 for a gym membership. The federal tax rate must be between 10 and 37 percent. The state tax rate must be between 3.25 and 11.33 percent. The local tax rate may not be greater than 3%. If any of those values are outside the valid range, return INVALID_PERCENTAGE from your function.   Federal, state, local taxes, and gym membership amounts should be passed in by value. FICA and parking should be a global constant.   COMPUTER LANGUAGE IN C
    Write a program that accepts as input: a.)miles you have driven b.)miles per gallon, car efficiency c.)gas cost per gallon. Your program the invokes the XXX( ) function which computes and returns how many gallons of gas you have consumed to drive those miles. Then your main program continues and computes the cost of the consumed gas and displays that cost. See below. Replace the XXX ( ) function name with whatever name you want and decide about the parameters. Output each floating-point value with two digits after the decimal point, which can be achieved as follows:print(f'{your_value:.2f}')
  • Display the first N magic numbers, where N is a positive number that the user provides as input. Here, a magic number is a number whose sum of its digits eventually leads to 1. For example, 1234 is a magic number because 1 + 2 + 3 + 4 = 10 and 1 + 0 = 1, while 1235 is not (1 + 2 + 3 + 5 = 11 and 1 + 1 = 2). Write a program that prints out the first N magic numbers, seven on each line.   You are required to use the following function prototype:   bool isMagic(int value);  // Returns true if value is a magic number The outline of this function will be as follows: Step 1: Calculate the sum of digits of the value Step 2: Repeat Step 1 until we get a single-digit Step 3: If the resulting sum is equal to 1 then it is a magic number, otherwise not Here is the sample output:
    Given the following:  printf ( “Enter your age in years: “ ); scanf ( “%d”, &age_in_years ); int is_voting_age          = ( age_in_years >= 18 ); int is_drinking_age       = ( age_in_years >= 21 ); int can_be_president    = ( age_in_years >= 35 ); int is_senior_citizen      = ( age_in_years >= 65 ); When the age entered is 33, what are the values of the variables above? When the age entered is 12, what are the values of the variables above? Write an IF statement in a function to print ‘Yes’ for each value above that is true.
    Write a function feet to meters() that changes the values of meter and cm based on the user inputs feet andinches, where meters, cm, feet, and inches are all variables. Embed this function in a loop so the user can keepinputting feet and inches and the program calls feet to meters() to give updated meters and cm.You are going to print the values of meters and cm in the main function so see how ref params work.Note that 1 inch = 2.54 cm
  • Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name (Candidate), the number of votes received (Votes Received), and the percentage of the total votes received by the candidate (% of Total Votes). Your program should also output the winner of the election. Your program must contain at least the following functions: a function to determine the sum of votes. a function to output the winner of the election. A sample output is: Candidate Votes Received % of Total Votes Johnson 5000 25.91 Miller 4000 20.73 Duffy 6000 31.09 ... ... ... Total Votes: 19300   The Winner of the Election is Duffy The program must contain Code Pattern: cout << "Candidate\s+Votes Received\s+% of Total Votes"
    Write a program that allows the user to enter the last names of five candidates in a local election and the number of votes received by each candidate. The program should then output each candidate’s name (Candidate), the number of votes received (Votes Received), and the percentage of the total votes received by the candidate (% of Total Votes). Your program should also output the winner of the election.   Your program must contain at least the following functions: a function to determine the sum of votes. a function to output the winner of the election. A sample output is: Candidate Votes Received % of Total Votes Johnson 5000 25.91 Miller 4000 20.73 Duffy 6000 31.09 ... ... ... Total Votes: 19300
    Write a function to convert temperature from Fahrenheit to Celsius named “Convert F to C” that takes one double value for Fahrenheit as argument, and returns the equivalent temperature in Celsius (type double). Create a program that exercise this function by obtaining a Fahrenheit value from the user, calling the function, and displaying the value of Celsius it returns.   [Formula:  Celsius = (Fahrenheit – 32) * 5/9 ]
    • SEE MORE QUESTIONS
    Recommended textbooks for you
  • C++ for Engineers and Scientists
    Computer Science
    ISBN:9781133187844
    Author:Bronson, Gary J.
    Publisher:Course Technology Ptr
    C++ Programming: From Problem Analysis to Program...
    Computer Science
    ISBN:9781337102087
    Author:D. S. Malik
    Publisher:Cengage Learning
  • C++ for Engineers and Scientists
    Computer Science
    ISBN:9781133187844
    Author:Bronson, Gary J.
    Publisher:Course Technology Ptr
    C++ Programming: From Problem Analysis to Program...
    Computer Science
    ISBN:9781337102087
    Author:D. S. Malik
    Publisher:Cengage Learning
    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