Chapter 3

C++ Programs: Input, Process, Output

Page 75

3.1 The output for entering "baah" for the animal and "sheep" for the noise will be a verse something like:

   Old MacDonald had a farm, Ee-igh, ee-igh, oh!
   And on his farm he had a baah, Ee-igh, ee-igh, oh!
   With a sheep sheep here
   And a sheep sheep there
   ...
If you enter "rooster" and "cock a doddle doo" for the noise the verse will be
   Old MacDonald had a farm, Ee-igh, ee-igh, oh!
   And on his farm he had a rooster, Ee-igh, ee-igh, oh!
   With a cock cock here
   And a cock cock there
   ...

This is because when >> is used to read strings whitespace (spaces, tabs, newlines) separates one string from another. Entering cock-a-doodle-doo fixes the problem.

3.2 No endl is used so that the input appears on the same line as the prompt to the user

    Enter the name of an animal: horse

3.3

#include 

void
Sing(string name)
{
    // code here
}

main()
{
    string name;
    cout << "enter name for birthday song ";
    cin >> name;
    Sing(name);
}

3.4 If cin >> noise is removed, then the variable noise will not have a value. Some gibberish may be printed, or the computer may crash. Using what is called an uninitialized variable can cause problems.


Page 80

3.5 Using the expression

    (ifahr - 32) * (5/9)
causes 0 to be printed because the value of (5/9) is 0 since division of ints truncates the result. When the parentheses aren't used, the left-to-right ordering multiplies by 5 first, then divides by 9 (and truncating only at this last step).

3.6 If the expression used is

   ifahr - 32 * 5 / 9
the result for a value of 40 for ifahr will be be 23. The precedence of * and / is higher than -. This means that first 32 * 5 / 9 is evaluated as 160/9 == 17 then the final value of the expression is 40 - 17 == 23

3.7 If the expression used is

    (ifahr - 32.0) * 5 / 9
the result will be 4.44444 because (ifahr - 32.0) is a double value since 32.0 is a double. A double multiplied by an int yields a double, so 8.0 * 5 / 9 is yields 40.0 / 9 == 4.4444

3.8 To convert Celsius to Fahrenheit a different equation for converting must be used. One solution is shown below.

main()
{
    double cels;
    cout << "enter a celsius temperature ";
    cin >> cels;
    cout << celse " = " << cels * 9 / 5 + 32 << " fahrenheit" << endl;
}

3.9 If the expression 24 * 60 * 60 * days is used instead of the expression days * 24 * 60 * 60 the answer changes because the expression is evaluated from left-to-right. The value of 24 * 60 * 60 is too large for an int, and the microcomputer will not use a long int since the 24 and 60 are ints. The too-large value, multiplied by a long int days, cannot be "fixed". The solution is to multiply by days first so that the expression is a long int value. Since a long int multiplied by an int yields a long int, the "right" value is obtained. It's also possible to use 24L * 60 * 60 * days where the modifier L converts the int to a long int.


Page 90

3.10 Here is a new program that uses the number of slices.

#include 

// find the price of one slice of pizza
// and the price per square inch

void SlicePrice(int radius, double price, int slices)
// compute pizza statistics     
{
    // use # of slices
    
    cout << "sq in/slice = ";
    cout << 3.14159*radius*radius/slices << endl;
    
    cout << "one slice: $" << price/slices << endl;
    
    cout << "$" << price/(3.14159*radius*radius);
    cout << " per sq. inch" << endl;
}

main()
{
    int radius;
    int slices;
    double price;

    cout << "enter radius of pizza ";
    cin >> radius;

    cout << "enter price of pizza ";
    cin >> price;

    cout << "enter number of slices ";
    cin >> slices;

    SlicePrice(radius,price,slices);
}

3.11 If the user enters the diameter, then since the radius is half the diameter, and the function SlicePrice expects the radius, the code below can be used.

main()
{
    int diameter;
    double price;

    cout << "enter diameter of pizza ";
    cin >> diameter;

    cout << "enter price of pizza ";
    cin >> price;

    SlicePrice(diameter/2,price);
}

3.12 The random wind-shear, described in comments, causes the different output each time the program is run. Programs with a "random" component exhibit different behavior each time they are executed.

3.13

main()
{
    Balloon floater;
    
    floater.Ascend(40);
    floater.Cruise(10);
    floater.Ascend(80);
    floater.Cruise(20);
    floater.Descend(0);
}

3.14 Using only the statements below will cause the balloon to rise to 50 meters (the first use of Ascend). The second use of Ascend has no effect because the balloon is already higher than 30 meters.

    montgolfier.Ascend(50);
    montgolfier.Ascend(30);
If the statements are reversed then the balloon will first rise to 30 meters, then will rise 20 meters more to 50 meters. The parameters to the function Ascend specify an absolute altitude, not an altitude relative to the current altitude.