PSEUDOCODE AND ALGORITHM DEVELOPMENT
Description of problem:
The U.S. post office has rules about mailing packages. A package cannot be mailed first class if the sum of its length and girth is greater than 100 inches, or if the package weighs more than 70 pounds. The girth is the perimeter around the height and width, where the length is defined as the longest of the three dimensions.
Write a program that takes in the weight of the package and the three dimensions of the package in any order. The program should determine the longest dimension of the package, calculate the girth, and compute the size of the box. The program should then print out one of the following messages about this package:
1. Package is too large and too heavy.
2. Package is too large.
3. Package is too heavy.
4. Package is acceptable.
Development of pseudocode:
Stepwise refinement 1 - Overall sections of this problem:
Get data from user
Solve math
Print answer
Stepwise refinement 2 - More detailed pseudocode version:
Prompt user for three dimensions
Prompt user for weight
Determine longest of three dimensions
Calculate the girth using the other two dimensions
If package is too big and too heavy, print appropriate message
else if package is too big, print appropriate message
else if package is too heavy, print appropriate message
else print package is acceptable
Stepwise refinement 3 - Determining longest of three dimensions:
My strategy is to end up with dim1 holding the largest value
Compare dim2 and dim1, if dim2 is greater, swap dim1 and dim2, dim1 will be holding largest value
Compare dim3 and dim1, if dim3 is greater, swap dim1 and dim3, dim1 is still holding largest value
Dim1 has largest value, compute math for package
Source code answer for mail problem:
// mail.cpp
#include <iostream.h> #include <iomanip.h> #include <bool.h>
void dataInput (int&, int&, int&, int&); void printAnswer (int, int, int, int); void swap (int&, int&);
main () { int weight, dim1, dim2, dim3;
dataInput (weight, dim1, dim2, dim3); printAnswer (weight, dim1, dim2, dim3); return 0; } |
void dataInput (int &wt, int &dim1, int &dim2, int &dim3)
/* Asks user for data: weight and 3 dimensions. Will return weight
and the 3 dimensions in this order: dim1 = length, dim2 and dim3
contain other 2 dimensions. */
{
cout << "Enter the weight ---> ";
cin >> wt;
cout << "Enter 3 dimensions separated by spaces ---> ";
cin >> dim1 >> dim2 >> dim3;
if (dim2 > dim1)
swap (dim2, dim1); // if dim2 is bigger, move into dim1
if (dim3 > dim1)
swap (dim3, dim1); // if dim3 is bigger, move into dim1
cout << endl << endl;
}
void printAnswer (int wt, int length, int width, int height)
// prints out answers
{
int total = length + (width*2) + (height*2);
bool tooLarge = (total > 100);
bool tooHeavy = (wt > 70);
cout << "Weight = " << wt << " lbs; ";
cout << "Length = " << length << " Other two dimensions = ";
cout << setw(4) << width << setw(4) << height << endl;
cout << " Package is ";
if (tooLarge && tooHeavy)
cout << "too large and too heavy" << endl;
else if (tooLarge && !tooHeavy)
cout << "too large" << endl;
else if (!tooLarge && tooHeavy)
cout << "too heavy" << endl;
else if (!tooLarge && !tooHeavy)
cout << "acceptable" << endl;
cout << endl;
}
void swap (int &a, int&b) { int temp = a; a = b; b = temp; }
|