Solution to Programming Assignment 2b

Solution to Programming Assignment 2b


////
//
// This program computes the volume and paintable surface area of containers
// having the following four shapes: rectangular, hemispheric, cylindrical, and
// conical.  The containers are available in a range of sizes.
//
// The program inputs two real numbers that specify the base and height
// dimensions of a container.  For a rectangular container, the base is the
// length of one of the square sides of its floor; for the other three shapes,
// the base is the diameter of its floor.  For all four shapes, the height
// measure is how tall the container is from floor to top.
//
// The program outputs eight real numbers for the volume and paintable surface
// areas of each container shape.  The paintable surface area is the total
// surface area both inside and out, minus the area of the bottom (one side) of
// the floor.  For the purposes of these computations, it is assumed that the
// thickness of the walls is negligible, so that both the inside and outside
// surface areas are the same.
//
// Author: Gene Fisher (gfisher@calpoly.edu)
// Created: 15apr99
// Modified: 15apr99
//
////

#include <iostream.h>
#include <math.h>


int main() {
    float base;                 // The generic base length for a container
    float height;               // The base-to-top height of a container

    //
    // Prompt for and input the base length and height dimensions.
    //
    cout << "Input the base and height as real numbers separated by spaces: ";
    cin >> base >> height;
    cout << endl;

    //
    // Output the volume and paintable surface areas of each of the four shapes
    // of container.  The formulae come from CRC Standard Mathematical Tables,
    // 16th Edition.
    //

    //
    // Rectangular shape:
    //     volume = base^2 * height
    //     surface = 2 * base^2 + 4 * base * height
    //     paintable surface = 2 * surface - base^2
    //
    cout << "Volume of rectangular container: "
         << pow(base, 2) * height
         << endl;

    cout << "Paintable surface area of rectangular container: "
         << 2 * (2 * pow(base, 2) + 4 * base * height) - pow(base, 2)
         << endl;

    //
    // Hemispheric shape:
    //     volume = (1/6 * PI * base^3) / 2             [hemispheric shape]
    //              (4/3 * PI * (base / 2)^2 * height)  [hemispheroidal shape]
    //     surface = (PI * base^2) / 2 +                [hemispheric shape]
    //                   1/4 * PI * base^2
    //               (4 * PI * (base / 2) * height) / 2 [hemispheroidal shape]
    //     paintable surface = 2 * surface - 1/4 * PI * base^2
    //
    // NOTE: the following computations are for both pure hemispheric shape and
    // hemispheroidal shape.  See the note at the end of the program for futher
    // explanation.
    //
    cout << "Volume of hemispheric container: "
         << (1.0/6.0 * M_PI * pow(base, 3)) / 2.0
         << " (hemisphere), "
         << endl
         << "                                 "
         << (4/3.0 * M_PI * pow(base / 2, 2) * height) / 2.0
         << " (hemispheroid)"
         << endl;

    cout << "Paintable surface area of hemispheric container: "
         << 2 * (M_PI * pow(base, 2) / 2 + 1/4.0 * M_PI * pow(base, 2)) -
                1/4.0 * M_PI * pow(base, 2)
         << " (hemisphere), "
         << endl
         << "                                                 "
         << 2 * (4 * M_PI * (base / 2 )* height / 2 +
                1/4.0 * M_PI * pow(base, 2)) -
                    1/4.0 * M_PI * pow(base, 2)
         << " (hemispheroid)"
         << endl;

    //
    // Cylindrical shape:
    //     volume = PI * 1/4 * base^2 * height
    //     surface = 2 * PI * (base / 2) * ((base / 2) + height)
    //     paintable surface = 2 * surface - 1/4 * PI * base^2
    //
    cout << "Volume of cylindrical container: "
         << M_PI * 1/4.0 * pow(base, 2) * height
         << endl;

    cout << "Paintable surface area of cylindrical container: "
         << 2 * 2 * M_PI * (base / 2) * ((base / 2) + height) -
                1/4.0 * M_PI * pow(base, 2)
         << endl;

    //
    // Conical shape:
    //     volume = 1/3 * 1/4 * PI * base^2 * height
    //     surface = PI * (base / 2) * ((base / 2) +
    //               sqrt((base / 2)^2 + height^2))
    //     paintable surface = 2 * surface - 1/4 * PI * base^2
    //
    cout << "Volume of conical container: "
         << 1/3.0 * 1/4.0 * M_PI * pow(base, 2) * height
         << endl;

    cout << "Paintable surface area of conical container: "
         << 2 * M_PI * (base / 2) * ((base / 2) +
                sqrt(pow(base / 2, 2) + pow(height, 2))) -
                    1/4.0 * M_PI * pow(base, 2)
         << endl;

    cout << endl;

#ifdef HEMISPHEROIDAL
    cout <<
"    NOTE: you need only output ONE of the two pairs of values output above"
         << endl <<
"    for the hemisphere volume and surface, and either pair of values is "
         << endl <<
"    acceptable.  The (pure) hemisphere values are computed with the height "
         << endl <<
"    fixed at one half the base.  The hemispheroid values are computed with "
         << endl <<
"    the height equal to the second program input."
    << endl << endl;
#endif

    return 0;
}


index | lectures | labs | handouts | assignments | solutions | grades | help