Answers are in blue.
Computer Science Illuminated, Seventh Edition Nell Dale, PhD; John Lewis, PhD |
CHAPTER 7 EXERCISES AND ANSWERS |
- Ask questions
- Look for familiar things
- Divide and conquer
- The first strategy to use when given a problem A
- Don’t reinvent the wheel. B
- Strategy used in the binary search algorithms C
- Is a solution to a previous problem appropriate for the cur- rent one?B
-
- Strategy used in the Quicksort algorithm C
- There is an apparent contradiction in the problem statement. A
- 对于练习1–6,请将问题解决策略与定义或示例进行匹配。
- A. 提问
- B. 寻找熟悉的事物
- C. 分而治之
- 1. 给定问题时使用的第一种策略 - A
- 2. 不要重复造轮子。 - B
- 3. 二分查找算法中使用的策略 - C
- 4. 先前问题的解决方案是否适用于当前问题? - B
- 5. Quicksort 算法中使用的策略 - C
- 6. 问题陈述中存在明显矛盾。 - A
For Exercises 7–10, match the following phase with its output.
-
-
- Analysis and specification phase
- Algorithm development phase
- Implementation phase
- Maintenance phase
- Working program C
- None D
- Problem statement A
- General solution B
-
For Exercises 7–10,将以下阶段与其输出进行匹配。
A. 分析和规范阶段
B. 算法开发阶段
C. 实现阶段
D. 维护阶段
18. 工作程序 - C
19. 无 - D
20. 问题陈述 - A
21. 一般解决方案 - B
For Exercises 11–15, match the term with the definition.
-
-
- Information hiding
- Abstraction
- Data abstraction
- Procedural abstraction
- Control abstraction
- 控制抽象
- 数据抽象
- 信息隐藏
- 抽象
- 过程抽象
-
-
- The practice of hiding the details of a module with the goal of controlling access to the details of the module
A
-
- A model of a complex system that includes only the details essential to the viewer
B
-
- The separation of the logical view of an action from its implementation
D
-
- The separation of the logical view of a control structure from its implementation
E
-
- The separation of the logical view of data from its implementation
C
22. 将模块的细节隐藏起来以控制对模块细节的访问的做法 A
23. 包括仅对观察者至关重要的细节的复杂系统的模型 B
24. 将对一个操作的逻辑视图与其实现分离 D
25. 将控制结构的逻辑视图与其实现分离 E
26. 将数据的逻辑视图与其实现分离 C
For Exercises 16–36, mark the answers true or false as follows:
- True
- False
- Count-controlled loops repeat a specific number of times.
A
-
- Event-controlled loops repeat a specific number of times. B
- Count-controlled loops are controlled by a counter. A
- Event-controlled loops are controlled by an event. A
- An infinite loop is a loop that never terminates. A
- Loops can be nested, but selection structures cannot. B
- Selection structures can be nested, but loops cannot. B
- All control structures can be nested. A
- The square root algorithm used a count-controlled loop. B
- An array is a homogeneous structure, but a record is not. A
- 中文与答案
-
- 计数控制的循环会重复特定次数。A
- 事件控制的循环会重复特定次数。B
- 计数控制的循环由计数器控制。A
- 事件控制的循环由事件控制。A
- 无限循环是永远不会终止的循环。A
- 循环可以嵌套,但选择结构不能。B
- 选择结构可以嵌套,但循环不能。B
- 所有控制结构都可以嵌套。A
- 求平方根的算法使用了计数控制的循环。B
- 数组是一种同质结构,而记录不是。A记录时异构的
-
- A record is a heterogeneous structure, but an array is not. A
- A record is a homogeneous structure; an array is a heteroge- neous structure.
B
-
- The bubble sort algorithm involves finding the smallest item in the unsorted portion of the array and swapping it with the first unsorted item.
B
-
- Quicksort is not always quick. A
- A binary search can be applied to both a sorted and unsorted array.
B
-
- A binary search is always faster than a linear search. B
- A selection sort puts one more item into its permanent place at each iteration.
A
-
- An insertion sort puts one more item into its place with respect to the already sorted portion.
A
-
- Recursion is another name for iteration. B
- Recursive algorithms use IF statements. A
- Iterative algorithms use WHILE statements. A
- 47. 记录是异构结构,而数组不是。A
- 48. 记录是同质结构;数组是异构结构。B
- 49. 冒泡排序算法涉及在数组的未排序部分中找到最小的项并将其与未排序的第一项交换。B,他在说选择排序
- 50. 快速排序并不总是快速的。A
- 正确的,在数组几乎已经有序时候快排退化
- 51. 二分查找可以应用于有序和无序数组。B,
- 52. 二分查找总是比线性查找快。B
- 53. 选择排序在每次迭代中将一个以上的项放入其永久位置。A
- 54. 插入排序在每次迭代中将一个以上的项放入其相对于已排序部分的位置。A
- 55. 递归是迭代的另一种说法。B
- 56. 递归算法使用 IF 语句。A
- 57. 迭代算法使用 WHILE 语句。A
Exercises 37–67 are short-answer questions.
-
- List the four steps in Polya’s “How to Solve It” List. Understanding the problem
Devising a plan Carrying out the plan Looking back
Polya的“如何解决问题”清单包括四个步骤:理解问题,制定计划,执行计划,回顾。
-
- Describe the four steps listed in Exercise 37 in your own words.
Each student’s answer is unique.
理解问题:把握问题的性质,确定已知和需要找到的内容。
制定计划:制定解决问题的策略或方法。
执行计划:执行选择的策略以得出解决方案。
回顾:反思解决方案,验证其正确性,并考虑替代方法
-
- List the problem-solving strategies discussed in this chapter. Ask questions.
Look for familiar things. Divide and conquer.
提问
寻找熟悉的事物
分而治之
-
- Apply the problem-solving strategies to the following situations.
Solutions are not unique.
-
-
- Buying a toy for your four-year-old cousin.
-
Ask questions:
What do four-year-olds like? Is he or she into sports?
What stores sell toys?
Where is a particular store located? What toys does the cousin already have?
Look for things that are familiar:
I liked Lincoln Logs; would my cousin?
I liked my red wagon; would my cousin?
My cousin is like his (or her) mother; what did she play with as a child?
Divide and conquer:
Go to store.
Go to toy aisle.
Find girl’s (or boy’s) toys. Choose one.
-
-
- Organizing an awards banquet four your soccer team.
-
Ask questions:
Where will it be? When will it be?
How many will be there?
How many trophies will be awarded?
Look for things that are familiar:
I organized one last year. I organized a fundraiser. I was a scout leader.
I play soccer.
Divide and conquer:
Have Jane decide on day and time. Have Jim choose menu.
Have Mary buy trophies. Have Jeremy call people.
-
-
- Buying a dress or suit for an awards banquet at which you are being honored.
-
Ask questions:
What time of day is the banquet? Where is the banquet being held? What will others be wearing?
What is my best color?
Look for things that are familiar:
Last year the award winner wore a blue dress (suit). Last year I wore a green suit.
I wore a suit when I was honored last year.
Divide and conquer:
Choose the store. Go to the store.
Choose possibles from racks. Choose one.
-
- Examine the solutions in Exercise 40 and determine three things they have in common.
Each solution includes data objects: toy, food, dress, suit. Each solution involves choices or decisions.
Each solution involves a container for objects: toy store, restaurant, clothing store.
73. 分析第40题中的解决方案,确定它们共有的三个要素:
- 每个解决方案都包含数据对象:玩具、食物、衣服、西装。
- 每个解决方案涉及选择或决策。
- 每个解决方案都涉及对象的容器:玩具店、餐厅、服装店。
-
- What is an algorithm?
An algorithm is a set of instructions for solving a problem in a finite amount of time using a finite amount of data.
-
- Write an algorithm for the following tasks. Solutions are not unique.
- Making a peanut butter and jelly sandwich.
- Write an algorithm for the following tasks. Solutions are not unique.
Get bread
Get peanut butter Get jelly
Get knife
Spread peanut butter on one slice of bread
Spread jelly on one slice of bread
Combine slices of bread, peanut butter facing jelly
-
-
- Getting up in the morning.
-
Alarm goes off Hit sleep button Alarm goes off Hit sleep button Alarm goes off Turn off alarm Move dog
Throw back covers
Put feet over side of the bed Stand up
-
-
- Doing your homework
-
Turn off TV Turn off CD Get backpack Sit at desk Open backpack Pet cat
Open book
Open assignment WHILE (more to do)
Solve problem Pet cat
-
-
- Driving home in the afternoon
-
Find car
Open car door Get into car Fasten seat belt Start engine Turn on radio
WHILE (not yet home) Keep going
Turn off engine Open car door Get out of car Close car door
-
- List the three phases of the computer problem-solving model.
Algorithm development phase Implementation phase Maintenance phase
76. 列出计算机问题解决模型的三个阶段。
算法开发阶段 实施阶段 维护阶段
-
- How does the computer problem-solving model differ from Polya’s?
In Polya’s list, the human executes the plan and evaluates the results. In a computer solution, a program is written that expresses the plan in a language that the computer can exe- cute. The human then takes the computer output and eval- uates the results.
-
- Describe the steps in the algorithm development phase. The algorithm development phase includes analysis (under- standing the problem), proposed solution (logical sequence of solution steps), and testing (following algorithm).
- 78. 描述算法开发阶段的步骤。算法开发阶段包括分析(理解问题),提出解决方案(解决步骤的逻辑顺序),和测试(遵循算法)。
- Describe the steps in the implementation phase.
The implementation phase includes coding (translating the algorithm into a computer language) and testing (compiling and running the program).
-
- Describe the steps in the maintenance phase.
The maintenance phase involves using the program and modifying the program to add functionality or correct errors.
83. 维护阶段的步骤是什么?
维护阶段涉及使用程序并修改程序以添加功能或纠正错误。
-
- Look up a recipe for chocolate brownies in a cookbook and answer the following questions.
- Is the recipe an algorithm? Justify your answer. (One author’s solution.)
- Look up a recipe for chocolate brownies in a cookbook and answer the following questions.
Yes, the recipe is an algorithm. If the steps are followed exactly, brownies are produced.
-
-
- Organize the recipe as an algorithm, using pseudo-code.
-
Preheat oven to 3750
Put 2 oz unsweetened chocolate in double boiler Add 1/2 cup butter to chocolate in double boiler Put double boiler over moderate flame
Melt contents of double boiler Remove double boiler from flame Get a cup of sugar
Put 2 eggs in bowl WHILE (more sugar)
Beat eggs
Add sugar gradually
Put contents of cooled double boiler in bowl Mix contents of bowl
Sift 1/2 cup flour and dash of salt Stir in flour mixture into bowl
Add 1 teaspoon vanilla to bowl Add 1/2 cup chopped nuts to bowl Mix contents of bowl
Grease 9-inch square pan Pour contents of bowl into pan Set minutes to 20
Put pan in oven
WHILE (minutes not 0)
Set minutes to minutes – 1 Remove pan from oven
Cut into 1-1/2” squares Eat
-
-
- List the words that have meaning in computing.
-
WHILE is the only computing word. It means repetition.
-
-
- List the words that have meaning in cooking.
-
Words with meaning in cooking include preheat, add, double boiler, melt, moderate flame, beat, gradually, mix, shift, dash, chopped, and grease.
-
-
- Make the cookies and take them to your professor.
- 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 pro- cess. (If it is good, send it to the authors.)
-
This is an activity. No answer expected.
-
- Describe the top-down design process.
The top-down design process is characterized by successive layers of refinement. The top-level tasks are listed. At each succeeding level, the tasks from the previous one are further developed.
自顶向下的设计过程的特点是逐层细化。首先列出顶层任务。在每个后续层次上,来自上一层次的任务会进一步发展。
-
- Differentiate between a concrete step and an abstract step. An abstract step is one in which further development is needed. A concrete step is one in which all the steps are fully specified.
抽象步骤是需要进一步发展的步骤。具体步骤是其中所有步骤都已完全指定的步骤
-
- Write a top-down design for the following tasks. Solutions are not unique.
- Buying a toy for your four-year-old cousin.
- Write a top-down design for the following tasks. Solutions are not unique.
Go to store Choose toy Buy toy
Go to store
Choose store Find location Take bus
Choose toy
Walk up and down aisles Panic at choices
Grab nearest large stuffed animal Buy toy
Go to clerk
Give stuffed animal to clerk Give credit card to clerk Sign credit card slip
-
-
- Organizing an awards banquet four your soccer team.
-
Rent banquet room Send invitations Choose menu
Buy trophies Rent banquet room
Find what is available Visit possible choices Choose one
Make reservation Send invitations
Get list of people to invite Buy invitations
Address invitations Mail invitations
Buy trophies
Find out how many to buy
Find store that carries trophies Order trophies over the phone Pick up trophies
-
-
- Buying a dress or suit for an awards banquet at which you are being honored.
-
Go to favorite store
Choose dress or suit that suits you Pay for choice
Go home
Go to favorite store Get in car
Drive to favorite store Get out of car
Walk in to store
Choose dress or suit for occasion Make an initial selection of several Try each one on
Choose best Pay for choice
Take purchase to cashier
Hand the cashier your credit card Sign receipt
Go home
Walk to car Get in
Find keys Start car Drive home
-
- Write a top-down design for the following tasks. Solutions are not unique.
- Calculating the average of ten test scores.
- Write a top-down design for the following tasks. Solutions are not unique.
Set count to 0 Set sum to 0
WHILE (count < 10)
Get score
Set sum to sum plus score Set count to count plus 1
Set average to sum divided by 10
-
-
- Calculating the average of an unknown number of test scores.
-
Set count to 0 Set sum to 0
WHILE (there are more scores) Get score
Set sum to sum plus score Set count to count plus 1
Set average to sum divided by count
-
-
- Describe the differences in the two designs.
-
The loop in the first design operates exactly 10 times. The loop in the second design operates as long as there were more scores.
-
- Write a top-down design for the following tasks. Solutions are not unique.
- Finding a telephone number in the phone book. Find the right page
- Write a top-down design for the following tasks. Solutions are not unique.
Find the right column Search the column for name
Find the right page
Open to approximate part of book WHILE (page not found)
Compare name with name on top of right page IF (name on top is less)
Turn page forward ELSE
Compare name with name on top of left page IF (name on top is greater)
Turn page backward ELSE
Page is found Find right column
Current column is leftmost one
WHILE (column not found)
IF (name on bottom of current column is greater) Column is found
ELSE
Set current column to one at right of current column Search the column for name
Set found to false
WHILE (more to look at and not found)
Get next name
IF (name is the one you want) Get phone number
Set found to trueIF (found is false) Number not in book
-
-
- Finding a telephone number on the Internet.
-
Log on to Internet
Go to favorite search engine Type in “Find phone number” Go to first response
Get phone number Log off
-
-
- Finding a telephone number on a scrap of paper that you have lost.
-
Search purses (wallets) for scrap of paper Search wastepaper baskets for scrap of paper Search trash can for scrap of paper
Search purses (wallets)
WHILE (paper not found and there are more purses or wallets) Get next one
IF (paper is there) paper is found
Search wastepaper baskets
WHILE (paper not found and there are more wastepaper baskets) Get next one
IF (paper is there) paper is found
-
-
- Describe the similarities and differences among these designs.
-
The first and third both have a process repeated a num- ber of times; the second does not. The first and third are processes that most of us have done physically many times. The first and third involve a linear search through a container of data: columns in a book, purses (wallets), and wastepaper baskets.
-
- Distinguish between information and data.
Information is any knowledge that can be communicated. When information is in the form that a computer can use, it is called data. Thus, data is any knowledge that can be commu- nicated in a form that a computer can process.
-
- Write a top-down design for sorting a list of names into alphabetical order.
WHILE (more names)
Scan list for name closest to beginning of the alphabet (smallest)
Copy name to new list Cross name off original list
Copy names back onto original list
-
- A. Why is information hiding important?
Information hiding defers details until the level where the details are important. This process keeps an algo- rithm from being dependent on the implementation details, which may change.
B. Name three examples of information hiding that you encounter every day.
Talking on the telephone. Driving a car.
Turning on the television.
-
- An airplane is a complex system. Solutions are not unique.
- Give an abstraction of an airplane from the view of a pilot.
- An airplane is a complex system. Solutions are not unique.
A pilot can view the airplane as a car that he or she drives on a highway of air.
-
-
- Give an abstraction of an airplane from the view of a passenger.
-
A passenger can view the airplane as the inside of a lim- ousine that is carrying the passenger from one place to another.
-
-
- Give an abstraction of an airplane from the view of the cabin crew.
-
The cabin crew can view an airplane as a dining room.
-
-
- Give an abstraction of an airplane from the view of a maintenance mechanic.
-
A maintenance mechanic can view an airplane as a col- lection of parts and wires put together according to his or her maintenance diagrams.
-
-
- Give an abstraction of an airplane from the view of the airline’s corporate office.
-
From the view of the boardroom, the airplane can be viewed as an expensive object used in the process of making money.
-
- List the identifiers and whether they named data or actions for the designs in Exercise 53.
- Actions: go, choose, buy, find, give, sign
- List the identifiers and whether they named data or actions for the designs in Exercise 53.
Data: store, toy, clerk, credit card
-
-
- Actions: rent, send, choose, buy, find, visit, make, get, address, mail, order, pick up
-
Data: banquet room, invitations, menu, trophies, reservation, list of people, phone
-
-
- Actions: go, choose, pay
-
Data: store, dress, suit, choice, home
-
- List the identifiers and whether they named data or actions for the designs in Exercise 54.
- Actions: set, get
- List the identifiers and whether they named data or actions for the designs in Exercise 54.
Data: count, sum, score, average
-
-
- Actions: set, get
-
Data: count, sum, score, average
-
- List the identifiers and whether they named data or actions for the designs in Exercise 55.
- Actions: find, search, open, compare, turn, set
- List the identifiers and whether they named data or actions for the designs in Exercise 55.
Data: page, column, name, book, right page, left page
-
-
- Actions: log on, go, type, get
-
Data: Internet, search engine, first response, phone number
Exercises 63–65 use the following array of values.
- 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?
Length
|
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]
-
- 4
11
-
- 44
|
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] |
2 | 9 | 19 | 20 | 23 | 90 | 41 | 34 | 66 | 40 | 99 |
-
- Show the state of the list when firstUnsorted is first set equal to the 5th item in the bubble sort algorithm. Array when firstUnsorted is first set equal to the 5th item.
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] |
2 | 9 | 19 | 20 | 23 | 41 | 66 | 34 | 40 | 90 | 99 |
-
- Show the state of the list when the first recursive call is made in Quicksort using list[0] as split value.
Array when first recursive call is made.
[0] | [1] | [2] | [3] | [4] | [5] | [6] | [7] | [8] | [9] | [10] |
2 | 19 | 9 | 20 | 23 | 90 | 66 | 34 | 41 | 40 | 99 |
Exercises 66–67 use the following array of values.
5
-
- 45
11
-
- 105
11
-
- 10
11
- 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?
- 4
4
-
- 44
4
-
- 46
1
-
- 105
4
-
- 106
4
Length
|
|
list
[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]