Practice Programs

Practice Programs can generally be solved with a short program that directly applies the programming principles presented in this chapter.

  1. 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 1 metric ton of cereal. Your program should allow the user to repeat this calculation as often as the user wishes.

  2. The Babylonian algorithm to compute the square root of a number n is as follows:

    1. Make a guess at the answer (you can pick n/2 as your initial guess).

    2. Compute r = n / guess

    3. Set guess = (guess + r) / 2

    4. Go back to step 2 for as many iterations as necessary. The more that steps 2 and 3 are repeated, the closer guess will become to the square root of n.

    Write a program that inputs a double for n and iterates through the Babylonian algorithm 100 times. For a more challenging version, iterate until guess is within 1% of the previous guess, and outputs the answer as a double.

  3. Many treadmills output the speed of the treadmill in miles per hour (mph) on the console, but most runners think of speed in terms of a pace. A common pace is the number of minutes and seconds per mile instead of mph.

    Write a program that starts with a quantity in mph and converts the quantity into minutes and seconds per mile. As an example, the proper output for an input of 6.5 mph should be 9 minutes and 13.8 seconds per mile. If you need to convert a double to an int, which will discard any value after the decimal point, then you may use

    intValue = static_cast<int>(dblVal);
  4. Write a program that plays the game of Mad Lib. Your program should prompt the user to enter the following strings:

    • The first or last name of your instructor

    • Your name

    • A food

    • A number between 100 and 120

    • An adjective

    • A color

    • An animal

    After the strings are input, they should be substituted into the story below and output to the console.

    Dear Instructor [Instructor Name],

    I am sorry that I am unable to turn in my homework at this time. First, I ate a rotten [Food], which made me turn [Color] and extremely ill. I came down with a fever of [Number 100-120]. Next, my [Adjective] pet [Animal] must have smelled the remains of the [Food] on my homework, because he ate it. I am currently rewriting my homework and hope you will accept it late.

    Sincerely,

    [Your Name]

  5. The following is a short program that computes the volume of a sphere given the radius. It will compile and run, but it does not adhere to the program style recommended in Section 2.5. Rewrite the program using the style described in the chapter for indentation, adding comments, and appropriately named constants.

    #include <iostream>
    using namespace std;
    int main() 
    {
        double  radius, vm;
        cout << "Enter radius of a sphere." << endl; cin >> radius;
        vm = (4.0 / 3.0) * 3.1415 * radius * radius * radius;
        cout << " The volume is " << vm << endl;
        return  0;
    }