How Many Widgets?
This programming challenge is from an initial C++ programming course I took in Fall 2007. The Chapter (3) was titled “Expressions and Interactivity”.
3.6: The Yukon Widget Company manufactures widgets that weigh 9.2 pounds each. Write a program that calculates how many widgets are stacked on a pallet, based on the total weight of the pallet. The program should ask the user how much the pallet weighs by itself and with the widgets stacked on it. It should then calculate and display the number of widgets stacked on the pallet.
My solution (with professor instructions to not worry about decimal place specification):
/* Chapter 3 Challenge 6
Written by Kirk Hingsberger
September 18, 2007
How many widgets?
*/
#include
using namespace std;
int main()
{
const float WIDGET_WEIGHT = 9.2;
float palletEmpty, palletLoaded, widgetQty;
cout << "How many pounds does your pallet weigh unloaded?" << endl;
cin >> palletEmpty;
cout << "How many pounds does your pallet weigh loaded?" << endl;
cin >> palletLoaded;
widgetQty = (palletLoaded – palletEmpty) / WIDGET_WEIGHT;
cout << "You must have " << widgetQty << " widgets loaded on your pallet.\n";
return 0;
}
Here is the capture script I turned in for grading:
Script started on Thu 20 Sep 2007 09:50:59 AM MDT
$ cat chap3chlg6.cc
/* Chapter 3 Challenge 6
Written by Kirk Hingsberger
September 18, 2007
How many widgets?
*/
#include
using namespace std;
int main()
{
const float WIDGET_WEIGHT = 9.2;
float palletEmpty, palletLoaded, widgetQty;
cout << "How many pounds does your pallet weigh unloaded?" << endl;
cin >> palletEmpty;
cout << "How many pounds does your pallet weigh loaded?" << endl;
cin >> palletLoaded;
widgetQty = (palletLoaded – palletEmpty) / WIDGET_WEIGHT;
cout << “You must have ” << widgetQty << ” widgets loaded on your pallet.\n”;
return 0;
}
$ g++ -o chap3chlg6 chap3chlg6.cc -s
$ chap3chlg6
How many pounds does your pallet weigh unloaded?
20
How many pounds does your pallet weigh loaded?
500
You must have 52.1739 widgets loaded on your pallet.
$ exit
exit
Script done on Thu 20 Sep 2007 09:51:42 AM MDT










