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

Concept explainers

bartleby

Videos

Textbook Question
Book Icon
Chapter 2, Problem 1P

A metric ton is 35,273.92 ounces. Write a program that will read the weight of a package of breakfast cereal in ounces and output the weight in metric tons as well as the number of boxes needed to yield 1 metric ton of cereal. Your program should allow the user to repeat this calculation as often as the user wishes.

Expert Solution & Answer
Check Mark
Program Plan Intro

Weight of breakfast package

Program Plan:

  • Include required header files.
  • Initializes “const” variable “OUNCES_PER_METRICTON” to “35273.92”.
  • Define main function,
    • Declare three variables in “double” datatype.
    • Declare a “char” variable for user option.
    • Then performs “do-while” loop.
      • Prompt statement for read the weight of packages in ounces.
      • Read the weight of packages from user.
      • Compute the weight in metric tons using “ounces/OUNCES_PER_METRICTON” and then store it in a variable “weightMetricTon”.
      • Compute the number of boxes required to produce “1” metric ton using “OUNCES_PER_METRICTON/ounces” and then store it in a variable “no_of_boxes”.
      • Display the weight in metric tons and number of boxes.
      • Prompt statement for read the user option to continue the calculation.
      • Read the user option.
    • Check the user option using “while” loop. If the user entered option is equal to “y” or “Y” then continue the above process.
      • Otherwise, the program terminated.
Program Description Answer

The below program is used to compute the weight of package of breakfast cereal in metric tons and number of boxes from the given weight of package in ounces

Explanation of Solution

Program:

//Header file

#include <iostream>

//For standard input and output

using namespace std;

/* Initializes the const variable "OUNCES_PER_METRICTON" to "35273.92" */

const double OUNCES_PER_METRICTON = 35273.92;

//Define main function

int main()

{

      //Declare variables in type of "double"

      double ounces, weightMetricTon, no_of_boxes;

      //Declare variable in type of "char"

      char option;

      //Check the condition using "do-while" loop

      do

      {

     /* Prompt statement for read the weight of package in ounces */

     cout << "Enter the weight of package of breakfast cereal in ounces: ";

            //Read the weight in ounces from user

            cin>>ounces;

            //Compute the given weight in metric ton

            weightMetricTon = ounces/OUNCES_PER_METRICTON;

     /* Compute the number of boxes required to produce "1" metric ton */

            no_of_boxes = OUNCES_PER_METRICTON/ounces;

            //Display the weight in metric tons

     cout << "Weight of packets in metric tons: " << weightMetricTon << endl;

            //Display the number of boxes

     cout << "Number of boxes needed to yield 1 metric ton of cereal: " << no_of_boxes << endl;

            //Prompt statement for asking the user choice

     cout << "Enter 'y' or 'Y' to continue (OR) Enter any character to terminate: ";

            //Read option from user

            cin >> option;

      }

  /* If the user entered option is "y" or "Y", then continue the above process. Otherwise program terminated */

      while (option == 'y' || option == 'Y');

      return 0;

}

Sample Output

Output:

Enter the weight of package of breakfast cereal in ounces:  4000

Weight of packets in metric tons: 0.113398

Number of boxes needed to yield 1 metric ton of cereal: 8.81848

Enter 'y' or 'Y' to continue (OR) Enter any character to terminate:  y

Enter the weight of package of breakfast cereal in ounces:  4820

Weight of packets in metric tons: 0.136645

Number of boxes needed to yield 1 metric ton of cereal: 7.31824

Enter 'y' or 'Y' to continue (OR) Enter any character to terminate:  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!
08:24
Students have asked these similar questions
Airline companies apply baggage restrictions for their passengers. An airline company has decided to apply a 10kg limitation for passengers' hand luggage and 20kg for their normal baggage. When passengers arrive, they enter their hand and normal luggage weight from the keyboard. If passengers exceed their normal baggage allowance of 10 dollars per gram, they pay 12 dollars per kg. Accordingly, write the program that calculates the baggage price of the airline they will go to according to the baggage values entered by the arriving passenger and keeps this from closing the program for each passenger. Note: If hand and normal baggage allowances are stretched below the maximum value, the payment amount will be considered not negative. An example printout is given on the right. Geri bildirim gönder
Body Mass Index (BMI) is a measure of health on weight. It can be calculated by taking your weight in kilograms and dividing by the square of your height in meters. Write a program which prompts the user to enter the weight in kgs (integer value) and height in feet and inches (both integers) and then calculates the BMI of the person. Your program first need to convert inches to meters (i.e. inches * 0.0254) and then use the following formula to calculate BMI: weight kilogram / (height_meter)^2 Your program displays the BMI value in two decimal places and the corresponding status as shown in the examples below. Note that one inch is 0.0254 meters and 1 foot is 12 inches. For example: Input Result 62 5 10 89 6 0 45 BMI BMI < 18.5 18.5 s BMI < 25.0 25.0 ≤ BMI < 30.0 30.0 ≤ BMI 7 82 5 Interpretation Underweight Normal Overweight Obese 2 Enter weight (kg): Enter height (feet): Enter height (inches): BMI is 19.61, Normal Enter weight (kg): Enter height (feet): Enter height (inches): BMI is…
Transient PopulationPopulations are affected by the birth and death rate, as well as the number of people who move in and out each year. The birth rate is the percentage increase of the population due to births and the death rate is the percentage decrease of the population due to deaths. Write a program that displays the size of a population for any number of years. The program should ask for the following data: The starting size of a population P The annual birth rate (as a percentage of the population expressed as a fraction in decimal form)B The annual death rate (as a percentage of the population expressed as a fraction in decimal form)D The average annual number of people who have arrived A The average annual number of people who have moved away M The number of years to display nYears Write a function that calculates the size of the population after a year. To calculate the new population after one year, this function should use the formulaN = P + BP - DP + A - Mwhere N is the…

Chapter 2 Solutions

Problem Solving with C++ (9th Edition)

Ch. 2.2 - Write a complete C++ program that writes the...Ch. 2.2 - Write a complete C++ program that reads in two...Ch. 2.2 - Prob. 13STECh. 2.2 - Write a short program that declares and...Ch. 2.3 - Convert each of the following mathematical...Ch. 2.3 - Prob. 16STECh. 2.3 - What is the output of the following program lines...Ch. 2.3 - Write a complete C++ program that reads two whole...Ch. 2.3 - Given the following fragment that purports to...Ch. 2.3 - What is the output of the following program lines...Ch. 2.4 - Write an if-else statement that outputs the word...Ch. 2.4 - Suppose savings and expenses are variables of type...Ch. 2.4 - Write an if-else statement that outputs the word...Ch. 2.4 - Write an if-else statement that outputs the word...Ch. 2.4 - Consider a quadratic expression, say x2 x 2...Ch. 2.4 - Consider the quadratic expression x2 4x + 3...Ch. 2.4 - What is the output of the following cout...Ch. 2.4 - What is the output produced by the following (when...Ch. 2.4 - What output would be produced in the previous...Ch. 2.4 - What is the output produced by the following (when...Ch. 2.4 - What is the output produced by the following (when...Ch. 2.4 - What is the most important difference between a...Ch. 2.4 - What is the output produced by the following (when...Ch. 2.4 - Write a complete C++ program that outputs the...Ch. 2.5 - The following if-else statement will compile and...Ch. 2.5 - Prob. 36STECh. 2.5 - Write a complete C++ program that asks the user...Ch. 2 - A metric ton is 35,273.92 ounces. Write a program...Ch. 2 - The Babylonian algorithm to compute the square...Ch. 2 - Many treadmills output the speed of the treadmill...Ch. 2 - Write a program that plays the game of Mad Lib....Ch. 2 - The following is a short program that computes the...Ch. 2 - A government research lab has concluded that an...Ch. 2 - Workers at a particular company have won a 7.6%...Ch. 2 - Modify your program from Programming Project 2 so...Ch. 2 - Negotiating a consumer loan is not always...Ch. 2 - Write a program that determines whether a meeting...Ch. 2 - Prob. 6PPCh. 2 - It is difficult to make a budget that spans...Ch. 2 - You have just purchased a stereo system that cost...Ch. 2 - Write a program that reads in ten whole numbers...Ch. 2 - Modify your program from Programming Project 9 so...Ch. 2 - Sound travels through air as a result of...Ch. 2 - Prob. 12PPCh. 2 - The HarrisBenedict equation estimates the number...Ch. 2 - Write a program that calculates the total grade...Ch. 2 - It is important to consider the effect of thermal...
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
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education
Constants, Variables, Data types, Keywords in C Programming Language Tutorial; Author: LearningLad;https://www.youtube.com/watch?v=d7tdL-ZEWdE;License: Standard YouTube License, CC-BY