7.7 Important Threads

In the last two chapters, we have mentioned several topics in passing that are important not only in problem solving but also in computing in general. Let’s review some of the common threads discussed in these chapter.

Information Hiding

We have mentioned the idea of deferring the details several times. We have used it in the context of giving a name to a task and not worrying about how the task is to be implemented until later. Deferring the details in a design has distinct advantages. The designer sees just those details that are relevant at a particular level of the design. This practice, called information hiding, makes the details at a lower level inaccessible during the design of the higher levels.

This practice must seem very strange! Why shouldn’t the details be accessible while the algorithm is being designed? Shouldn’t the designer know everything? No. If the designer knows the low-level details of a module, he or she is more likely to base the module’s algorithm on these details—and it is precisely these low-level details that are more likely to change. If they do, then the entire module has to be rewritten.

Abstraction

Abstraction and information hiding are two sides of the same coin. Information hiding is the practice of hiding details; abstraction is the result with the details hidden. As we said in Chapter 1, an abstraction is a model of a complex system that includes only the details that are essential for the viewer to know. Take, for example, Daisy, the English spaniel. To her owner, she is a household pet; to a hunter, she is a bird dog; and to the vet, she is a mammal. Her owner watches Daisy’s wagging tail, hears her yelp when she gets left outside, and sees the hair she leaves everywhere. The hunter sees a finely trained helper who knows her job and does it well. The vet sees all of the organs, flesh, and bones of which she is composed. See FIGURE 7.17.

Three photographs of the same dog are shown in different views of the same concept as the dog is playing with a ball, the dog is walking on grass, and the skeleton of the dog.

FIGURE 7.17 Different views of the same concept

In computing, an algorithm is an abstraction of the steps needed to implement it. The casual user of a program that includes the algorithm, who sees only the description of how to run the program, is like the dog owner: He or she sees only the surface. The programmer, who incorporates another’s algorithm in her program, is like the hunter who uses the well-trained dog: He or she uses the algorithm for a purpose. The implementer of the algorithm, who must understand it thoroughly, is like the vet: He or she must see the inner workings to implement the algorithm.

In computing, various kinds of abstraction are apparent. Data abstraction refers to the view of data; it is the separation of the logical view of data from its implementation. For example, your bank’s computer may represent numbers in two’s complement or one’s complement, but this distinction is of no concern to you as long as your bank statements are accurate.

Procedural abstraction refers to the view of actions; it is the separation of the logical view of an action from its implementation. For example, when we gave a name to a subprogram, we were practicing procedural abstraction.

A third kind of abstraction in computing is called control abstraction. Control abstraction refers to the view of a control structure; it is the separation of the logical view of a control structure from its implementation. A control structure lets us alter this sequential flow of control of an algorithm. WHILE and IF are control structures. How these control structures are implemented in the languages into which we might translate an algorithm is immaterial to the design of the algorithms.

Abstraction is the most powerful tool people have for managing complexity. This statement is true in computing as well as real life.

Naming Things

When we write algorithms, we use shorthand phrases to stand for the tasks and data with which we are dealing. We give names to data and processes. These names are called identifiers. For example, we used newBase and decimalNumber in the base-changing algorithm. We also gave names to tasks. For example, we used Split to name the task of splitting an array in the Quicksort algorithm. Our identifiers for data values were created from a combination of the words, using uppercase to make the meaning clearer. We left the names of tasks as phrases. Eventually the task names must be converted to a single identifier.

When we get to the stage where we translate an algorithm into a program in a language that a computer can execute, we may have to modify the identifiers. Each language has its own rules about forming identifiers. So there is a two-stage process: Data and actions are given names in the algorithm, and then these names are translated into identifiers that meet the rules of the computer language. Notice that giving identifiers to data and actions is a form of abstraction.

Testing

We have demonstrated testing at the algorithm phase using algorithm walk-throughs. We have shown how to design test plans and implemented one in assembly language. Testing is important at every stage in programming. There are basically two types of testing: clear-box testing, which is based on the code itself, and black-box testing, which is based on testing all possible input values. Often, a test plan incorporates both types of testing.

SUMMARY

Polya, in his classic book How to Solve It, outlined a problem-solving strategy for mathematical problems. This strategy can be applied to all problems, including those for which a computer program is to be written. These strategies include asking questions, looking for familiar things, and dividing and conquering; when these strategies are applied, they should lead to a plan for solving a problem. In computing, such a plan is called an algorithm.

Two categories of loops are distinguished: count controlled and event controlled. A count-controlled loop executes the loop a predetermined number of times. An event-controlled loop executes until an event within the loop changes.

Data comes in two forms: atomic (simple) and composite. An array is a homogeneous structure that gives a name to a collection of items and allows the user to access individual items by position within the structure.

Searching is the act of looking for a particular value in an array. In this chapter we examined the linear search in an unsorted array, the linear search in a sorted array, and the binary search in a sorted array. Sorting is the act of putting the items in an array into some kind of order. The selection sort, bubble sort, insertion sort, and Quicksort are four commonly used sorting algorithms.

Recursive algorithms are algorithms for which the name of a subprogram appears in the subprogram itself. The factorial and binary search are naturally recursive algorithms.

KEY TERMS

EXERCISES

For Exercises 1–6, match the problem-solving strategy with the definition or example.

  1. Ask questions

  2. Look for familiar things

  3. Divide and conquer

  1.   1. The first strategy to use when given a problem

  2.   2. Don’t reinvent the wheel.

  3.   3. Strategy used in the binary search algorithms

  4.   4. Is a solution to a previous problem appropriate for the current one?

  5.   5. Strategy used in the Quicksort algorithm

  6.   6. There is an apparent contradiction in the problem statement.

For Exercises 7–10, match the phase with its output.

  1. Analysis and specification phase

  2. Algorithm development phase

  3. Implementation phase

  4. Maintenance phase

  1.   7. Working program

  2.   8. None

  3.   9. Problem statement

  4. 10. General solution

For Exercises 11–15, match the term with the definition.

  1. Information hiding

  2. Abstraction

  3. Data abstraction

  4. Procedural abstraction

  5. Control abstraction

  1. 11. The practice of hiding the details of a module with the goal of controlling access to the details of the module.

  2. 12. A model of a complex system that includes only details that are essential to the viewer.

  3. 13. The separation of the logical view of an action from its implementation.

  4. 14. The separation of the logical view of a control structure from its implementation.

  5. 15. The separation of the logical view of data from its implementation.

For Exercises 16–36, mark the answers true or false as follows:

  1. True

  2. False

  1. 16. Count-controlled loops repeat a specific number of times.

  2. 17. Event-controlled loops repeat a specific number of times.

  3. 18. Count-controlled loops are controlled by a counter.

  4. 19. Event-controlled loops are controlled by an event.

  5. 20. An infinite loop is a loop that never terminates.

  6. 21. Loops can be nested, but selection structures cannot.

  7. 22. Selection structures can be nested, but loops cannot.

  8. 23. All control structures can be nested.

  9. 24. The square root algorithm uses a count-controlled loop.

  10. 25. An array is a homogeneous structure, but a record is not.

  11. 26. A record is a heterogeneous structure, but an array is not.

  12. 27. A record is a homogeneous structure; an array is a heterogeneous structure.

  13. 28. The bubble sort algorithm involves finding the smallest item in the unsorted portion of the array and swapping it with the first unsorted item.

  14. 29. Quicksort is not always quick.

  15. 30. A binary search can be applied to both a sorted array and an unsorted array.

  16. 31. A binary search is always faster than a linear search.

  17. 32. A selection sort puts one more item into its permanent place at each iteration.

  18. 33. An insertion sort puts one more item into its place with respect to the already sorted portion.

  19. 34. Recursion is another word for iteration.

  20. 35. Recursive algorithms use IF statements.

  21. 36. Iterative algorithms use WHILE statements.

Exercises 37–62 are short-answer questions.

  1. 37. List the four steps in Polya’s How to Solve It list.

  2. 38. Describe the four steps listed in Exercise 37 in your own words.

  3. 39. List the problem-solving strategies discussed in this chapter.

  4. 40. Apply the problem-solving strategies to the following situations.

    1. Buying a toy for your four-year-old cousin

    2. Organizing an awards banquet for your soccer team

    3. Buying a dress or suit for an awards banquet at which you are being honored

  5. 41. Examine the solutions in Exercise 40 and determine three things they have in common.

  6. 42. What is an algorithm?

  7. 43. Write an algorithm for the following tasks.

  1. Making a peanut butter and jelly sandwich

  2. Getting up in the morning

  3. Doing your homework

  4. Driving home in the afternoon

  1. 44. List the phases of the computer problem-solving model.

  2. 45. How does the computer problem-solving model differ from Polya’s problem-solving model?

  3. 46. Describe the steps in the algorithm development phase.

  4. 47. Describe the steps in the implementation phase.

  5. 48. Describe the steps in the maintenance phase.

  6. 49. Look up a recipe for chocolate brownies in a cookbook and answer the following questions.

  1. Is the recipe an algorithm? Justify your answer.

  2. Organize the recipe as an algorithm, using pseudocode.

  3. List the words that have meaning in computing.

  4. List the words that have meaning in cooking.

  5. Make the brownies and take them to your professor.

  1. 50. We said that following a recipe is easier than developing one. Go to the supermarket and buy a vegetable that you have not cooked (or eaten) before. Take it home and develop a recipe. Write up your recipe and your critique of the process. (If it is good, send it to the authors.)

  2. 51. Describe the top-down design process.

  3. 52. Differentiate between a concrete step and an abstract step.

  4. 53. Write a top-down design for the following tasks.

  1. Buying a toy for your four-year-old cousin

  2. Organizing an awards banquet for your soccer team

  3. Buying a dress or suit for an awards banquet at which you are being honored

  1. 54. Write a top-down design for the following tasks.

  1. Calculating the average of ten test scores

  2. Calculating the average of an unknown number of test scores

  3. Describing the differences in the two designs

  1. 55. Write a top-down design for the following tasks.

  1. Finding a telephone number in the phone book

  2. Finding a telephone number on the Internet

  3. Finding a telephone number on a scrap of paper that you have lost

  4. Describe the differences among these designs.

  1. 56. Distinguish between information and data.

  2. 57. Write a top-down design for sorting a list of names into alphabetical order.

  3. 58.

  1. Why is information hiding important?

  2. Name three examples of information hiding that you encounter every day.

  1. 59. An airplane is a complex system.

  1. Give an abstraction of an airplane from the view of a pilot.

  2. Give an abstraction of an airplane from the view of a passenger.

  3. Give an abstraction of an airplane from the view of the cabin crew.

  4. Give an abstraction of an airplane from the view of a maintenance mechanic.

  5. Give an abstraction of an airplane from the view of the airline’s corporate office.

  1. 60. List the identifiers and indicate whether they named data or actions for the designs in Exercise 53.

  2. 61. List the identifiers and indicate whether they named data or actions for the designs in Exercise 54.

  3. 62. List the identifiers and indicate whether they named data or actions for the designs in Exercise 55.

Exercises 63–65 use the following array of values.

An array named ‘list’ of length 11 from the index [0] to [10] reads from left to right: 23, 41, 66, 20, 2, 90, 9, 34, 19, 40, and 99.
  1. 63. Show the state of the list when firstUnsorted is first set equal to the fourth item in the selection sort.

  2. 64. Show the state of the list when firstUnsorted is first set equal to the fifth item in the bubble sort algorithm.

  3. 65. Show the state of the list when the first recursive call is made in Quicksort using list[0] as the split value.

Exercises 66 and 67 use the following array of values.

An array named ‘list’ of length 11 from the index [0] to [10] reads from left to right: 5, 7, 20, 33, 44, 46, 48, 99, 101, 102, and 105.
  1. 66. How many comparisons does it take using a sequential search to find the following values or determine that the item is not in the list?

  1. 4

  2. 44

  3. 45

  4. 105

  5. 106

  1. 67. How many comparisons does it take using a binary search to find the following values or determine that the item is not in the list?

  1. 4

  2. 44

  3. 46

  4. 105

  5. 106

THOUGHT QUESTIONS

  1. Top-down design creates scaffolding that is used to write a program. Is all of this scaffolding just a waste of effort? Is it ever used again? Of what value is it after the program is up and running?

  2. Which of the problem-solving strategies do you use the most? Can you think of some others that you use? Would they be appropriate for computing problem solving?

  3. There are several common examples of open-source software that many people use in their everyday lives. Can you name any?

  4. Do you believe that the quality of an open-source software product is likely to be higher or lower than the quality of software produced by a large corporation? How do you think technical support for open-source software compares to that for proprietary software?

  5. Daniel Bricklin (whose biography appears in Chapter 12) did not patent or copyright his software, believing that software should not be proprietary. As a result, he lost a great deal of money in the form of possible royalties. Do you consider his actions to be visionary or naive?

  6. The Free Software Foundation is a tax-exempt charity that raises funds for work on the GNU Project. GNU software is free. Read about its philosophy on the Web. Compare GNU products with those of manufacturers such as Microsoft and Sun.

  7. If you were to continue with computing and become a programmer, which side of the argument would you take: Should software be copyrighted or should it be free?

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.236.174