计算机概论第七章

Answers are in blue.

Computer Science Illuminated, Seventh Edition

Nell Dale, PhD; John Lewis, PhD

CHAPTER 7

EXERCISES AND ANSWERS

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. The first strategy to use when given a problem A
    2. Don’t reinvent the wheel. B
    3. Strategy used in the binary search algorithms C
    4. Is a solution to a previous problem appropriate for the cur- rent one?B
    1. Strategy used in the Quicksort algorithm C
    2. There is an apparent contradiction in the problem statement. A
    3. 对于练习1–6,请将问题解决策略与定义或示例进行匹配。
    4. A. 提问
    5. B. 寻找熟悉的事物
    6. C. 分而治之
    7. 1. 给定问题时使用的第一种策略 - A
    8. 2. 不要重复造轮子。 - B
    9. 3. 二分查找算法中使用的策略 - C
    10. 4. 先前问题的解决方案是否适用于当前问题? - B
    11. 5. Quicksort 算法中使用的策略 - C
    12. 6. 问题陈述中存在明显矛盾。 - A

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

      1. Analysis and specification phase
      2. Algorithm development phase
      3. Implementation phase
      4. Maintenance phase
    1. Working program C
    2. None D
    3. Problem statement A
    4. 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.

      1. Information hiding
      2. Abstraction
      3. Data abstraction
      4. Procedural abstraction
      5. Control abstraction
      6. 控制抽象
      7. 数据抽象
      8. 信息隐藏
      9. 抽象
      10. 过程抽象
 
    1. The practice of hiding the details of a module with the goal of controlling access to the details of the module

A

    1. A model of a complex system that includes only the details essential to the viewer

B

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

D

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

E

    1. 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:

  1. True
  2. False
    1. Count-controlled loops repeat a specific number of times.

A

    1. Event-controlled loops repeat a specific number of times. B
    2. Count-controlled loops are controlled by a counter. A
    3. Event-controlled loops are controlled by an event. A
    4. An infinite loop is a loop that never terminates. A
    5. Loops can be nested, but selection structures cannot. B
    6. Selection structures can be nested, but loops cannot. B
    7. All control structures can be nested. A
    8. The square root algorithm used a count-controlled loop. B
    9. An array is a homogeneous structure, but a record is not. A
    10. 中文与答案
    1. 计数控制的循环会重复特定次数。A
    2. 事件控制的循环会重复特定次数。B
    3. 计数控制的循环由计数器控制。A
    4. 事件控制的循环由事件控制。A
    5. 无限循环是永远不会终止的循环。A
    6. 循环可以嵌套,但选择结构不能。B
    7. 选择结构可以嵌套,但循环不能。B
    8. 所有控制结构都可以嵌套。A
    9. 求平方根的算法使用了计数控制的循环。B
    10. 数组是一种同质结构,而记录不是。A记录时异构的
    1. A record is a heterogeneous structure, but an array is not. A
    2. A record is a homogeneous structure; an array is a heteroge- neous structure.

B

    1. 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

    1. Quicksort is not always quick. A
    2. A binary search can be applied to both a sorted and unsorted array.

B

    1. A binary search is always faster than a linear search. B
    2. A selection sort puts one more item into its permanent place at each iteration.

A

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

A

    1. Recursion is another name for iteration. B
    2. Recursive algorithms use IF statements. A
    3. Iterative algorithms use WHILE statements. A
    4. 47. 记录是异构结构,而数组不是。A
    5. 48. 记录是同质结构;数组是异构结构。B
    6. 49. 冒泡排序算法涉及在数组的未排序部分中找到最小的项并将其与未排序的第一项交换。B,他在说选择排序
    7. 50. 快速排序并不总是快速的。A
    8. 正确的,在数组几乎已经有序时候快排退化
    9. 51. 二分查找可以应用于有序和无序数组。B,
    10. 52. 二分查找总是比线性查找快。B
    11. 53. 选择排序在每次迭代中将一个以上的项放入其永久位置。A
    12. 54. 插入排序在每次迭代中将一个以上的项放入其相对于已排序部分的位置。A
    13. 55. 递归是迭代的另一种说法。B
    14. 56. 递归算法使用 IF 语句。A
    15. 57. 迭代算法使用 WHILE 语句。A

Exercises 37–67 are short-answer questions.

    1. 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的“如何解决问题”清单包括四个步骤:理解问题,制定计划,执行计划,回顾。

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

Each student’s answer is unique.

理解问题:把握问题的性质,确定已知和需要找到的内容。

制定计划:制定解决问题的策略或方法。

执行计划:执行选择的策略以得出解决方案。

回顾:反思解决方案,验证其正确性,并考虑替代方法

    1. List the problem-solving strategies discussed in this chapter. Ask questions.

Look for familiar things. Divide and conquer.

提问

寻找熟悉的事物

分而治之

    1. Apply the problem-solving strategies to the following situations.

Solutions are not unique.

      1. 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.

      1. 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.

      1. 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.

    1. 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题中的解决方案,确定它们共有的三个要素:

   - 每个解决方案都包含数据对象:玩具、食物、衣服、西装。

   - 每个解决方案涉及选择或决策。

   - 每个解决方案都涉及对象的容器:玩具店、餐厅、服装店。

    1. 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.

    1. Write an algorithm for the following tasks. Solutions are not unique.
      1. Making a peanut butter and jelly sandwich.

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

      1. 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

      1. 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

      1. 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

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

Algorithm development phase Implementation phase Maintenance phase

76. 列出计算机问题解决模型的三个阶段。

算法开发阶段 实施阶段 维护阶段

    1. 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.

    1. 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).
    2. 78. 描述算法开发阶段的步骤。算法开发阶段包括分析(理解问题),提出解决方案(解决步骤的逻辑顺序),和测试(遵循算法)。
    3. 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).

 
    1. 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. 维护阶段的步骤是什么?

维护阶段涉及使用程序并修改程序以添加功能或纠正错误。

    1. Look up a recipe for chocolate brownies in a cookbook and answer the following questions.
      1. Is the recipe an algorithm? Justify your answer. (One author’s solution.)

Yes, the recipe is an algorithm. If the steps are followed exactly, brownies are produced.

      1. 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

      1. List the words that have meaning in computing.

WHILE is the only computing word. It means repetition.

      1. 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.

      1. Make the cookies and take them to your professor.
    1. 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.

    1. 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.

自顶向下的设计过程的特点是逐层细化。首先列出顶层任务。在每个后续层次上,来自上一层次的任务会进一步发展。

    1. 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.

抽象步骤是需要进一步发展的步骤。具体步骤是其中所有步骤都已完全指定的步骤

    1. Write a top-down design for the following tasks. Solutions are not unique.
      1. Buying a toy for your four-year-old cousin.

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

      1. 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

      1. 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

    1. Write a top-down design for the following tasks. Solutions are not unique.
      1. Calculating the average of ten test scores.

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

      1. 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

      1. 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.

    1. Write a top-down design for the following tasks. Solutions are not unique.
      1. Finding a telephone number in the phone book. Find the right page

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

      1. 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

      1. 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

      1. 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.

    1. 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.

    1. 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

    1. 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.

    1. An airplane is a complex system. Solutions are not unique.
      1. Give an abstraction of an airplane from the view of a pilot.

A pilot can view the airplane as a car that he or she drives on a highway of air.

      1. 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.

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

The cabin crew can view an airplane as a dining room.

      1. 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.

      1. 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.

    1. List the identifiers and whether they named data or actions for the designs in Exercise 53.
      1. Actions: go, choose, buy, find, give, sign

Data: store, toy, clerk, credit card

      1. 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

      1. Actions: go, choose, pay

Data: store, dress, suit, choice, home

    1. List the identifiers and whether they named data or actions for the designs in Exercise 54.
      1. Actions: set, get

Data: count, sum, score, average

      1. Actions: set, get

Data: count, sum, score, average

    1. List the identifiers and whether they named data or actions for the designs in Exercise 55.
      1. Actions: find, search, open, compare, turn, set

Data: page, column, name, book, right page, left page

      1. Actions: log on, go, type, get

Data: Internet, search engine, first response, phone number

Exercises 63–65 use the following array of values.

 
  1. 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

 

11

list

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

 
    1. 4

11

    1. 44

23

41

66

20

2

90

9

34

19

40

99

Show the state of the list when firstUnsorted is first set equal to the 4th item in the selection sort. Array when firstUnsorted is first set to 4th item.

[0]

[1]

[2]

[3]

[4]

[5]

[6]

[7]

[8]

[9]

[10]

2

9

19

20

23

90

41

34

66

40

99

    1. 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

    1. 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

    1. 45

11

    1. 105

11

    1. 10

11

  1. 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

4

    1. 44

4

    1. 46

1

    1. 105

4

    1. 106

4

Length

11

5

7

20

33

44

46

48

49

101

102

105

list

[0] [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值