Sample Solution from
Absolute C++
6th Edition
ISBN: 9780133970784
Chapter 1
Problem 1PP
Try another sample solution
Textbook Problem

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 one metric ton of cereal.

Expert Solution
Program Plan Intro

List of variables:

ounces: Store the weight of the breakfast meal in ounces.

result: Store the weight in metric tons.

List of functions used:

ceil(): Calculate the ceiling value of the function.

cin(): To take input from input streams like keyboard, files, etc.

cout(): To display the output.

Summary Introduction:

Program will use Main () method to prompt the user to enter the weight of the breakfast meal in ounces and convert it into metric tons and also find the number of boxes needed to yield one metric ton of cereal.

Program Description:

Purpose of program is to find the weight in metric tons and also find the number of boxes needed to yield one metric ton of cereal.

Explanation of Solution

Program:

Following is the C++ program to find the weight in metric tons, and to find the number of boxes needed to yield one metric ton of cereal.

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
 float ounces;
cout<< "Enter the weight of the breakfast meal in ounces: ";
cin>> ounces;
// Conversion from ounces to metric tons 
 float result = ounces/35273.92;
cout<<endl;
cout<< "The weight of the package of breakfast in metric ton is : " << result;
cout<<endl;
cout<< "The number of boxes needed to yield the one metric ton of cereal are: " << ceil(1/result);
cout<<endl;
 return 0;
}

Explanation:

In the above program,the user prompts the weight of the breakfast meal in ouncesand the formula to convert the weight from ounce to metric ton is used. The number of boxes needed is the reciprocal of the weight in metric tons. Then cout() function is used to show the output of the program.

Sample Output:

Following is the sample output for the given program:

Enter the weight of the breakfast meal in ounces: 350 
The weight of the package of breakfast in metric ton is : 0.00992235 
The number of boxes needed to yield the one metric ton of cereal are : 101
Not sold yet?Try another sample solution