ADTs, Data Structures, and Problem Solving with C++, 2E Larry R. Nyhoff
Email: Richard@qwconsultancy.com
Chapter 1
Chapter One: Software Development 1.5 Exercises 1. Problem analysis and specification involves determining: - output required to solve problem - input given - other info, such as software usage, feasibility of computer solution System design involves: - selecting appropriate data structures to organize the input data - designing algorithms needed to process the data efficiently Coding and integration involves - selecting appropriate programming language - translating algorithms into well-structured, well-documented, readable code Verification and validation may involve - "walking through the code" or, possibly proving correctness of algorithms - executing the software and correcting errors - modifying algorithms System maintenance may involve - fixing possibly obscure bugs - modifying software to improve performance - adding new features, etc. 2. Introductory programming problems are usually well-defined, simple, clearly stated and solvable. Real world problems are generally not well-defined, may be complex, may not have an obvious method of solution. 3. Top-down (or modular) design: Partition a problem into simpler subproblems, each of which can be considered individually. Repeat this until the subproblems obtained are simple enough t o s o l v e . Object-oriented design (OOD): Identify a collection of objects, each of which consists of data and operations on the data, that model the real-world objects in the problem and that interact to solve the problem. 4. Encapsulation, inheritance, polymorphism. 5. Structured data types are user-defined structures used to collectively organize data in a given problem. Typical examples include arrays, files, stacks, etc.
-1-
Chapter 1
6. An algorithm is a procedure that can be executed by a computer. It must be: • unambiguous (clear what each instruction is intended to do) • simple enough to be done by a computer • finite (definite termination) 7. Pseudocode is shorthand notation for representing programs in texts, articles and design phases. It is a mixture of natural language (English) and features and syntax common to high-level languages. 8. Three control structures: • sequential -- steps performed serially, one after another in established sequence • selection -- one of several alternative actions is selected and executed • repetition -- one or more steps is performed repeatedly 9. Typical student programs are small (< 1000 lines), are used infrequently, and are standalone (not part of a system); errors don't have serious consequences and usually quite easy to find and correct; lifetimes of programs are short and thus require little or no maintenance. Typical real-world programs are large (thousands or millions of lines), used often, and are integrated with other software. Errors may serious — even tragic — results, and may be difficult to find; software may be used for a long period of time and require frequent maintenance. 10. Syntax errors: Incorrect syntax (e.g., misplaced or missing semicolons), misspelled words, missing declarations/ The compiler generates error messages that identify the source and type of error. Run-time errors: Division by zero, integer overflow, missing files, illegal memory access. Execution of the program will be terminated with an error message. Logical errors: Incorrect coding of algorithm instructions, improper initialization of variables (so garbage values are used), errors in the algorithms (e.g., not checking boundary conditions). The program executes, but incorrect results are produced. 11. Program maintenance is required to fix bugs, accommodate new uses of software (changes in environment), improve performance, or incorporate new features. 12. Many examples can be found on the Internet.
-2-
Chapter 2
Chapter Two: ADTs -- C-Style types Exercises 2.2 1. 2. 3. 4. 5. 6.
0000 0000 0110 0011 0001 0100 1010 0000 0000 0000 1111 1111 1111 1111 0000 0001 0000 0100 0000 0000 1111 1100 0000 0000
7. a) 0011 1111 0010 0000 0000 0000 0000 0000 8. a) 0100 0001 1100 1101 0000 0000 0000 0000 9. a) 0100 0001 0110 1100 1000 0000 0000 0000 10. a) 0011 1100 1000 0000 0000 0000 0000 0000 11. a) 0011 1101 1100 1100 1100 1100 1100 1100 b) 0011 1101 1100 1100 1100 1100 1100 1101 12. a) 0100 0000 0000 0000 1010 0011 1101 0111
b) same as (a) b) same as (a) b) same as (a) b) same as (a) b) same as (a)
13. 01000010 01000101 14. 01100010 01100101 15. 01000001 01000010 01001100 01000101 16. 01001110 01101111 00100000 01000111 01101111 00100001 17. 00110001 00110010 00110011 00110100 18. 00110001 00110010 00101110 00110011 00110100 19. 64 20. 28271 21. –16386 22. –16383 23. –26215 24. –21846 25. 3.0 26. 6.0 27. 504.0 28. 1.5 29. 213 × 236 30. –3.375 31. null @ 32. no 33. false true 34. true true -3-
Chapter 2
Exercises 2.3 1. typedef char byte; 2. typedef float Real; typedef float SinglePrecision;
3. enum MonthAbbrev {JAN, FEB, MAR, APR, MAY, JUN,
JUL, AUG, SEP, OCT, NOV, DEC};
4. true 5. true 6. OCT 7. MAR 8. OCT 9. JUN 10. enum Digit {ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE};
11. enum CURRENCY {PENNY = 1, NICKEL = 5, DIME = 10, QUARTER = 25, HALF_DOLLAR = 50, DOLLAR = 100};
Exercises 2.4 1. 1. Specifies how memory is to be allocated for that variable 2. Associates the variable's name with that memory 3. Initializes that memory with values provided in the declaration (if any). 2. double * p1, * p2; 3. p1 = &d1; p2 = &d2;
4. Not possible — i2 is an integer, but p1 can only store addresses of doubles. 5. int * ptr1 = &i1, * ptr2 = &i2;
6. p1 = p2; 7. *ptr1 = *ptr2; 8. double temp = *p1; *p1 = *p2; *p2 = temp;
9. typedef char * CharPointer; -4-