Answers to Self-Test Exercises

  1. 1.

        Hello
        Goodbye
        One more time:
        Hello
        End of program.
  2. 2.No, a void function definition need not contain a return statement. A void function definition may contain a return statement, but one is not required.

  3. 3.Omitting the return statement in the function definition for initializeScreen in Display 5.2 would have absolutely no effect on how the program behaves. The program will compile, run, and behave exactly the same. Similarly, omitting the return statement in the function definition for showResults also will have no effect on how the program behaves. However, if you omit the return statement in the function definition for celsius, that will be a serious error that will keep the program from running. The difference is that the functions initializeScreen and showResults are void functions, but celsius is not a void function.

  4. 4.

    #include <iostream>
    void productOut(int n1, int n2, int n3);
    int main()
    {
        using namespace std;
        int num1, num2, num3;
        cout << "Enter three integers: ";
        cin >> num1 >> num2 >> num3;
        productOut(num1, num2, num3);
        return 0;
    }
    void productOut(int n1, int n2, int n3)
    {
        using namespace std;
        cout << "The product of the three numbers "
             << n1 << ", " << n2 << ", and "
             << n3 << " is " << (n1 * n2 * n3) << endl;
    }
  5. 5.These answers are system dependent.

  6. 6.A call to a void function followed by a semicolon is a statement. A call to a function that returns a value is an expression.

  7. 7.

        10 20 30
        1 2 3
        1 20 3
  8. 8.

    An illustration shows a program's output:
  9. 9.

    An illustration shows:
  10. 10.

    void zeroBoth(int& n1, int& n2)
    {
        n1 = 0;
        n2 = 0;
    }
  11. 11.

    void addTax(double taxRate, double& cost)
    {
        cost = cost + ( taxRate/100.0 ) * cost;
    }

    The division by 100 is to convert a percent to a fraction. For example, 10% is 10/100.0 or 1/10th of the cost.

  12. 12.Yes, a function that returns a value can have a call-by-reference parameter. Yes, a function can have a combination of call-by-value and call-by-reference parameters.

  13. 13.No, a function definition cannot appear inside the body of another function definition.

  14. 14.Yes, a function definition can contain a call to another function.

  15. 15.

    void order(int& n1, int& n2);
    //Precondition: The variables n1 and n2 have values.
    //Postcondition: The values in n1 and n2 have been
    //ordered so that n1 <= n2.
  16. 16.

    double sqrt(double n);
    //Precondition: n >= 0.
    //Returns the squareroot of n.

    You can rewrite the second comment line to the following if you prefer, but the previous version is the usual form used for a function that returns a value:

    //Postcondition: Returns the squareroot of n.
  17. 17.The fundamental rule for testing functions is that every function should be tested in a program in which every other function in that program has already been fully tested and debugged. This is a good way to test a function because if you follow this rule, then when you find a bug, you will know which function contains the bug.

  18. 18.A driver program is a program written for the sole purpose of testing a function.

  19. 19.

    #include <iostream>
    void introduction();
    //Postcondition: Description of program is written on
    //the screen.
    int main()
    {
        using namespace std;
        introduction();
        cout << "End of test.\n";
        return 0;
    }
    //Uses iostream:
    void introduction()
    {
        using namespace std;
        cout << "This program determines the retail price for\n"
             << "an item at a Quick-Shop supermarket store.\n";
    }
  20. 20.

    //Driver program for the function addTax.
    #include <iostream>
    void addTax(double taxRate, double& cost);
    //Precondition: taxRate is the amount of sales tax as
    //a percentage and cost is the cost of an item before
    //tax.
    //Postcondition: cost has been changed to the cost of
    //the item after adding sales tax.
    int main()
    {
        using namespace std;
        double cost, taxRate;
        char ans;
        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.precision(2);
        do
        {
            cout << "Enter cost and tax rate:\n";
            cin >> cost >> taxRate;
            addTax(taxRate, cost);
            cout << "After call to addTax\n"
                 << "taxRate is " << taxRate << endl
                 << "cost is " << cost << endl;    
            cout << "Test again?"
                 << " (Type y for yes or n for no): ";
            cin >> ans;
            cout << endl;
        } while (ans == 'y' || ans == 'Y');
        return 0;
    }
    void addTax(double taxRate, double& cost)
    {
        cost = cost + ( taxRate/100.0 )* cost;
    }
  21. 21.A stub is a simplified version of a function that is used in place of the function so that other functions can be tested.

  22. 22.

    //THIS IS JUST A STUB.
    double rainProb(double pressure, double humidity, double temp)
    {
        return 0.25; //Not correct, but good enough for some testing.
    }
  23. 23.assert(z != 0).

  24. 24.A debugger is a tool that allows the programmer to set breakpoints, step through the code line by line, and inspect or modify the value of variables.

  25. 25.Keeping an open mind, adding cout statements to narrow down the cause of the error, using a debugger, searching for common errors, and devising a variety of tests are a few techniques that you can use to debug a program.