• No results found

The Phases of Software Development

SOLUTIONS TO SELF-TEST EXERCISES

Solutions to Self-Test Exercises 31

Solutions to Self-Test Exercises

Solutions to Self-Test Exercises

?

1. a) The potential for code reuse. b) The possi-bility of future changes to the program.

2. A function prototype consists of the return type, name, and parameter list, which are all followed by a semicolon.

3. The function returns 7 on July 22, 2009. On both July 30, 2009 and February 1, 2010 the function returns 0 (since July 29, 2009 has already passed).

4. assert (month > 0 && month <=12);

5. Precondition: x >= 0. Postcondition: The return value is the positive square root of x. 6. #include <cmath>

Older compilers may require <math.h>

instead.

7. using namespace std;

8. The modification should change only the con-stants at the top of the program, the function celsius_to_fahrenheit and the call to this function.

9. Stopping early with an error message makes debugging easier.

10. #define NDEBUG should appear before any include directives.

11. Part d is linear (i.e., O(n)); parts f and g are logarithmic (i.e., O(log n)); all of the others are quadratic (i.e.,O(n2)).

12. The only O(n) formula is (d).

13. Worst-case analysis counts the maximum required number of operations for a function.

If the exact count of the number of operations cannot be determined, the number of opera-tions may be estimated, provided that the esti-mate is guaranteed to be higher than the actual number of operations.

14. This is a nested loop in which the number of times the inner loop executes is one more than the value of the outer loop index. The inner loop statements execute n + (n 1) + ... + 2 + 1 times. This sum is n(n + 1)/2 and gives O(n2).

15. n2 + 1; 10n + 10,000; 50 log n; 1,000,000.

16. Here is one implementation of the function:

int sum(int n) // Precondition: n >= 1.

// Postcondition: The value returned is the // sum of all integers from 1 to n.

Our solution uses answer += i, which causes the current value of i to be added to what’s already in answer.

For a time analysis, there are two assign-ment operations (answer = 0 andi = 1). The

<= test is executed n + 1 times (the first n times it is true, and the final time, with i equal ton + 1, it is false). The ++ and += operations are each executed n times. The entire code is O(n).

17. Choose test data for which you know the cor-rect output. Test inputs should include those that are most likely to cause errors.

18. 28, 29, 30, and 31 should be boundary values to account for the number of days in any month. 1 should also be tested as a lower boundary value, and 27 as the biggest number that cannot be the number of days in a month.

To some extent, though, this is a trick ques-tion: Any time that the number of possible inputs to a function is relatively small, we’d suggest that a test program test all possible input values.

19. As always, 0, 1, and 1 are boundary values.

In this problem, 20 (smallest value) and 20 (largest value) are also boundary values, as are 9 and 10 (since the number changes from a single digit to two digits) and 9 and 10. (By the way, this particular problem is small enough that it would be reasonable to test all legal inputs, rather than testing just the bound-ary values.)

20. Make sure that each line of your code is exe-cuted at least once by some of your test data.

If part of your code is sometimes skipped dur-ing execution, make sure that at least one test input skips this part of your code.

21. You should include an empty line (with no characters before the carriage return) and lines with 0, 1, 2, and 3 A’s. Also include a line with 4 A’s (the smallest case with more than three) and a line with more than 4 A’s. For the lines with 1 or more A’s, include lines that have only the A’s, and also include lines that have A’s together with other characters. Also test the case where all the A’s appear at the front or the back of the line.

22. A profiler can ensure that your code is being fully exercised (by printing the count of how many times each line of your code has been executed). Once an error has been noticed, a debugger can help track down the cause of the error by displaying the values of variables while the code executes one line at a time.

33

LEARNING OBJECTIVES

When you complete Chapter 2, you will be able to...

• specify and design new classes using a pattern of information hiding with private member variables, const member functions, and modification member functions.

• write a header file and a separate implementation file for any new class.

• create and use namespaces to organize new classes.

• use your new classes (and at least one STL class) in small test programs.

• use the automatic assignment operator and the automatic copy constructor.

• identify situations in which member functions and constructors can benefit from using default arguments.

• correctly identify and use value parameters, reference parameters, and const reference parameters.

• overload certain binary operators and input/output operators for new classes.

• identify the need for friend functions of a new class and correctly implement such nonmember functions (which are sometimes overloaded operators).

• Use STL classes, such as the pair class, in an application program.

CHAPTER CONTENTS

2.1 Classes and Members 2.2 Constructors

2.3 Using a Namespace, Header File, and Implementation File 2.4 Classes and Parameters

2.5 Operator Overloading

2.6 The Standard Template Library and the Pair Class Chapter Summary

Solutions to SelfTest Exercises Programming Projects

The happiest way to deal with a man is never to tell him anything he does not need to know.

ROBERT A. HEINLEIN Time Enough for Love

A b s t r a c t D a t a Types a n d C + + C l a s s e s

2 A b s t r a c t D a t a Types a n d C + + C l a s s e s

2 c h apte r