2 solutions to assigned problems

Upload: adewumiezekiel

Post on 05-Jul-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/16/2019 2 Solutions to Assigned Problems

    1/16

    Programming Logic and Design, 6e Solutions 2-1

    Programming Logic and Design, 6th Edition

    Chapter 2

    Exercises

    1. Explain why each o the ollowing names does or does not seem li!e a good "aria#lename to you.

     Answer $%nswers will "ary. % possi#le solution$a. c  & 'alid, #ut pro#a#ly too

    short to #e descripti"e #. cost  & (oodc. costAmount  & (ood, #ut redundant

    d. cost amount  & )n"alid, spaces aren*tallowed

    e. cstofdngbsns  & 'alid, #ut diicult to read. costOfDoingBusinessThisFiscalYear  & 'alid, #ut long and

    aw!wardg. costYear2012  & (oodh. 2012YearCost  & )n"alid, cannot start with

    a digit1+2. ) myAge and yourRate are numeric "aria#les, and deartment!ame is a string

    "aria#le, which o the ollowing statements are "alid assignments ) a statement is

    not "alid, explain why not.

     Answer $a. myAge " 2#  & 'alid

     #. myAge " yourRate  & 'alidc. myAge " deartment!ame  & )n"alid, cannot assign string to numericd. myAge " $deartment!ame%  & )n"alid, cannot assign string to numerice. &2 " myAge  & )n"alid, cannot assign a "alue to a literal

    constant. yourRate " #'(  & 'alidg. yourRate " myAge  & 'alid

    h.yourRate " deartment!ame

     & )n"alid, cannot assign string to numerici. )'*1 " yourRate  & )n"alid, cannot assign a "alue to a literalconstant

     . deartment!ame " +ersonnel  & )n"alid, literal string must #e in uotes!. deartment!ame " $+ersonnel%  & 'alidl. deartment!ame " &1#  & )n"alid, literal string must #e in uotesm

    .

    deartment!ame " $&1#%  & 'alid

  • 8/16/2019 2 Solutions to Assigned Problems

    2/16

    Programming Logic and Design, 6e Solutions 2-2

    n. deartment!ame " myAge  & )n"alid, cannot assign numeric to stringo. deartment!ame " yourRate  & )n"alid, cannot assign numeric to string p. &1# " deartment!ame  & )n"alid, cannot assign a "alue to a literal

    constant. $&1#% " deartment!ame  & )n"alid, cannot assign a "alue to a literal

    constant

    /. %ssume that cost " 10 and rice " 12. 0hat is the "alue o each o the

    ollowing expressions

     Answer $a. rice , cost - 2 -

     #. 1( . rice , # - 2 21

    c. /rice . cost - # 66

    d. & , # - 2 . cost

    e. cost - //rice , . ( . 100 1

    3

    4. Draw a typical hierarchy chart or a paychec!-producing program. 5ry to thin! o atleast 13 separate modules that might #e included. or example, one module mightcalculate an employee*s dental insurance premium.

     Answer $

    7. a. Draw the hierarchy chart and then plan the logic or a program or the salesmanager o 5he 8ouch Potato urniture 8ompany. 5he manager needs a program todetermine the proit on any item sold. )nput includes the wholesale price and retail price or an item. 5he output is the item*s proit, which is the retail price minus thewholesale price. 9se three modules. 5he main program declares glo#al "aria#les and

    compute(ross:;

     print8hec!:;calculate8hec!:;getData:;

    main:;

    compute'oluntaryDeductions:;compute

  • 8/16/2019 2 Solutions to Assigned Problems

    3/16

    main program

    house!eeping:; detail:; end=>o#:;

    Programming Logic and Design, 6e Solutions 2-/

    calls house!eeping, detail, and end-o-o# modules. 5he house!eeping module prompts or and accepts a wholesale price. 5he detail module prompts or and acceptsthe retail price, computes the proit, and displays the result. 5he end-o-o# moduledisplays the message ?5han!s or using this program@.

     Answer $ % sample solution is as ollows$

    a. Hierarchy chart$

    Flowchart:

    Pseudocode:

    startDeclarations

    num holesale+ricenum retail+rice

  • 8/16/2019 2 Solutions to Assigned Problems

    4/16

    main program

    house!eeping:; detailLoop:; end=>o#:;

    Programming Logic and Design, 6e Solutions 2-4

    num rofitstring 34O567+RO8+T " $6nter the holesale rice%string R6TA957+RO8+T " $6nter the retail rice%string 6!D759!6 " $Than:s for using this rogram%

    house:eeing/detail/endOf;ob/

    sto

    house:eeing/outut 34O567+RO8+Tinut holesale+rice

    return

    detail/outut R6TA957+RO8+Tinut retail+ricerofit " retail+rice < holesale+riceoutut rofit

    return

    endOf;ob/outut 6!D759!6

    return

     #. Ae"ise the proit-determining program so that it runs continuously or any num#ero items. 5he detail loop executes continuously while the wholesale price is not 3B inaddition to calculating the proit, it prompts the user or and gets the next wholesale price. 5he end-o-o# module executes ater 3 is entered or the wholesale price.

     Answer $ % sample solution is as ollows$

     #. Hierarchy chart$

    Flowchart:

  • 8/16/2019 2 Solutions to Assigned Problems

    5/16

    Programming Logic and Design, 6e Solutions 2-7

    Pseudocode:

    startDeclarations

    num holesale+ricenum retail+ricenum rofitstring 34O567+RO8+T " $6nter the holesale rice%string R6TA957+RO8+T " $6nter the retail rice%string 6!D759!6 " $Than:s for using this rogram%

    house:eeing/hile not /holesale+rice " 0

    detail5oo/endhileendOf;ob/

    sto

    house:eeing/outut 34O567+RO8+Tinut holesale+rice

    return

    detail5oo/outut R6TA957+RO8+T

  • 8/16/2019 2 Solutions to Assigned Problems

    6/16

    main program

    house!eeping:; detail:; end=>o#:;

    Programming Logic and Design, 6e Solutions 2-6

    inut retail+ricerofit " retail+rice < holesale+riceoutut rofitoutut 34O567+RO8+Tinut holesale+rice

    return

    endOf;ob/outut 6!D759!6

    return

    6. a. Draw the hierarchy chart and then plan the logic or a program that calculates thegown siCe a student needs or a graduation ceremony. 5he program uses threemodules. 5he irst prompts a user or and accepts the student*s height in inches. 5hesecond module accepts the student*s weight in pounds and con"erts the student*sheight to centimeters and weight to grams. 5hen, it calculates the graduation gownsiCe needed #y adding 1/ o the weight in grams to the "alue o the height incentimeters. 5he program*s output is the gown siCe the student should order. 5here

    are 2.74 centimeters in an inch and 47/.7 grams in a pound. 9se named constantswhere"er you thin! they are appropriate. 5he last module displays the message ?Endo o#@.

     Answer $ % sample solution is as ollows$

    Hierarchy chart$

    Flowchart:

  • 8/16/2019 2 Solutions to Assigned Problems

    7/16

    Programming Logic and Design, 6e Solutions 2-+

    Pseudocode:

    startDeclarations

    num student9nchesnum student+oundsnum studentCmnum student=ramsnum student>i?enum C6!T79!79!C4 " 2'(&num =RA879!7+O@!D " &(#'(*string 469=4T7+RO8+T " $6nter the students height in

    inches%string 369=4T7+RO8+T " $6nter the students eight in

    ounds%string 6!D759!6 " $6nd of ob%

    house:eeing/detail/endOf;ob/

    sto

    house:eeing/outut 469=4T7+RO8+Tinut student9nches

  • 8/16/2019 2 Solutions to Assigned Problems

    8/16

    main program

    house!eeping:; detailLoop:; end=>o#:;

    Programming Logic and Design, 6e Solutions 2-

    return

    detail/outut 369=4T7+RO8+Tinut student+oundsstudentCm " student9nches - C6!T79!79!C4student=rams " student+ounds - =RA879!7+O@!Dstudent>i?e " //1 # - student=rams . studentCmoutut student>i?e

    return

    endOf;ob/outut 6!D759!6

    return

     #. Ae"ise the siCe-determining program to execute continuously until the user enters 3or the height in inches.

     Answer $ % sample solution is as ollows$

     #. Hierarchy chart$

    Flowchart:

  • 8/16/2019 2 Solutions to Assigned Problems

    9/16

    Programming Logic and Design, 6e Solutions 2-

    Pseudocode:

    startDeclarations

    num studentFeetnum student9nchesnum student+oundsnum studentCm

    num student=ramsnum student>i?enum C6!T79!79!C4 " 2'(&num =RA879!7+O@!D " &(#'(*string 469=4T7+RO8+T " $6nter the students height in

    inches%string 369=4T7+RO8+T " $6nter the students eight in

    ounds%string 6!D759!6 " $6nd of ob%

    house:eeing/

  • 8/16/2019 2 Solutions to Assigned Problems

    10/16

    main program

    house!eeping:; detailLoop:; end=>o#:;

    Programming Logic and Design, 6e Solutions 2-13

    hile not /student9nches " 0detail5oo/

    endhileendOf;ob/

    sto

    house:eeing/outut 469=4T7+RO8+Tinut student9nches

    return

    detail5oo/outut 369=4T7+RO8+Tinut student+oundsstudentCm " student9nches - C6!T79!79!C4student=rams " student+ounds - =RA879!7+O@!Dstudent>i?e " //1 # - student=rams . studentCmoutut student>i?eoutut 469=4T7+RO8+Tinut student9nches

    return

    endOf;ob/outut 6!D759!6

    return

    +. Draw the hierarchy chart and design the logic or a program that containshouse!eeping, detail loop, and end-o-o# modules, and that calculates the ser"icecharge customers owe or writing a #ad chec!. 5he main program declares anyneeded glo#al "aria#les and constants and calls the other modules. 5he house!eepingmodule displays a prompt or and accepts a customer*s last name. 0hile the user does

    not enter ?@ or the name, the detail loop accepts the amount o the chec! indollars and cents. 5he ser"ice charge is computed as F23 plus 2 percent o the chec!amount. 5he detail loop also displays the ser"ice charge and then prompts the user or the next customer*s name. 5he end-o-o# module, which executes ater the userenters the sentinel "alue or the name, displays a message that indicates the programis complete.

     Answer $ % sample solution is as ollows$

    Hierarchy chart$

  • 8/16/2019 2 Solutions to Assigned Problems

    11/16

    Programming Logic and Design, 6e Solutions 2-11

    Flowchart:

    Pseudocode:

    startDeclarations

    string customer5ast!amenum chec:Amountnum sericeChargenum >6RE9C67C4AR=67BA>6 " 20num >6RE9C67C4AR=67+6RCT " 0'02string !A867+RO8+T " $6nter the customers last name%string C46C7+RO8+T " $6nter the amount of the chec: in

    dollars and cents%string 6!D759!6 " $Than: you for using the rogram%

    house:eeing/

    hile not /customer5ast!ame " $GGGG%detail5oo/

    endhileendOf;ob/

      sto

    house:eeing/outut !A867+RO8+Tinut customer5ast!ame

    return

  • 8/16/2019 2 Solutions to Assigned Problems

    12/16

    main program

    house!eeping:; detailLoop:; end=>o#:;

    Programming Logic and Design, 6e Solutions 2-12

    detail5oo/outut C46C7+RO8+Tinut chec:AmountsericeCharge " >6RE9C67C4AR=67BA>6 .

    >6RE9C67C4AR=67+6RCT - chec:Amountoutut sericeChargeoutut !A867+RO8+Tinut customer5ast!ame

    return

    endOf;ob/outut 6!D759!6

    return

    . Draw the hierarchy chart and design the logic or a program or the owner o Gits andPieces

  • 8/16/2019 2 Solutions to Assigned Problems

    13/16

    Programming Logic and Design, 6e Solutions 2-1/

    Pseudocode:

    start

    Declarationsstring emloyee!amenum ee:ly>alarynum ercent9ncreasenum ne>alarystring !A867+RO8+T " $6nter the emloyees name%string +6RCT7+RO8+T " $6nter the ercentage increase

    /as a decimal%string >A5ARY7+RO8+T " $6nter the ee:ly salary%string 6!D759!6 " $Than: you for using the rogram%

    house:eeing/hile not /emloyee!ame " $HHH%

    detail5oo/endhile

    endOf;ob/  sto

    house:eeing/outut +6RCT7+RO8+Tinut ercent9ncreaseoutut !A867+RO8+Tinut emloyee!ame

    return

  • 8/16/2019 2 Solutions to Assigned Problems

    14/16

    main program

    house!eeping:; detailLoop:; end=>o#:;

    Programming Logic and Design, 6e Solutions 2-14

    detail5oo/outut >A5ARY7+RO8+Tinut ee:ly>alaryne>alary " ee:ly>alary - ercent9ncrease . ee:ly>alaryoutut ne>alaryoutut !A867+RO8+Tinut emloyee!ame

    return

    endOf;ob/outut 6!D759!6

    return

    . Draw the hierarchy chart and design the logic or a program or the manager o the>eter 8ounty sot#all team, who wants to compute #atting a"erages or his players. % #atting a"erage is computed as hits di"ided #y at-#ats, and is usually expressed tothree decimal positions :or example, .2/7;. Design a program that prompts the user

    or a player ersey num#er, the num#er o hits, and the num#er o at-#ats, and thendisplays all the data, including the calculated #atting a"erage. 5he program accepts players continuously until 3 is entered or the ersey num#er. 9se appropriatemodules, including one that displays ?End o o#@ ater the sentinel is entered or the ersey num#er.

     Answer $ % sample solution is as ollows$

    Hierarchy chart$

    Flowchart:

  • 8/16/2019 2 Solutions to Assigned Problems

    15/16

    Programming Logic and Design, 6e Solutions 2-17

    Pseudocode:

    startDeclarations

    num ersey!umbernum numOf4itsnum numOfAtBatsnum battingAgstring !@87+RO8+T " $6nter the layers ersey number%string 49T>7+RO8+T " $6nter the number of hits%string BAT>7+RO8+T " $6nter the number of at bats%string 6!D759!6 " $6nd of ob%

    house:eeing/hile not /ersey!umber " 0

    detail5oo/endhileendOf;ob/

      sto

    house:eeing/outut !@87+RO8+Tinut ersey!umber

  • 8/16/2019 2 Solutions to Assigned Problems

    16/16

    Programming Logic and Design, 6e Solutions 2-16

    return

    detail5oo/outut 49T>7+RO8+Tinut numOf4itsoutut BAT>7+RO8+Tinut numOfAtBatsbattingAg " numOf4its numOfAtBatsoutut battingAgoutut !@87+RO8+Tinut ersey!umber

    return

    endOf;ob/outut 6!D759!6

    return