fork download
  1. //Devin Scheu CS1A Chapter 3, P. 144, #6
  2. //
  3. /**************************************************************
  4. *
  5. * CALCULATE NUMBER OF WIDGETS ON PALLET
  6. * ____________________________________________________________
  7. * This program calculates the number of widgets stacked on a
  8. * pallet based on the pallet's weight with and without widgets.
  9. * Each widget weighs 9.2 pounds.
  10. *
  11. * Computation is based on the formula:
  12. * widgetCount = (totalWeight - palletWeight) / 9.2
  13. * ____________________________________________________________
  14. * INPUT
  15. * palletWeight : Weight of the pallet alone (in pounds, as a double)
  16. * totalWeight : Total weight of pallet with widgets (in pounds, as a double)
  17. *
  18. * PROCESSING
  19. * widgetCount : Number of widgets stacked on the pallet (as an integer)
  20. *
  21. * OUTPUT
  22. * widgetCount : The calculated number of widgets
  23. *
  24. **************************************************************/
  25.  
  26. #include <iostream>
  27. #include <iomanip>
  28. #include <string>
  29.  
  30. using namespace std;
  31.  
  32. int main () {
  33.  
  34. //Variable Declarations
  35. double palletWeight; //INPUT - Weight of the pallet alone (in pounds, as a double)
  36. double totalWeight; //INPUT - Total weight of pallet with widgets (in pounds, as a double)
  37. int widgetCount; //PROCESSING - Number of widgets stacked on the pallet (as an integer)
  38.  
  39. //Prompt for Input
  40. cout << "Enter the weight of the pallet alone (in pounds): ";
  41. cin >> palletWeight;
  42. cout << "\nEnter the total weight of the pallet with widgets (in pounds): ";
  43. cin >> totalWeight;
  44.  
  45. //Calculate Number of Widgets
  46. widgetCount = static_cast<int>((totalWeight - palletWeight) / 9.2 + 0.5); // Rounding to nearest integer
  47.  
  48. //Output Result
  49. cout << fixed << setprecision(0);
  50. cout << "\n";
  51. cout << left << setw(25) << "Pallet Weight:" << right << setw(15) << palletWeight << " pounds" << endl;
  52. cout << left << setw(25) << "Total Weight:" << right << setw(15) << totalWeight << " pounds" << endl;
  53. cout << left << setw(25) << "Number of Widgets:" << right << setw(15) << widgetCount << endl;
  54.  
  55. } //end of main()
Success #stdin #stdout 0s 5320KB
stdin
200
500
stdout
Enter the weight of the pallet alone (in pounds): 
Enter the total weight of the pallet with widgets (in pounds): 
Pallet Weight:                       200 pounds
Total Weight:                        500 pounds
Number of Widgets:                    33