Answers to Self-Test Exercises

  1. 1.

    int feet = 0, inches = 0;
    int feet(0), inches(0);
  2. 2.

    int count = 0;
    double distance = 1.5;

    Alternatively, you could use

    int count(0);
    double distance(1.5);
  3. 3. sum = n1 + n2;

  4. 4. length = length + 8.3;

  5. 5. product = product * n;

  6. 6. The actual output from a program such as this is dependent on the system and the history of the use of the system.

    #include <iostream>
    using namespace std;
    int main()
    {
        int first, second, third, fourth, fifth;
        cout << first << " " << second << " " << third
             << " " << fourth << " " << fifth << endl;
        return  0;
    }
  7. 7. There is no unique right answer for this one. Below are possible answers:

    1. speed

    2. pay_rate

    3. highest or max_score

  8. 8.

    cout << "The answer to the question of\n"
         << "Life, the Universe, and Everything is 42.\n";
  9. 9.

    cout << "Enter a whole number and press return: ";
    cin >> the_number;
  10. 10.

    cout.setf(ios::fixed);
    cout.setf(ios::showpoint);
    cout.precision(3);
  11. 11.

    #include <iostream>
    using namespace std;
    int main()
    {
         cout << "Hello world\n";
         return 0;
    }
  12. 12.

    #include <iostream>
    using namespace std;
    int main()
    {
        int  n1, n2, sum;
        cout << "Enter two whole numbers\n";
        cin >> n1 >> n2;
        sum = n1 + n2;
        cout << "The sum of " << n1 << " and "
             << n2 << " is " << sum << endl;
        return 0;
    }

  13. 13.

    cout << endl << "\t";
  14. 14.

    #include <iostream>
    using namespace std;
    
    int main( )
    {
        double one(1.0), two(1.414), three(1.732),
               four(2.0),five(2.236);
        cout << "\tN\tSquare Root\n";
        cout << "\t1\t" << one << endl
             << "\t2\t" << two << endl
             << "\t3\t" << three << endl
             << "\t4\t" << four << endl
             << "\t5\t" << five << endl;
        return 0;
    }
  15. 15.

    3 * x
    3 * x + y
    (x + y) / 7 Note that x + y / 7 is not correct.
    (3 * x + y) / (z + 2)
  16. 16.

    bcbc
  17. 17.

    (1/3) * 3 is equal to 0

    Since 1 and 3 are of type int, the / operator performs integer division, which discards the remainder, so the value of 1/3 is 0, not 0.3333. This makes the value of the entire expression 0 * 3, which of course is 0.

  18. 18.

    #include <iostream>
    using namespace std;
    
    int main()
    {
        int number1, number2;
        cout << "Enter two whole numbers: ";
        cin >> number1 >> number2;
        cout << number1 << " divided by " << number2
             << " equals " << (number1/number2) << endl
             << "with a remainder of " << (number1%number2)
             << endl;
        return 0;
    }
  19. 19.

    1. 52.0

    2. 9/5 has int value 1; since numerator and denominator are both int, integer division is done; the fractional part is discarded.

      f = (9.0 / 5) * c + 32.0;

      or this

      f = 1.8 * c + 32.0;
  20. 20. 030406

    The strings are concatenated with the + operator.

  21. 21.

    if (score > 100)
        cout << "High";
    else
         cout << "Low";

    You may want to add \n to the end of these quoted strings depending on the other details of the program.

  22. 22.

    if (savings >= expenses)
    {
        savings = savings - expenses;
        expenses = 0;
        cout << "Solvent";
    }
    else {
        cout << "Bankrupt";
    }

    You may want to add \n to the end of these quoted strings depending on the other details of the program.

  23. 23.

    if ( (exam >= 60) && (programs_done >= 10) )
        cout << "Passed";
    else     cout << "Failed";

    You may want to add \n to the end of these quoted strings depending on the other details of the program.

  24. 24.

    if ( (temperature >= 100) || (pressure >= 200) )
       cout << "Warning";
    else
        cout << "OK";

    You may want to add \n to the end of these quoted strings depending on the other details of the program.

  25. 25.

    (x < −1) || ( x > 2)

  26. 26.

    (1 < x) && (x < 3)
  27. 27.

    1. 0 is false. In the section on type compatibility, it is noted that the int value 0 converts to false.

    2. 1 is true. In the section on type compatibility, it is noted that a nonzero int value converts to true.

    3. −1 is true. In the section on type compatibility, it is noted that a nonzero int value converts to true.

  28. 28.

    10
    7
    4
    1
  29. 29. There would be no output, since the Boolean expression (x < 0) is not satisfied and so the while statement ends without executing the loop body.

  30. 30. The output is exactly the same as it was for Self-Test Exercise 27.

  31. 31. The body of the loop is executed before the Boolean expression is checked, the Boolean expression is false, and so the output is

    −42
  32. 32. With a do-while statement the loop body is always executed at least once. With a while statement there can be conditions under which the loop body is not executed at all.

  33. 33. This is an infinite loop. The output would begin with the following and conceptually go on forever:

    10
    13
    16
    19

    (Once the value of x becomes larger than the largest integer allowed on your computer, the program may stop or exhibit other strange behavior, but the loop is conceptually an infinite loop.)

  34. 34.

    #include <iostream>
    using namespace std;
    
    int main()
    {
         int n = 1;
         while (n <= 20)
         {
            cout << n << endl;
            n++;
        }
        return 0;
    }
  35. 35.

    if (x < 0)
    {
        x = 7;
        cout << "x is now positive.";
    }
    else {
        x = −7;
        cout << "x is now negative.";
    }
  36. 36. The first line is a comment and is not executed. So the entire output is just the following line:

    Self-Test Exercise
  37. 37.

    #include <iostream>
    using namespace  std;
    
    int main()
    {
        const double LITERS_PER_GALLON = 3.78533;
        double  gallons, liters;
    
        cout << "Enter the number of gallons:\n";
        cin >> gallons;
    
        liters = gallons*LITERS_PER_GALLON;
        cout << "There are " << liters << " in "
             << gallons << " gallons.\n";
    
        return  0;
    }