+ All Categories
Home > Documents > 169467420-Ch06-Logic6e-Solutions (1)

169467420-Ch06-Logic6e-Solutions (1)

Date post: 02-Mar-2018
Category:
Upload: binaryoperator
View: 217 times
Download: 0 times
Share this document with a friend
56
7/26/2019 169467420-Ch06-Logic6e-Solutions (1) http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 1/56 Programming Logic and Design, 6e Solutions 6- Programming Logic and Design, 6th Edition Chapter 6 Exercises 1. a. Design the logic for a program that allows a user to enter 10 numbers, then displays them in the reerse order of their entry.  Answer:  ! sample solution follows Flowchart: Pseudocode: start Declarations num index num SIZE = 10 num numbers[SIZE] = 0,0,0,0,0,0,0,0,0,0 getReady( 1
Transcript
Page 1: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 1/56

Programming Logic and Design, 6e Solutions 6-

Programming Logic and Design, 6th Edition

Chapter 6

Exercises

1. a. Design the logic for a program that allows a user to enter 10 numbers, thendisplays them in the reerse order of their entry.

 Answer:

 ! sample solution follows

Flowchart:

Pseudocode:

startDeclarations

num indexnum SIZE = 10num numbers[SIZE] = 0,0,0,0,0,0,0,0,0,0

getReady(

1

Page 2: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 2/56

Programming Logic and Design, 6e Solutions 6-

!"ile index # SIZEget$umbers(

end!"ile%inis"&'(

sto'

getReady(index = 0return

get$umbers(out'ut Enter a number %or 'osition ), indexin'ut numbers[index]index = index * 1

return

%inis"&'(out'ut +"e numbers in reerse order are- )!"ile index . 0

index = index / 1

out'ut numbers[index]end!"ile

return

 b. "odify the reerse-display program so that the user can enter up to 10 numbersuntil a sentinel alue is entered.

 Answer:

 ! sample solution follows

Flowchart:

#

Page 3: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 3/56

Programming Logic and Design, 6e Solutions 6-

Pseudocode:

start

Declarationsnum indexnum SIZE = 10num numbers[SIZE] = 0,0,0,0,0,0,0,0,0,0string $+I$&E = 2)string more$umbers = $+I$&E

getReady(!"ile index # SIZE 3$D more$umbers e4ual to $+I$&E

get$umbers(end!"ile%inis"&'(

sto'

getReady(index = 0out'ut Do you !ant to enter a number5 (26$)in'ut more$umbers

return

get$umbers(out'ut Enter a number %or 'osition ), indexin'ut numbers[index]index = index * 1

$

Page 4: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 4/56

Programming Logic and Design, 6e Solutions 6-

out'ut Do you !ant to enter more numbers5 (26$)in'ut more$umbers

return

%inis"&'(out'ut +"e numbers in reerse order are- )!"ile index . 0

index = index / 1out'ut numbers[index]

end!"ilereturn

#. a. Design the logic for a program that allows a user to enter 10 numbers, thendisplays each and its difference from the numeric aerage of the numbers.

 Answer:

 ! sample solution follows

Flowchart:

%

Page 5: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 5/56

Programming Logic and Design, 6e Solutions 6-

Pseudocode:

startDeclarations

num index

num sumnum agnum SIZE = 10num numbers[SIZE] = 0,0,0,0,0,0,0,0,0,0

getReady(!"ile index # SIZE

get$umbers(end!"ile%inis"&'(

sto'

getReady(index = 0sum = 0

return

get$umbers(out'ut Enter a number %or 'osition ), indexin'ut numbers[index]sum = sum * numbers[index]index = index * 1

return

%inis"&'(ag = sum6SIZEindex = 0!"ile index # SIZE

out'ut numbers[index], ag / numbers[index]index = index * 1

end!"ilereturn

 b. "odify the program in &'ercise #a so that the user can enter up to 10 numbersuntil a sentinel alue is entered.

 Answer:

 ! sample solution follows

Flowchart:

(

Page 6: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 6/56

Programming Logic and Design, 6e Solutions 6-

Pseudocode:

startDeclarations

num indexnum sumnum agnum actualSi7enum SIZE = 10num numbers[SIZE] = 0

6

Page 7: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 7/56

Programming Logic and Design, 6e Solutions 6-

string $+I$&E = 2)string more$umbers = $+I$&E

getReady(!"ile index # SIZE 3$D more$umbers = $+I$&E

get$umbers(end!"ile%inis"&'(

sto'

getReady(index = 0sum = 0out'ut Do you !ant to enter a number5 (26$)in'ut more$umbers

return

get$umbers(out'ut Enter a number %or 'osition ), indexin'ut numbers[index]sum = sum * numbers[index]

index = index * 1out'ut Do you !ant to enter more numbers5 (26$)in'ut more$umbers

return

%inis"&'(actualSi7e = indexi% actualSi7e . 0 t"en

ag = sum6actualSi7eindex = 0!"ile index # actualSi7e

out'ut numbers[index], ag / numbers[index]index = index * 1

end!"ileendi%

return

$. a. )he city of *ary is holding a special census. )he city has collected data oncards that each hold the oting district and age of a citi+en. )he districts are numbered1 through ##, and residents ages range from 0 through 10(. Design a program thatallows a cler to go through the cards, entering the district for each citi+en until anappropriate sentinel alue is entered. )he output is a list of all ## districts and thenumber of residents in each.

 Answer:

! sample solution follows

Flowchart:

Page 8: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 8/56

Programming Logic and Design, 6e Solutions 6-

Pseudocode:

startDeclarations

num districtnum DIS8SIZE = 99

/

Page 9: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 9/56

Programming Logic and Design, 6e Solutions 6-

num districtnt[SIZE] = 0num :&I+ = ;1

getReady(!"ile district not e4ual to :&I+

collectData(end!"ile%inis"&'(

sto'

getReady(out'ut Enter a district number or ), :&I+, to 4uit)in'ut district

return

collectData(i% district .= 1 3$D district #= SIZE t"en

districtnt[district;1] = districtnt[district;1] * 1else

out'ut Inalid district, record not counted)endi%

out'ut Enter a district number or ), :&I+, to 4uit)in'ut district

return

%inis"&'(district = 0!"ile district # DIS8SIZE

out'ut district*1, districtnt[district]district = district * 1

end!"ilereturn

 b. "odify &'ercise $a so the cler enters both the district and age on each card.Produce a count of the number of residents in each of the ## districts and a count ofresidents in each of the following age groups under 1/, 1/ through $0, $1 through%(, %6 through 6%, and 6( and older.

 Answer:

! sample solution follows

Flowchart:

Page 10: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 10/56

Programming Logic and Design, 6e Solutions 6-

Pseudocode:

startDeclarations

10

Page 11: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 11/56

Programming Logic and Design, 6e Solutions 6-

num agenum districtnum xnum :&I+ = ;1num <383>E = 10?num 3>E8SIZE = ?num agent[SIZE] = 0num DIS8SIZE = 99num districtnt[DIS8SIZE] = 0num 3>E8R3$>E = 0, 1@, A1, BC, C?string 3>E8>R&S = under 1@), 1@ t"roug" A0),

A1 t"roug" B?), BC t"roug" CB),C? and older)

getReady(!"ile district not e4ual to :&I+

count3ges(end!"ile%inis"&'(

sto'

getReady(out'ut Enter a district number or ), :&I+, to 4uit)in'ut district

return

count3ges(i% district .= 1 3$D district #= SIZE t"en

out'ut Enter t"e citi7ens age)in'ut agei% age .=0 3$D age #= <383>E t"en

districtnt[district;1] = districtnt[district;1]*1x = 3>E8SIZE / 1!"ile age # 3>E8R3$>E[x]

x = x / 1end!"ileagent[x] = agent[x] * 1

elseout'ut Inalid age, record not counted)

endi%else

out'ut Inalid district, record not counted)endi%out'ut Enter a district number or ), :&I+, to 4uit)in'ut district

return

%inis"&'(

district = 0!"ile district # DIS8SIZE

out'ut district*1, districtnt[district]district = district*1

end!"ilex = 0!"ile x # 3>E8SIZE

out'ut 3>E8>R&S[x], agent[x]x = x * 1

11

Page 12: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 12/56

Programming Logic and Design, 6e Solutions 6-

end!"ilereturn

%. a. )he "idille Par District maintains fie soccer teams, as shown in the table.Design a program that accepts a players team number and displays the players teamname.

Team Number Team Name

1 >oal >etters9 +"e ForceA +o' >unsB S"ooting Stars? <id%ield <onsters

 Answer:

! sample solution follows

Flowchart:

1#

Page 13: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 13/56

Programming Logic and Design, 6e Solutions 6-

Pseudocode:

startDeclarations

num team$um

num :&I+ = ;1num SIZE = ?string teams[SIZE] = >oal >etters), +"e Force),

+o' >uns), S"ooting Stars),<id%ield <onsters)

getReady(!"ile team$um not e4ual to :&I+

dis'lay+eam$ame(end!"ile%inis"&'(

sto'

getReady(out'ut Enter t"e 'layers team number or ), :&I+, to 4uit)in'ut team$um

return

dis'lay+eam$ame(i% team$um .= 1 3$D team$um #= SIZE t"en

out'ut team$um, teams[team$um;1]else

out'ut Inalid team number)endi%out'ut Enter t"e 'layers team number or ), :&I+, to 4uit)in'ut team$um

return

%inis"&'(out'ut End o% 'rogram)

return

 b. "odify the "idille Par District program so that after the last player has beenentered, the program displays a count of the number of players registered for each.

 Answer:

! sample solution follows

Flowchart:

1$

Page 14: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 14/56

Programming Logic and Design, 6e Solutions 6-

Pseudocode:

startDeclarations

num team$umnum :&I+ = ;1num SIZE = ?num teamnt[SIZE] = 0string teams[SIZE] = >oal >etters), +"e Force),

+o' >uns), S"ooting Stars),<id%ield <onsters)

getReady(!"ile team$um not e4ual to :&I+

count+eams(end!"ile%inis"&'(

1%

Page 15: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 15/56

Programming Logic and Design, 6e Solutions 6-

sto'

getReady(out'ut Enter t"e 'layers team number or ), :&I+, to 4uit)in'ut team$um

return

count+eams(i% team$um .= 1 3$D team$um #= SIZE t"en

out'ut team$um, teams[team$um;1]teamnt[team$um;1] = teamnt[team$um;1] * 1

elseout'ut Inalid team number)

endi%out'ut Enter t"e 'layers team number or ), :&I+, to 4uit)in'ut team$um

return

%inis"&'(team$um = 0

!"ile team$um # SIZEout'ut team$um*1, teamnt[team$um]team$um = team$um * 1

end!"ilereturn

(. a. 2atson &lementary School contains $0 classrooms numbered 1 through $0.&ach classroom can contain any number of students up to $(. &ach student taes anachieement test at the end of the school year and receies a score from 0 through100. 2rite a program that accepts data for each student in the school3student 4D,classroom number, and score on the achieement test. Design a program that lists the

total points scored for each of the $0 classrooms.

 Answer:

 ! sample solution follows

Flowchart:

1(

Page 16: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 16/56

Programming Logic and Design, 6e Solutions 6-

Pseudocode:

startDeclarations

num stuIDnum class$umnum score

num SIZE = A0num :&I+ = GGGGnum totalnts[SIZE] = 0

getReady(!"ile stuID not e4ual to :&I+

collectStudentData(end!"ile%inis"&'(

sto'

16

Page 17: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 17/56

Programming Logic and Design, 6e Solutions 6-

getReady(out'ut Enter a student ID or ), :&I+, to 4uit)in'ut stuID

return

collectStudentData(out'ut Enter t"e classroom number and score %or student ), stuIDin'ut class$um, scorei% class$um .= 1 3$D class$um #= SIZE t"en

totalnts[class$um;1] = totalnts[class$um;1] * scoreelse

out'ut Inalid classroom number)endi%out'ut Enter a student ID or ), :&I+, to 4uit)in'ut stuID

return

%inis"&'(class$um = 0!"ile class$um # SIZE

out'ut class$um*1, totalnts[class$um]class$um = class$um * 1

end!"ilereturn

 b. "odify the 2atson &lementary School program so that each classrooms aerageof the test scores is output, rather than each classrooms total.

 Answer:

! sample solution follows

Flowchart:

1

Page 18: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 18/56

Programming Logic and Design, 6e Solutions 6-

Pseudocode:

startDeclarations

num stuIDnum class$umnum score

1/

Page 19: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 19/56

Programming Logic and Design, 6e Solutions 6-

num agnum SIZE = A0num :&I+ = GGGGnum totalnts[SIZE] = 0num classnt[SIZE] = 0

getReady(!"ile stuID not e4ual to :&I+

collectStudentData(end!"ileclass$um = 0%inis"&'(

sto'

getReady(out'ut Enter a student ID or ), :&I+, to 4uit)in'ut stuID

return

collectStudentData(out'ut Enter t"e classroom number and score %or student ), stuID

in'ut class$um, scorei% class$um .= 1 3$D class$um #= SIZE t"en

totalnts[class$um;1] = totalnts[class$um;1] * scoreclassnt[class$um;1] = classnt[class$um;1] * 1

elseout'ut Inalid classroom number)

endi%out'ut Enter a student ID or ), :&I+, to 4uit)in'ut stuID

return

%inis"&'(!"ile class$um # SIZE

 i% classnt[class$um] = 0 t"en  ag = 0else  ag =totalnts[class$um]6classnt[class$um]endi%out'ut class$um*1, agclass$um = class$um * 1

end!"ilereturn

6. )he 5illy oat 7ast-7ood restaurant sells the following products

Product Price ($)"eeseburger 9HBGe'si 1H00"i's 0H?G

Design the logic for an application allows a user to enter an order item continuouslyuntil a sentinel alue is entered. !fter each item, display its price or the message

1

Page 20: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 20/56

Programming Logic and Design, 6e Solutions 6-

8Sorry, we do not carry that9 as output. !fter all items hae been entered, displaythe total price for the order.

 Answer:

! sample solution follows

Flowchart:

#0

Page 21: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 21/56

Programming Logic and Design, 6e Solutions 6-#1

Page 22: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 22/56

Programming Logic and Design, 6e Solutions 6-

Pseudocode:

startDeclarations

string itemrderednum x

num %ound = 0num total = 0num SIZE = Astring :&I+ = ZZZZ)num RIE[SIZE] = 9HBG, 1, 0H?Gstring RD&+[SIZE] = "eeseburger), e'si), "i's)string ERRR8<S> = Sorry, !e do not carry t"at)

getReady(!"ile itemrdered not e4ual to :&I+

detailoo'(end!"ile%inis"&'(

sto'

getReady(out'ut Enter an item or ), :&I+, to com'lete your order)in'ut itemrdered

return

detailoo'(%ound = 0x = 0!"ile x # SIZE

i% itemrdered = RD&+[x] t"enout'ut RIE[x]x = SIZE%ound = 1

total = total * RIE[x]else

x = x * 1endi%

end!"ilei% %ound = 0 t"en

out'ut ERRR8<S>endi%out'ut Enter an item or ), :&I+, to com'lete your order)in'ut itemrdered

return

%inis"&'(out'ut 2our order total is- J), total

return

. Design the application logic for a company that wants a report containing a breadown of payroll by department. 4nput includes each employees departmentnumber, hourly salary, and number of hours wored. )he output is a list of the seendepartments in the company and the total gross payroll :rate times hours; for eachdepartment. )he department names are shown in the accompanying table.

##

Page 23: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 23/56

Programming Logic and Design, 6e Solutions 6-

Department

um!er

Department

ame

1 Personnel

# "areting

$ "anufacturing

% *omputer Serices

( Sales

6 !ccounting

Shipping

 Answer:

! sample solution follows

Flowchart:

#$

Page 24: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 24/56

Programming Logic and Design, 6e Solutions 6-

Pseudocode:

startDeclarations

num de't$umnum salarynum "rsKorLednum SIZE = Mnum total>ross[SIZE] = 0string DE+S[SIZE] = ersonnel), <arLeting),

  <anu%acturing),om'uter Serices), Sales),3ccounting), S"i''ing)

#%

Page 25: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 25/56

Programming Logic and Design, 6e Solutions 6-

getReady(!"ile not eo%

detailoo'(end!"ile%inis"&'(

sto'

getReady(out'ut Enter t"e de'artment number, "ourly salary, and number o%

"ours !orLed)in'ut de't$um, salary, "rsKorLed

return

detailoo'(i% de't$um .= 1 3$D de't$um #= SIZE t"en

total>ross[de't$um;1] = total>ross[de't$um;1] *("rsKorLed N salary

elseout'ut Inalid de'artment number)

endi%

out'ut Enter t"e de'artment number, "ourly salary, and number o%"ours !orLed)

in'ut de't$um, salary, "rsKorLedreturn

%inis"&'(de't$um = 0!"ile de't$um # SIZE

out'ut de't$um*1, DE+S[de't$um], total>ross[de't$um]de't$um = de't$um * 1

end!"ilereturn

/. Design a program that computes pay for employees. !llow a user to continuouslyinput employees names until an appropriate sentinel alue is entered. !lso input eachemployees hourly wage and hours wored. *ompute each employees gross pay:hours times rate;, withholding ta' percentage :based on the accompanying table;,withholding ta' amount, and net pay :gross pay minus withholding ta';. Display allthe results for each employee. !fter the last employee has been entered, display thesum of all the hours wored, the total gross payroll, the total withholding for allemployees, and the total net payroll.

  "ee#l %ross Pa "ithholding Percent (&)0H00 / 900H00 10

900H01 / A?0H00 1BA?0H01 / ?00H00 1@

?00H01 / u'H000  99

 Answer: ! sample solution follows

Flowchart:

#(

Page 26: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 26/56

Programming Logic and Design, 6e Solutions 6-

Pseudocode:

startDeclarations

string em'$amenum salary

#6

Page 27: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 27/56

Programming Logic and Design, 6e Solutions 6-

num "rsKorLednum xnum grossaynum !it"+axnum netaynum total>rossay = 0num totalOrsKorLed = 0num totalKit"+ax = 0num total$etay = 0string :&I+ = 7777)num SIZE = Bnum KI+O8R3+E[SIZE] = 0H10, 0H1B, 0H1@, 0H99num KI+O8R3$>E[SIZE] = 0, 900H01, A?0H01, ?00H01

getReady(!"ile em'$ame not e4ual to :&I+

detailoo'(end!"ile%inis"&'(

sto'

getReady(out'ut Enter an em'loyee name or ), :&I+, to 4uit)in'ut em'$ame

return

detailoo'(out'ut Enter ), em'$ame, s "ourly !age and "ours !orLed)in'ut salary, "oursKorLedgrossay = "rsKorLed N salarytotalOrsKorLed = totalOrsKorLed * "rsKorLedtotal>rossay = total>rossay * grossayx = SIZE / 1!"ile grossay # KI+O8R3$>E[x]

x = x / 1end!"ile!it"+ax = grossay N KI+O8R3+E[x]totalKit"+ax = totalKit"+ax * !it"+axnetay = grossay / !it"+axtotal$etay = total$etay * netayout'ut em'$ame, grossay, KI+O8R3+E[x] N 100, !it"+ax, netayout'ut Enter an em'loyee name or ), :&I+, to 4uit)in'ut em'$ame

return

%inis"&'(out'ut totalOrsKorLed, total>rossay, totalKit"+ax, total$etay

return

#

Page 28: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 28/56

Programming Logic and Design, 6e Solutions 6-

. )he Perfect Party *atering *ompany hosts eents for clients. *reate anapplication that accepts an eent number, the eent hosts last name, and numericmonth, day, and year alues representing the eent date. )he application should alsoaccept the number of guests that will attend the eent and a numeric meal code thatrepresents the entr<e the eent hosts will sere. !s each clients data is entered, erify

that the month, day, year, and meal code are alid= if any of these is not alid,continue to prompt the user until it is. )he alid meal codes are shown in theaccompanying table.

Code Entr'e Price per Person ($)

1 >oast beef #%.(0

# Salmon 1.00

$ Linguine 16.(0

% *hicen 1/.00

Design the logic for an application that outputs each eent number, host name,alidated date, meal code, entr<e name, number of guests, gross total price for the party, and price for the party after discount. )he gross total price for the party is themeal price per guest times the number of guests. )he final price includes a discount based on the accompanying table.

um!er o %uests Discount ($)

1-#( 0

#6-(0 (

(1-100 1#(

101-#(0 #00

#(1 and oer $00

 Answer: ! sample solution follows

Flowchart:

#/

Page 29: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 29/56

Programming Logic and Design, 6e Solutions 6-

Pseudocode:

startDeclarations

num eent$umstring last$amenum mont"num daynum year

#

Page 30: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 30/56

Programming Logic and Design, 6e Solutions 6-

num num%>uestsnum mealodenum xnum inalidDate = 0num grossricenum <SIZE = Bnum DSIZE = ?string E$+REES[<SIZE] = Roast bee%), Salmon), inguine),

"icLen)num RIE[<SIZE] = 9BH?0, 1G, 1CH?0, 1@num DIS8R3$>E[DSIZE] = 1, 9C, ?1, 101, 9?1num DIS&$+[DSIZE] = 0, M?, 19?, 900, A00

getReady(detail(%inis"&'(

sto'

getReady(out'ut Enter t"e eent number, "osts last name, and eent date

(as mont", day, and year)

in'ut eent$um, last$ame, mont", day, yearout'ut Enter t"e number o% guests and t"e meal code)in'ut num%>uests, mealode

return

detail(alidate<ealode(alidateDate(grossrice = RIE[mealode;1] N num%>uestsx = DSIZE / 1!"ile num%>uests # DIS8R3$>E[x]

x = x / 1end!"ileout'ut eent$um, last$ame, mont", 6), day, 6), year,

 mealode, E$+REES[mealode;1], num%>uests, grossrice, (grossrice / DIS&$+[x]

  return

alidate<ealode(!"ile mealode # 1 R mealode . <SIZE

out'ut Inalid meal code, 'lease reenter)in'ut mealode

end!"ilereturn

alidateDate(c"ecLDate(

!"ile inalidDate = 1ou'ut Inalid date, 'lease reenter)in'ut mont", day, year

end!"ilereturn

c"ecLDate(inalidDate = 0i% (mont" # 1 R mont" . 19 R day # 1 R day . A1 R

year # 900G R year . 9019 t"en

$0

Page 31: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 31/56

Programming Logic and Design, 6e Solutions 6-

inalidDate = 1else

i% day = A1 3$D (mont" = B R mont" = C R mont" = G Rmont" = 11 t"eninalidDate = 1

elsei% day . 9@ 3$D mont" = 9 t"en

inalidDate = 1endi%

endi%endi%

return

%inis"&'(out'ut End o% 'rogram)

return

10. a. Daily Life Magazine wants an analysis of the demographic characteristics ofits readers. )he "areting department has collected reader surey recordscontaining the age, gender, marital status, and annual income of readers. Design anapplication that accepts reader data and, when data entry is complete, produces acount of readers by age groups as follows under #0, #0?#, $0?$, %0?%, and (0and older.

 Answer: ! sample solution follows

Flowchart:

$1

Page 32: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 32/56

Programming Logic and Design, 6e Solutions 6-

Pseudocode:

startDeclarations

num agenum incomenum xstring genderstring maritalStatusnum SIZE = ?num agent[SIZE] = 0

$#

Page 33: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 33/56

Programming Logic and Design, 6e Solutions 6-

num 3>E8R3$>E[SIZE] = 0, 90, A0, B0, ?0string 3>E8>R&S[SIZE] = under 90), 90 t"roug" 9G),

A0 t"roug" AG), B0 t"roug" BG),?0 and older)

getReady(!"ile not eo%

detailoo'(end!"ile%inis"&'(

sto'

getReady(out'ut Enter t"e age, gender, marital status, and income o% a

reader)in'ut age, gender, maritalStatus, income

return

detailoo'(x = SIZE / 1!"ile age # 3>E8R3$>E[x]

x = x / 1end!"ileagent[x] = agent[x] * 1out'ut Enter t"e age, gender, marital status, and income o% a

reader)in'ut age, gender, maritalStatus, income

return

%inis"&'(x = 0!"ile x # SIZE

out'ut 3>E8>R&S[x], agent[x]x = x * 1

end!"ilereturn

 b. "odify the Daily Life Magazine program so that it produces a count of readers bygender within age group3that is, under #0 females, under #0 males, and so on.

 Answer: ! sample solution follows

Flowchart:

$$

Page 34: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 34/56

Programming Logic and Design, 6e Solutions 6-

Pseudocode:

startDeclarations

num agenum incomenum xstring genderstring maritalStatusnum SIZE = ?

$%

Page 35: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 35/56

Programming Logic and Design, 6e Solutions 6-

num malent[SIZE] = 0num %emalent[SIZE] = 0num 3>E8R3$>E[SIZE] = 0, 90, A0, B0, ?0string 3>E8>R&S[SIZE] = under 90), 90 t"roug" 9G),

A0 t"roug" AG), B0 t"roug" BG),?0 and older)

getReady(!"ile not eo%

detailoo'(end!"ile%inis"&'(

sto'

getReady(out'ut Enter t"e age, gender, marital status, and income o% a

reader)in'ut age, gender, maritalStatus, income

return

detailoo'(

x = SIZE / 1!"ile age # 3>E8R3$>E[x]

x = x / 1end!"ilei% gender = male) t"en

malent[x] = malent[x] * 1else

%emalent[x] = %emalent[x] * 1endi%out'ut Enter t"e age, gender, marital status, and income o% a

reader)in'ut age, gender, maritalStatus, income

return

%inis"&'(x = 0!"ile x # SIZE

out'ut 3>E8>R&S[x], malent[x], %emalent[x]x = x * 1

end!"ilereturn

c. "odify the Daily Life Magazine program so that is produces a count of readers byannual income groups as follows under @$0,000, @$0,000?@%,, (0,000?@6,,and @0,000 and up.

 Answer: ! sample solution follows

Flowchart:

$(

Page 36: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 36/56

Programming Logic and Design, 6e Solutions 6-

Pseudocode:

start

Declarationsnum agenum incomenum xstring genderstring maritalStatusnum SIZE = Bnum incoment[SIZE] = 0num I$<E8R3$>E[SIZE] = 0, A0000, ?0000, M0000

$6

Page 37: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 37/56

Programming Logic and Design, 6e Solutions 6-

string I$<E8>R&S[SIZE] = under JA0,000),  JA0,000;JBG,GGG), J?0,000;

JCG,GGG),JM0,000 and u')

getReady(!"ile not eo%

detailoo'(end!"ile%inis"&'(

sto'

getReady(out'ut Enter t"e age, gender, marital status, and income o% a

reader)in'ut age, gender, maritalStatus, income

return

detailoo'(x = SIZE / 1!"ile age # I$<E8R3$>E[x]

x = x / 1end!"ileincoment[x] = incoment[x] * 1out'ut Enter t"e age, gender, marital status, and income o% a

reader)in'ut age, gender, maritalStatus, income

return

%inis"&'(x = 0!"ile x # SIZE

out'ut I$<E8>R&S[x], incoment[x]x = x * 1

end!"ilereturn

11. len >oss Aacation Property Sales employs seen salespeople, as shown in theaccompanying table.

D um!er *alesperson ame

10$ Darwin

10% Brat+

#01 Shulstad

$1 7ortune

$6 2icert

$// "iller  

%$( Aic  

2hen a salesperson maes a sale, a record is created including the date, time, anddollar amount of the sale. )he time is e'pressed in hours and minutes, based on a #%-hour cloc. )he sale amount is e'pressed in whole dollars. Salespeople earn acommission that differs for each sale, based on the rate schedule in the accompanyingtable.

$

Page 38: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 38/56

Programming Logic and Design, 6e Solutions 6-

*ale +mount ($) Commission ate (&)0 / ?0,GGG B?1,000 ; 19?,GGG ?19C,000 ; 900,GGG C901,000 and u' M

Design an application that produces each of the following

a. ! list of each salesperson number, name, total sales, and total commissions

 Answer: ! sample solution follows

Flowchart:

$/

Page 39: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 39/56

Programming Logic and Design, 6e Solutions 6-

Pseudocode:

$

Page 40: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 40/56

Programming Logic and Design, 6e Solutions 6-

startDeclarations  num date  num "ours

num minutesnum sale3mtnum id$umnum 'ersonnum xstring datenum SSIZE = Bnum SIZE = Mnum totalSales[SIZE] = 0num totalomm[SIZE] = 0num ID8$&<[SIZE] = 10A, 10B, 901, A1G, ACM, A@@, BA?string $3<E[SIZE] = Dar!in), Prat7), S"ulstad), Fortune),

KicLert), <iller), QicL)num S3E83<+[SSIZE] = 0, ?1000, 19C000, 901000num <<8R3+E[SSIZE] = 0H0B, 0H0?, 0H0C, 0H0M

getReady(

!"ile not eo%detailoo'(

end!"ile%inis"&'(

sto'

getReady(out'ut Enter t"e sales'erson ID number)in'ut id$um

return

detailoo'('erson = 0!"ile 'erson # SIZE 3$D id$um not e4ual to ID8$&<['erson]

'erson = 'erson * 1end!"ilei% 'erson = SIZE t"en

out'ut 3n inalid number "as been entered,'lease try again)

elsein'ut date, "ours, minutes, sale3mttotalSales['erson] = totalSales['erson] * sale3mtx = SSIZE / 1!"ile sale3mt # S3E83<+[x]

x = x / 1end!"iletotalomm['erson] = totalomm['erson] *

(<<8R3+E[x] N sale3mtendi%out'ut Enter t"e sales'erson ID number)in'ut id$um

return

%inis"&'(x = 0!"ile x # SIZE

out'ut ID8$&<[x], $3<E[x], totalSales[x], totalomm[x]

%0

Page 41: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 41/56

Programming Logic and Design, 6e Solutions 6-

x = x * 1end!"ile

return

 b. ! list of each each month of the year as both a number and a word :for e'ample,

801 Canuary9;, and the total sales for the month for all salespeople

 Answer: ! sample solution follows

Flowchart:

%1

Page 42: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 42/56

Programming Logic and Design, 6e Solutions 6-

Pseudocode:

startDeclarations  num "ours, minutes

num mont", day, year

%#

Page 43: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 43/56

Programming Logic and Design, 6e Solutions 6-

num sale3mtnum id$umnum 'erson, xnum SSIZE = Bnum SIZE = Mnum <SIZE = 19num totalSales[<SIZE] = 0num totalomm[SIZE] = 0num ID8$&<[SIZE] = 10A, 10B, 901, A1G, ACM, A@@, BA?string $3<E[SIZE] = Dar!in), Prat7), S"ulstad), Fortune),

KicLert), <iller), QicL)num S3E83<+[SSIZE] = 0, ?1000, 19C000, 901000num <<8R3+E[SSIZE] = 0H0B, 0H0?, 0H0C, 0H0M

  string <$+OS[<SIZE] = anuary), February), <arc") 3'ril), <ay), une), uly), 3ugust),Se'tember), ctober), $oember), December)

  getReady(!"ile not eo%

detailoo'(end!"ile

%inis"&'(sto'

getReady(out'ut Enter t"e sales'erson ID number)in'ut id$um

return

detailoo'('erson = 0!"ile 'erson # SIZE 3$D id$um not e4ual to ID8$&<['erson]

'erson = 'erson * 1end!"ilei% 'erson = SIZE t"en

out'ut 3n inalid number "as been entered,'lease try again)

elsein'ut mont", day, year, "ours, minutes, sale3mt!"ile mont" # 1 R mont" . 19 R day # 1 R day . A1

out'ut Inalid date, 'lease reenter)in'ut mont", day, year

end!"iletotalSales[mont";1] = totalSales[mont";1] * sale3mtendi%

out'ut Enter t"e sales'erson ID number)in'ut id$um

return

%inis"&'(x = 0!"ile x # <SIZE

out'ut x*1, <$+OS[x], totalSales[x]x = x * 1

end!"ilereturn

%$

Page 44: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 44/56

Programming Logic and Design, 6e Solutions 6-

c. ! list of total sales as well as total commissions earned by all salespeople for eachof the following time frames, based on hour of the day 00?0(, 06?1#, 1$?1/, and1?#$

 Answer: ! sample solution follows

Flowchart: )he flowchart will be similar to those shown in parts a and b.

Pseudocode:

startDeclarations

num "ours, minutesnum mont", day, yearnum sale3mtnum id$um

num time, xnum SSIZE = Bnum SIZE = Mnum +SIZE = Bnum totalSales[+SIZE] = 0num totalomm[+SIZE] = 0num ID8$&<[SIZE] = 10A, 10B, 901, A1G, ACM, A@@, BA?string $3<E[SIZE] = Dar!in), Prat7), S"ulstad),Fortune),

KicLert), <iller), QicL)num S3E83<+[SSIZE] = 0, ?1000, 19C000, 901000num <<8R3+E[SSIZE] = 0H0B, 0H0?, 0H0C, 0H0Mnum +I<E8R3$>E[+SIZE] = 0, C, 1A, 1G

string +I<ES[+SIZE] = 00;0?, 0C;19), 1A;1@), 1G;9A)getReady(!"ile not eo%

detailoo'(end!"ile%inis"&'(

sto'

getReady(out'ut Enter t"e sales'erson ID number)in'ut id$um

return

detailoo'(

'erson = 0!"ile 'erson # SIZE 3$D id$um not e4ual to ID8$&<['erson]

'erson = 'erson * 1end!"ilei% 'erson = SIZE t"en

out'ut 3n inalid number "as been entered,'lease try again)

elsein'ut mont", day, year, "ours, minutes, sale3mt

%%

Page 45: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 45/56

Programming Logic and Design, 6e Solutions 6-

!"ile mont" # 1 R mont" . 19 R day # 1 R day . A1out'ut Inalid date, 'lease reenter)in'ut mont", day, year

end!"iletime = +SIZE;1!"ile "ours # +I<E8R3$>E[time]

time = time / 1end!"iletotalSales[time] = totalSales[time] * sale3mtx = SSIZE / 1!"ile sale3mt # S3E83<+[x]

x = x / 1end!"iletotalomm[time] = totalomm[time] * (<<8R3+E[x]Nsale3mt

endi%out'ut Enter t"e sales'erson ID number)in'ut id$um

return

%inis"&'(

x = 0!"ile x # +SIZE

out'ut +I<E[x], totalSales[x], totalomm[x]x = x * 1

end!"ilereturn

Find the -ugs

1#. our student dis contains files named D&5E06-01.t't, D&5E06-0#.t't, andD&5E06-0$.t't. &ach file starts with some comments that describe the problem.

*omments are lines that begin with two slashes :FF;. 7ollowing the comments, eachfile contains pseudocode that has one or more bugs you must find and correct.

 Answer 

Please see the D&5E06-01.t't, D&5E06-0#.t't, and D&5E06-0$.t't solutionfiles.

%ame .one

1$. *reate the logic for a "agic / 5all game in which the user enters a Guestion suchas 82hat does my future holdH9 )he computer randomly selects one of eight possibleague answers, such as 84t remains to be seen.9

startDeclarations

num I<I+ = @

%(

Page 46: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 46/56

Programming Logic and Design, 6e Solutions 6-

num indexstring user:uestionstring :&I+ = 7777)string 3$SKERS[I<I+] =

3s I see it, yes),It is certain),Signs 'oint to yes),It remains to be seen),Re'ly "a7y, try again),utlooL not so good),annot 'redict no!),<y sources say no)

getReady(!"ile user:uestion #. :&I+

detailoo'(end!"ile%inis"&'(

sto'

getReady(

out'ut Enter a 4uestion or ), :&I+, to 4uit)in'ut user:uestion

return

detailoo'(index = random(I<I+out'ut 3$SKERS[index]out'ut Enter a 4uestion or ), :&I+, to 4uit)in'ut user:uestion

return

%inis"&'(out'ut End o% 'rogram)

return

1%. *reate the logic for an application that contains an array of 10 multiple-choiceGuestions related to your faorite hobby. &ach Guestion contains three answerchoices. !lso create a parallel array that holds the correct answer to each Guestion3 !, 5, or *. Display each Guestion and erify that the user enters only !, 5, or * asthe answer3if not, eep prompting the user until a alid response is entered. 4f theuser responds to a Guestion correctly, display 8*orrectI9= otherwise, display 8)hecorrect answer is9 and the letter of the correct answer. !fter the user answers all theGuestions, display the number of correct and incorrect answers.

 Answer:

! sample solution follows

Pseudocode :Please note, for breity this solution contains only fie Guestions.Students should proide 10 Guestions.;:

start

%6

Page 47: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 47/56

Programming Logic and Design, 6e Solutions 6-

Declarationsnum correct = 0num incorrect = 0num xnum SIZE = ?string guesses[SIZE] = )string :&ES+I$S[SIZE] =

K"at is t"e closest star to Eart"5),K"at is t"e name o% Eart"s moon5),Oo! many miles (in trillions are in a lig"t year5),Oo! many moons does luto "ae5),K"at 'lanet is t"e biggest5)

string OIES[SIZE] =3H Sco''er, TH Di''er, H Sun),3H una, TH Euro'a, H Sol),3H C, TH GA, H 10),3H C, TH 9, H A),3H Saturn, TH u'iter, H Eart")

string 3$SKERS[SIZE] = ), 3), 3), ), T)getReady(

!"ile x # SIZEdetailoo'(

end!"ile%inis"&'(

sto'

getReady(x = 0out'ut :&ES+I$S[x], OIES[x]in'ut guesses[x]

return

detailoo'(!"ile (guesses[x] not e4ual to 3) R

guesses[x] not e4ual to T) Rguesses[x] not e4ual to )out'ut Inalid ans!er, 'lease enter eit"er 3, T, or )in'ut guesses[x]

end!"ilei% guesses[x] = 3$SKERS[x] t"en

out'ut orrectU)correct = correct * 1

elseout'ut +"e correct ans!er is- , 3$SKERS[x]incorrect = incorrect * 1

endi%x = x * 1

out'ut :&ES+I$S[x], OIES[x]in'ut guesses[x]

return

%inis"&'(out'ut $umber o% correct = ), correctout'ut $umber o% incorrect = ), incorrect

return

%

Page 48: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 48/56

Programming Logic and Design, 6e Solutions 6-

1(. a. *reate the logic for a dice game. )he application randomly 8throws9 fie dicefor the computer and fie dice for the player. !s each random throw, store it in anarray. )he application displays all the alues, which can be from 1 to 6 inclusie foreach die. Decide the winner based on the following hierarchy of die alues. !nyhigher combination beats a lower one= for e'ample, fie of a ind beats four of a ind.

•  7ie of a ind

•  7our of a ind

•  )hree of a ind

•  ! pair 

7or this game, the numeric dice alues do not count. 7or e'ample, if both playershae three of a ind, its a tie, no matter what the alues of the three dice are.!dditionally, the game does not recogni+e a full house :three of a ind plus two of aind;. 7igure 6-1 shows how the game might be played in a command-lineenironment.

 Answer:

! sample solution follows

Pseudocode:

startDeclarations

num xnum ynum 'layer<atc"

num com'uter<atc"num 'layerargestnum com'uterargestnum I<I+ = Cnum SIZE = ?num 'layerDice[SIZE] = 0num com'uterDice[SIZE] = 0num 'layerQalues[I<I+] = 0num com'uterQalues[I<I+] = 0

getReady(accumulate$ums(%indargest(%inis"&'(

sto'

getReady(66 'o'ulate bot" arrays !it" random numbersx = 0!"ile x # SIZE

'layerDice[x] = random(I<I+com'uterDice[x] = random(I<I+x = x * 1

end!"ile

%/

Page 49: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 49/56

Programming Logic and Design, 6e Solutions 6-

66 out'ut !"at t"e com'uter rolledx = 0out'ut om'uter rolled- )!"ile x # SIZE

out'ut com'uterDice[x]x = x * 1

end!"ile

66 out'ut !"at 'layer rolledx = 0out'ut 2ou rolled- )!"ile x # SIZE

out'ut 'layerDice[x]x = x * 1

end!"ilereturn

acculate$ums(66 accumulate "o! many o% eac" number !as rolled

66 by bot" t"e com'uter and t"e 'layer, store t"ese66 alues in arraysx = 0!"ile x # I<I+

y = 0!"ile y # SIZE

i% 'layerDice[y] = (x*1 t"en'layerQalues[x] = 'layerQalues[x] * 1

endi%i% com'uterDice[y] = (x*1 t"en

com'uterQalues[x] = com'uterQalues[x] * 1endi%y = y * 1

end!"ilex = x * 1

end!"ilereturn

%indargest(66 %ind t"e largest accumulated alue / t"is !ill tell t"e66 'rogram t"e largest o% a Lind)x = 0com'uterargest = 0'layerargest = 0!"ile x # I<I+;1

i% com'uterQalues[x*1] . com'uterQalues[com'uterargest]com'uterargest = x * 1

endi%i% 'layerQalues[x*1] . 'layerQalues['layerargest]

'layerargest = x * 1endi%x = x * 1

end!"ile

com'uter<atc" = com'uterQalues[com'uterargest]'layer<atc" = 'layerQalues['layerargest]

return

%

Page 50: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 50/56

Programming Logic and Design, 6e Solutions 6-

%inis"&'(out'ut om'uter "as ), com'uter<atc", o% a Lind)out'ut 2ou "ae ), 'layer<atc", o% a Lind)

i% com'uter<atc" . 'layer<atc" t"enout'ut om'uter !ins)

elsei% 'layer<atc" . com'uter<atc" t"en

out'ut 2ou !in)else

out'ut +ie)endi%

endi%return

 b. 4mproe the dice game so that when both players hae the same combination ofdice, the higher alue wins. 7or e'ample, two 6s beats two (s.

 Answer:

! sample solution follows

Pseudocode:start

Declarationsnum xnum ynum 'layer<atc"num com'uter<atc"num 'layerargest

num com'uterargestnum I<I+ = Cnum SIZE = ?num 'layerDice[SIZE] = 0num com'uterDice[SIZE] = 0num 'layerQalues[I<I+] = 0num com'uterQalues[I<I+] = 0

getReady(accumulate$ums(%indargest(%inis"&'(

sto'

getReady(x = 0!"ile x # SIZE

'layerDice[x] = random(I<I+com'uterDice[x] = random(I<I+x = x * 1

end!"ile

x = 0

(0

Page 51: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 51/56

Programming Logic and Design, 6e Solutions 6-

out'ut om'uter rolled- )!"ile x # SIZE

out'ut com'uterDice[x]x = x * 1

end!"ile

x = 0out'ut 2ou rolled- )!"ile x # SIZE

out'ut 'layerDice[x]x = x * 1

end!"ilereturn

accumulate$ums(x = 0!"ile x # I<I+

y = 0!"ile y # SIZE

i% 'layerDice[y] = x * 1 t"en

'layerQalues[x] = 'layerQalues[x] * 1endi%i% com'uterDice[y] = x * 1 t"en

com'uterQalues[x] = com'uterQalues[x] * 1endi%y = y * 1

end!"ilex = x * 1

end!"ilereturn

%indargest(x = 0com'uterargest = 0'layerargest = 0!"ile x # I<I+;1

i% com'uterQalues[x*1] . com'uterQalues[com'uterargest]com'uterargest = x * 1

endi%i% 'layerQalues[x*1] . 'layerQalues['layerargest]

'layerargest = x * 1endi%x = x * 1

end!"ile

com'uter<atc" = com'uterQalues[com'uterargest]'layer<atc" = 'layerQalues['layerargest]

return

%inis"&'(out'ut om'uter "as ), com'uter<atc", o% a Lind)out'ut 2ou "ae ), 'layer<atc", o% a Lind)

i% com'uter<atc" . 'layer<atc" t"enout'ut om'uter !ins)

elsei% 'layer<atc" . com'uter<atc" t"en

(1

Page 52: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 52/56

Programming Logic and Design, 6e Solutions 6-

out'ut 2ou !in)else

i% com'uterargest . 'layerargest t"enout'ut om'uter !ins)

elsei% 'layerargest . com'uterargest t"en

out'ut 2ou !in)else

out'ut +ie)endi%

endi%endi%

endi%return

16. Design the logic for the game Jangman, in which the user guesses letters in ahidden word. Store the letters of a word in an array of characters. Display a dash foreach missing letter. !llow the user to continuously guess a letter until all the letters inthe word are correctly guessed. !s the user enters each guess, display the word again,

filling in the guess if it was correct. 7or e'ample, if the hidden word is 8computer9,first display 8--------9. !fter the user guesses 8p9, the display becomes 8---p----9."ae sure that when a user maes a correct guess, all the matching letters are filledin. 7or e'ample, if the word is 8banana9 then when the user guesses 8a9, all three 8a9characters are filled in.

 Answer:

 ! sample solution follows

Pseudocode:

startDeclarations

num xnum numorrectnum E$>+O = @string KRD[E$>+O] = ), ), <), ), &),

+), E), R)string "iddenKord[E$>+O] = ;),;),;),;),

  ;),;),;),;)string guess

getReady(!"ile numorrect not e4ual to E$>+O

detailoo'(

end!"ile%inis"&'(

sto'

getReady(numorrect = 0out'ut "iddenKordout'ut lease guess a letter)in'ut guess

(#

Page 53: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 53/56

Programming Logic and Design, 6e Solutions 6-

return

detailoo'(x = 0!"ile x # E$>+O

i% KRD[x] = guess t"ennumorrect = numorrect * 1"iddenKord[x] = guess

endi%x = x * 1

end!"ileout'ut "iddenKordi% numorrect not e4ual to E$>+O

out'ut lease guess a letter)in'ut guess

endi%return

%inis"&'(out'ut 2ou guessed t"e !ordU)

out'ut "iddenKordreturn

1. *reate two parallel arrays that represent a standard dec of (# playing cards. Knearray is numeric and holds the alues 1 through 1$ :representing !ce, # through 10,Cac, ueen, and Bing;. )he other array is a string array and holds suits :8*lubs9,8Diamonds9, 8Jearts9, and 8Spades9;. *reate the arrays so that all (# cards arerepresented. *reate a 2ar card game that randomly selects two cards :one for the player and one for the computer; and declares a winner :or a tie; based on thenumeric alue of the two cards. )he game should play for #6 rounds of 2ar, dealinga full dec with no repeated cards. 7or this game, assume the lowest card is the !ce.Display the alues of the players and computers cards, compare their alues, anddetermine the winner. 2hen all the cards in the dec are e'hausted, display a count ofthe number of times the player wins, the number of times the computer wins, and thenumber of ties.

Jere are some hints

• Start by creating an array of all (# playing cards.

• Select a random number for the dec position of the players first card and assign

the card at that array position to the player.

"oe eery higher-positioned card in the dec 8down9 one to fill in the gap. 4nother words, if the players first random number is %, select the card at position% :both the numeric alue and the string;, moe the card that was in position (0to position %, and moe the card that was in position (1 to position (0. Knly (1cards remain in the dec after the players first card is dealt, so the aailable cardarray is smaller by one.

• 4n the same way, randomly select a card for the computer and 8remoe9 the card

from the dec.

($

Page 54: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 54/56

Programming Logic and Design, 6e Solutions 6-

 Answer: ! sample solution follows

Pseudocode:

start

Declarationsnum xnum ynum limitnum indexnum 'layerardnum 'layerard$umnum com'uterardnum com'uterard$umnum 'layerKin = 0num com'uterKin = 0num tie = 0string 'layerardSuitstring com'uterardSuit

num E$>+O = ?9num >R&S = Bnum R&$DS = 9Cnum T&$DS[>R&S] = 0, 1A, 9C, AGnum cards[E$>+O] =

1, 9, A, B, ?, C, M, @, G, 10, 11, 19, 1A,  1B, 1?, 1C, 1M, 1@, 1G, 90, 91, 99, 9A, 9B, 9?, 9C,  9M, 9@, 9G, A0, A1, A9, AA, AB, A?, AC, AM, A@, AG,  B0, B1, B9, BA, BB, B?, BC, BM, B@, BG, ?0, ?1, ?9

string S&I+S[>R&S] = lubs),Diamonds),Oearts),S'ades)getReady(!"ile y # R&$DS

detailoo'(

end!"ile%inis"&'(sto'

getReady(limit = E$>+Oy = 0

return

detailoo'(66 in'ut t"e 'layers cardindex = random(limit'layerard = cards[index]x = >R&S;1

!"ile 'layerard # T&$DS[x]x = x / 1

end!"ile'layerard$um = 'layerard / T&$DS[x]'layerardSuit = S&I+S[x]

66 moe t"e cards u' (taLe out 'layer cardx = index!"ile x # (limit / 1

(%

Page 55: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 55/56

Programming Logic and Design, 6e Solutions 6-

cards[x] = cards[x*1]x = x * 1

end!"ilelimit = limit / 1

66 in'ut t"e com'uters cardindex = random(limitcom'uterard = cards[index]x = >R&S;1!"ile com'uterard # T&$DS[x]

x = x / 1end!"ilecom'uterard$um = com'uterard / T&$DS[x]com'uterardSuit = S&I+S[x]

66 moe t"e cards u' (taLe out com'uters cardx = index!"ile x # (limit / 1

cards[x] = cards[x*1]x = x * 1

end!"ilelimit = limit / 1out'ut layers card- ), 'layerard$um, o% ),'layerardSuit

out'ut om'uters card- ), com'uterard$um, o% ),com'uterardSuit

i% 'layerard$um . com'uterard$umout'ut layer !ins "andU)'layerKin = 'layerKin * 1

elsei% com'uterard$um . 'layerard$um

out'ut om'uter !ins "andU)com'uterKin = com'uterKin * 1

elseout'ut +ieU)tie = tie * 1

endi%endi%

y = y * 1return

%inis"&'(out'ut End o% game)

return

/p or Discussion

1/. ! train schedule is an eeryday, real-life e'ample of an array. )hin of at leastfour more.

 Answer 

((

Page 56: 169467420-Ch06-Logic6e-Solutions (1)

7/26/2019 169467420-Ch06-Logic6e-Solutions (1)

http://slidepdf.com/reader/full/169467420-ch06-logic6e-solutions-1 56/56

Programming Logic and Design, 6e Solutions 6-

Student answers will vary but might include:

• tax table

• life expectancy table based on year of birth

• insurance company height and weight table

• loan amortization schedule• company salary schedule

• instructor’s grading scale

• shipping charges based on distance

• sliding fees for a service based on income

1. &ery element in an array always has the same data type. 2hy is this necessaryH

 Answer 

Every element in an array has the same data type which forcesevery element in an array to be the same size in bytes. Therefore, itbecomes possible to use a subscript to evenly measure anelements distance from the start of an array. !or example, if youuse a data type that occupies exactly four bytes of memory thenthe array element at subscript " is " bytes away from the beginningof the array, the element at subscript # is $ bytes away, theelement at subscript % is & bytes away, and so on.

(6


Recommended