Ans_CH07

Embed Size (px)

Citation preview

  • 7/25/2019 Ans_CH07

    1/11

    Chapter 7 Exercises and Answers

    Answers are in blue.

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

    A. Ask !estions". #ook for familiar things

    $. %ivide and con!er

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

    A

    2. Don't reinvent the wheel.B

    3. Strategy used in the binary searh algorithms

    !

    ". #s a solution to a previous problem appropriate for the urrent one$B

    %. Strategy used in the &uisort algorithm

    !

    (. There is an apparent ontradition in the problem statement.A

    For Exercises &-1', match the following phase with its o!tp!t.

    A. Analysis and specification phase

    ". Algorithm development phase

    $. (mplementation phase

    %. )aintenance phase

    ). *oring program

    !

    +. ,one

    D-. roblem statement

    A

    1/. 0eneral solution

    B

    For Exercises 11-1*, match the term with the definition.

    A. (nformation hiding

    ". Abstraction

    $. %ata abstraction

    %. +roced!ral abstraction

    E. $ontrol abstraction

    11. The pratie of hiding the details of a module with the goal of

    ontrolling aess to the details of the moduleA

    12. A model of a omple system that inludes only the details essential tothe viewer

    B

    13. The separation of the logial view of an ation from its implementationD

    1". The separation of the logial view of a ontrol struture from its

    implementation

    2/1( ones 4 Bartlett 5earning 55!6 an Asend 5earning !ompanywww.7blearning.om

    http://www.jblearning.com/http://www.jblearning.com/
  • 7/25/2019 Ans_CH07

    2/11

    8

    1%. The separation of the logial view of data from its implementation

    !

    For Exercises 16 - 6, mark the answers tr!e or false as follows

    A. r!e

    ". False

    1(. !ount9ontrolled loops repeat a speifi number of times.

    A

    1). 8vent9ontrolled loops repeat a speifi number of times.B

    1+. !ount9ontrolled loops are ontrolled by a ounter.

    A1-. 8vent9ontrolled loops are ontrolled by an event.

    A

    2/. An infinite loop is a loop that never terminates.A

    21. 5oops an be nested6 but seletion strutures annot.

    B22. Seletion strutures an be nested6 but loops annot.

    B

    23. All ontrol strutures an be nested.

    A

    2". The s:uare root algorithm used a ount9ontrolled loop.

    B2%. An array is a homogeneous struture6 but a reord is not.

    A

    2(. A reord is a heterogeneous struture6 but an array is not.

    A

    2). A reord is a homogeneous struture; an array is a heterogeneousstruture.

    B

    2+. The bubble sort algorithm involves finding the smallest item in the

    unsorted portion of the array and swapping it with the first unsorteditem.

    B

    2-. &uisort is not always :ui.

    A3/. A binary searh an be applied to both a sorted and unsorted array.

    B

    31. A binary searh is always faster than a linear searh.

    B

    32. A seletion sort puts one more item into its permanent plae at eahiteration.

    A

    33. An insertion sort puts one more item into its plae with respet to the

    already sorted portion.A

    3".

  • 7/25/2019 Ans_CH07

    3/11

    A

    3(. #terative algorithms use *>#58 statements.

    A

    Exercises & / 6& are short-answer !estions.

    3). 5ist the four steps in olya's >ow To Solve #t 5ist.?nderstanding the problem

    Devising a plan!arrying out the plan

    5ooing Ba3+. Desribe the four steps listed in 8erise 3) in your own words.

    8ah student's answer is uni:ue.

    3-. 5ist the problem9solving strategies disussed in this hapter.

    As :uestions

    5oo for familiar things

    Divide and on:uer

    "/. Apply the problem9solving strategies to the following situations.

    Solutions are not uni:ue.A. Buying a toy for your four9year9old ousin.

    Ask questions:

    *hat do four9year olds lie$

    #s he or she into sports$

    *hat stores sell toys$

    *here is a partiular store loated$

    *hat toys does the ousin already have$

    Look for things that are familiar:

    # lied 5inoln 5ogs; would my ousin$

    # lied my red wagon; would my ousin$

    @y ousin is lie his or her mother; what did she play with as a hild$

    Divide and conquer:

    0o to store.0o to toy aisle.

    =ine girl's or boy's toys.

    !hoose one.

    B. Crganiing an awards ban:uet four your soer team.

    Ask questions:

    *here will it be$

    *hen will it be$

    >ow many will be there$

    >ow many trophies will be awarded$

    Look for things that are familiar:

    # organied one last year.

    # organied a fundraiser.

    # was a sout leader.

    # play soer.

    Divide and conquer:

    >ave ane deide on day and time.

    >ave im hoose menu.

    >ave @ary by trophies.

    >ave eremy all people.

    !. Buying a dress or suit for an awards ban:uet at whih you are being honored.

    Ask Questions:

    2/1( ones 4 Bartlett 5earning 55!6 an Asend 5earning !ompanywww.7blearning.om

    http://www.jblearning.com/http://www.jblearning.com/
  • 7/25/2019 Ans_CH07

    4/11

    *hat time of day is the ban:uet$

    *here is the ban:uet being held$

    *hat will others be wearing$

    *hat is my best olor$

    Look for things that are familiar:

    5ast year the award winner wore a blue dress suit.

    5ast year # wore a green suit.# wore a suit when # was honored last year.

    Divide and conquer:

    !hoose the store

    0o to the store

    !hoose possibles from ras

    !hoose one.

    "1. 8amine the solutions in 8erise "/ and determine three things they have in ommon.

    8ah solution inludes data ob7etsE toy6 food6 dress6 suit.

    8ah solution involves hoies or deisions.

    8ah solution involves a ontainer for ob7etsE toy store6 restaurant6 lothing store.

    "2. *hat is an algorithm$

    An algorithm is a set of instrutions for solving a problem in a finite amount of time using a finite

    amount of data."3. *rite and algorithm for the following tass.

    Solutions are not uni:ue.

    A. @aing a peanut butter and 7elly sandwih.

    0et bread

    0et peanut butter

    0et 7elly

    0et nife

    Spread peanut butter on one slie of bread

    Spread 7elly on one slie of bread

    !ombine slies of bread6 peanut butter faing 7elly

    B. 0etting up in the morning.

    Alarm goes off

    >it sleep button

    Alarm goes off

    >it sleep button

    Alarm goes off

    Turn off alarm

    @ove dog

    Throw ba overs

    ut feet over side of the bed

    Stand up

    !. Doing your homewor

    Turn off TF

    Turn off !D

    0et bapaSit at des

    Cpen bapa

    et at

    Cpen boo

    Cpen assignment

    *>#58 more to do

    Solve problem

    et at

    D. Driving home in the afternoon

    2/1( ones 4 Bartlett 5earning 55!6 an Asend 5earning !ompanywww.7blearning.om

    http://www.jblearning.com/http://www.jblearning.com/
  • 7/25/2019 Ans_CH07

    5/11

    =ind ar

    Cpen ar door

    0et into ar

    =asten seat belt

    Start engine

    Turn on radio

    *>#58 not yet homeGeep going

    Turn off engine

    Cpen ar door

    0et out of ar

    !lose ar door

    "". 5ist the three phases of the omputer problem9solving model.

    Algorithm development phase

    #mplementation phase

    @aintenane phase

    "%. >ow does the omputer problem9solving model differ from olya's$

    #n olya's list6 the human eeutes the plan and evaluates the results. #n a omputer solution6 a

    program is written that epresses the plan in a language that the omputer an eeute. The human

    then taes the omputer output and evaluates the results."(. Desribe the steps in the algorithm development phase.

    The algorithm development phase inludes analysis understanding the problem6 proposed

    solution logial se:uene of solution steps6 and testing following algorithm.

    "). Desribe the steps in the implementation phase.

    The implementation phase inludes oding translating the algorithm into a omputer language

    and testing ompiling and running the program.

    "+. Desribe the steps in the maintenane phase.

    The maintenane phase involves using the program and modifying the program to add funtionality

    or orret errors.

    "-. 5oo up a reipe for hoolate brownies in a ooboo and answer the following :uestions.

    A. #s the reipe an algorithm$ ustify your answer.

    Cne author's solution.

    Hes6 the reipe is an algorithm. #f the steps are followed eatly6 brownies are produed.

    B. Crganie the reipe as an algorithm6 using pseudo9ode.

    reheat oven to 3)%/

    ut 2 o unsweetened hoolate in double boiler

    Add 1I2 up butter to hoolate in double boiler

    ut double boiler over moderate flame

    @elt ontents of double boiler

    #58more sugar

    Beat eggs

    add sugar graduallyut ontents of ooled double boiler in bowl

    @i ontents of bowl

    Sift 1I2 up flour and dash of salt

    Stir in flour miture into bowl

    Add 1 teaspoon vanilla to bowl

    Add 1I2 up hopped nuts to bowl

    @i ontents of bowl

    0rease -9inh s:uare pan

    our ontents of bowl into pan

    2/1( ones 4 Bartlett 5earning 55!6 an Asend 5earning !ompanywww.7blearning.om

    http://www.jblearning.com/http://www.jblearning.com/
  • 7/25/2019 Ans_CH07

    6/11

    Set minutes to 2/

    ut pan in oven

    *>#58 minutes not /

    Set minutes to minutes 9 1

    #58 is the only omputing word. #t means repetition.

    D. 5ist the words that have meaning in ooing.

    *ords with meaning in ooing inlude preheat6 add6 double boiler6 melt6 moderate flame6 beat6

    gradually6 mi6 shift6 dash6 hopped6 and grease.

    8. @ae the ooies and tae them to your professor.

    %/. *e said that following a reipe is easier than developing one. 0o to the supermaret and buy a

    vegetable that you have not ooed or eaten before. Tae it home and develop a reipe. *rite up

    your reipe and your riti:ue of the proess. #f it is good6 send it to the authors.

    This is an ativity. ,o answer epeted.

    %1. Desribe the top9down design proess.

    The top9down design proess is harateried by suessive layers of refinement. The top9level

    tass are listed. At eah sueeding level6 the tass from the previous one are further developed.%2. Differentiate between a onrete step and an abstrat step.

    An abstrat step is one in whih further development is needed. A onrete step is one in whih all

    the steps are fully speified.

    %3. *rite a top9down design for the following tass.

    Solutions are not uni:ue.

    A. Buying a toy for your four9year9old ousin.

    0o to store

    !hoose toy

    Buy toy

    0o to store

    !hoose store

    =ind loation

    Tae bus

    !hoose toy

    *al up and down aisles

    ani at hoies

    0rab nearest large stuffed animal

    Buy toy

    0o to ler

    0ive stuffed animal to ler

    0ive redit ard to ler

    Sign redit ard slip

    B. Crganiing an awards ban:uet four your soer team.

  • 7/25/2019 Ans_CH07

    7/11

    Fisit possible hoies

    !hoose one

    @ae reservation

    Send invitations

    0et list of people to invite

    Buy invitationsAddress invitations

    @ail invitations

    Buy trophies

    =ind out how many to buy

    =ind store that arries trophies

    Crder trophies over the phone

    i up trophies

    !. Buying a dress or suit for an awards ban:uet at whih you are being honored.

    0o to favorite store

    !hoose dress or suit that suits you

    ay for hoie0o home

    0o to favorite store

    0et in ar

    Drive to favorite store

    0et out of ar

    *al in to store

    !hoose dress or suit for oasion

    @ae an initial seletion of several

    Try eah one on

    !hoose best

    ay for hoie

    Tae purhase to ashier

    >and the ashier your redit ard

    Sign reeipt

    0o home

    *al to ar

    0et in

    =ind eys

    Start ar

    Drive home

    %". *rite a top9down design for the following tass.

    Solutions are not uni:ue.A. !alulating the average of ten test sores.

    Set ount to /

    Set sum to /

    *>#58 ount K 1/

    0et sore

    Set sum to sum plus sore

    Set ount to ount plus 1

    Set average to sum divided by 1/

    B. !alulating the average of an unnown number of test sores.

    2/1( ones 4 Bartlett 5earning 55!6 an Asend 5earning !ompanywww.7blearning.om

    http://www.jblearning.com/http://www.jblearning.com/
  • 7/25/2019 Ans_CH07

    8/11

    Set ount to /

    Set sum to /

    *>#58 there are more sores

    0et sore

    Set sum to sum plus sore

    Set ount to ount plus 1

    Set average to sum divided by ount!. Desribe the differenes in the two designs.

    The loop in the first design operates eatly 1/ times. The loop in the seond design operates as

    long as there were more sores.

    %%. *rite a top9down design for the following tass.

    Solutions are not uni:ue.

    A. =inding a telephone number in the phone boo.

    =ind the right page

    =ind the right olumn

    Searh the olumn for name

    =ind the right page

    Cpen to approimate part of boo

    *>#58 page not found!ompare name with name on top of right page

    #= name on top is less

    Turn page forward

    85S8

    !ompare name with name on top of left page

    #= name on top is greater

    Turn page baward

    85S8

    age is found

    =ind right olumn

    !urrent olumn is leftmost one

    *>#58 olumn not found

    #= name on bottom of urrent olumn is greater

    !olumn is found

    85S8

    Set urrent olumn to one at right of urrent olumn

    Searh the olumn for name

    Set found to false

    *>#58 more to loo at and not found

    0et net name

    #= name is the one you want

    0et phone number

    Set found to true#= found is false

    ,umber not in boo

    B. =inding a telephone number on the #nternet.

    5og on to #nternet

    0o to favorite searh engine

    Type in J=ind phone numberJ

    0o to first response

    0et phone number

    5og off

    2/1( ones 4 Bartlett 5earning 55!6 an Asend 5earning !ompanywww.7blearning.om

    http://www.jblearning.com/http://www.jblearning.com/
  • 7/25/2019 Ans_CH07

    9/11

    !. =inding a telephone number on a srape of paper that you have lost.

    Searh purses wallets for srap of paper

    Searh waste paper basets for srap of paper

    Searh trash an for srap of paper

    Searh purses wallets

    *>#58 paper not found and there are more purses or wallets0et net one

    #= paper is there

    paper is found

    Searh waste paper basets

    *>#58 paper not found and there are more waste paper basets

    0et net one

    #= paper is there

    paper is found

    D. Desribe the similarities and differenes among these designs.

    The first and third both have a proess repeated a number of times; the seond does not. The first

    and third are proesses that most of us have done physially many times. The first and third involve

    a linear searh through a ontainer of dataE olumns in a boo and purses wallets6 and waste paperbasets.

    %(. Distinguish between information and data.

    #nformation is any nowledge that an be ommuniated. *hen information is in the form that a

    omputer an use6 it is alled data. Thus data is any nowledge that an be ommuniated in a

    form that a omputer an proess.

    %). *rite a top9down design for sorting a list of names into alphabetial order.

    *>#58 more names

    San list for name losest to beginning of the alphabet smallest

    !opy name to new list

    !ross name off original list

    !opy names ba onto original list

    %+. A. *hy is information hiding important$

    #nformation hiding defers details until the level where the details are important. This proess eeps

    an algorithm from being dependent on the implementation details6 whih may hange.

    B. ,ame three eamples of information hiding that you enounter every day.

    Taling on the telephone.

    Driving a ar.

    Turning on the television.

    %-. An airplane is a omple system.

    Solutions are not uni:ue.

    A. 0ive an abstration of an airplane from the view of a pilot.

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

    B. 0ive an abstration of an airplane from the view of a passenger.

    A passenger an view the airplane as the inside of a limousine that is arrying the passenger from

    one plae to another.!. 0ive an abstration of an airplane from the view of the abin rew.

    The abin rew an view an airplane as a dining room.

    D. 0ive an abstration of an airplane from the view of a maintenane mehani.

    A maintenane mehani an view an airplane as a olletion of parts and wires put together

    aording to his or her maintenane diagrams.

    8. 0ive an abstration of an airplane from the view of the airline's orporate offie.

    =rom the view of the boardroom6 the airplane an be viewed as an epensive ob7et used in the

    proess of maing money.

    (/. 5ist the identifiers and whether they named data or ations for the designs in 8erise %3.

    2/1( ones 4 Bartlett 5earning 55!6 an Asend 5earning !ompanywww.7blearning.om

    http://www.jblearning.com/http://www.jblearning.com/
  • 7/25/2019 Ans_CH07

    10/11

    A. AtionsE go6 hoose6 buy6 find6 give6 sign

    DataE store6 toy6 ler6 redit ard

    B. AtionsE rent send6 hoose6 buy6 find6 visit6 mae6 get6 address6 mail6 order6 pi up

    DataE ban:uet room6 invitations6 menu6 trophies6 reservation6 list of people6 phone

    !. AtionsE go hoose pay

    DataE store6 dress6 suit6 hoie6 home

    (1. 5ist the identifiers and whether they named data or ations for the designs in 8erise %".A. AtionsE set6 get

    DataE ount6 sum6 sore6 average

    B. AtionsE set6 get

    DataE ount6 sum6 sore6 average

    (2. 5ist the identifiers and whether they named data or ations for the designs in 8erise %%.

    A. AtionsE find6 searh6 open6 ompare6 turn6 set

    DataE page6 olumn6 name6 boo6 right page6 left page

    B. AtionsE log on6 go6 type6 get

    DataE #nternet6 searh engine6 first response6 phone number

    Exercises 6-6* !se the following array of val!es.

    lengthlist

    L/M L1M L2M L3M L"M L%M L(M L)M L+M L-M L1/M

    11 23 "1 (( 2/ 2 -/ - 3" 1- "/ --

    (3. Show the state of the list when first?nsorted is first set e:ual to the "th item in theseletion sort. Array when first?nsorted is first set to "th item.

    L/M L1M L2M L3M L"M L%M L(M L)M L+M L-M L1/M

    2 - 1- 2/ 23 -/ "1 3" (( "/ --

    (". Show the state of the list when first?nsorted is first set e:ual to the %th item in the

    bubble sort algorithm.Array when first?nsorted is first set e:ual to the %th item.

    L/M L1M L2M L3M L"M L%M L(M L)M L+M L-M L1/M

    2 - 1- 2/ 23 "1 (( 3" "/ -/ --

    (%. Show the state of the list when the first reursive all is made in &uisort using

    listL/M as split value.Array when first reursive all is made.

    L/M L1M L2M L3M L"M L%M L(M L)M L+M L-M L1/M

    2 1- - 2/ 23 -/ (( 3" "1 "/ --

    Exercises 66-6& !se the following array of val!es.

    length

    list

    L/M L1M L2M L3M L"M L%M L(M L)M L+M L-M L1/M

    11 % ) 2/ 33 "" "( "+ "- 1/1 1/2 1/%

    ((. >ow many omparisons does it tae using a se:uential searh to find the following

    values or determine that the item is not in the list$

    A. "

    11

    2/1( ones 4 Bartlett 5earning 55!6 an Asend 5earning !ompanywww.7blearning.om

    http://www.jblearning.com/http://www.jblearning.com/
  • 7/25/2019 Ans_CH07

    11/11

    B. ""

    %

    !. "%11

    D. 1/%

    118. 1/

    11(). >ow many omparisons does it tae using a binary searh to find the following

    values or determine that the item is not in the list$

    A. "

    "

    B. """

    !. "(

    1

    D. 1/%"

    8. 1/("

    2/1( ones 4 Bartlett 5earning 55!6 an Asend 5earning !ompanywww.7blearning.om

    http://www.jblearning.com/http://www.jblearning.com/