You are on page 1of 56

Programming Logic and Design, 6e

Solutions 6-1

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 reverse order of their entry.

Answer: A 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()

Programming Logic and Design, 6e while index < SIZE getNumbers() endwhile finishUp() stop getReady() index = 0 return getNumbers() output “Enter a number for position ”, index input numbers[index] index = index + 1 return finishUp() output “The numbers in reverse order are: ” while index > 0 index = index – 1 output numbers[index] endwhile return

Solutions 6-2

b. Modify the reverse-display program so that the user can enter up to 10 numbers until a sentinel value is entered. Answer: A sample solution follows Flowchart:

Programming Logic and Design, 6e

Solutions 6-3

Pseudocode:
start Declarations num index num SIZE = 10 num numbers[SIZE] = 0,0,0,0,0,0,0,0,0,0 string CONTINUE = “Y” string moreNumbers = CONTINUE getReady() while index < SIZE AND moreNumbers equal to CONTINUE getNumbers() endwhile finishUp() stop getReady() index = 0 output “Do you want to enter a number? (Y/N)” input moreNumbers return getNumbers() output “Enter a number for position ”, index input numbers[index] index = index + 1

Programming Logic and Design, 6e output “Do you want to enter more numbers? (Y/N)” input moreNumbers return finishUp() output “The numbers in reverse order are: ” while index > 0 index = index – 1 output numbers[index] endwhile return

Solutions 6-4

2.

a. Design the logic for a program that allows a user to enter 10 numbers, then displays each and its difference from the numeric average of the numbers.

Answer: A sample solution follows Flowchart:

Programming Logic and Design, 6e

Solutions 6-5

Pseudocode:
start Declarations num index num sum num avg num SIZE = 10 num numbers[SIZE] = 0,0,0,0,0,0,0,0,0,0 getReady() while index < SIZE getNumbers() endwhile finishUp() stop getReady() index = 0 sum = 0 return getNumbers() output “Enter a number for position ”, index input numbers[index] sum = sum + numbers[index] index = index + 1 return finishUp() avg = sum/SIZE index = 0 while index < SIZE output numbers[index], avg – numbers[index] index = index + 1 endwhile return

b. Modify the program in Exercise 2a so that the user can enter up to 10 numbers until a sentinel value is entered. Answer: A sample solution follows Flowchart:

Programming Logic and Design. 6e Solutions 6-6 Pseudocode: start Declarations num index num sum num avg num actualSize num SIZE = 10 num numbers[SIZE] = 0 .

Programming Logic and Design. index input numbers[index] sum = sum + numbers[index] index = index + 1 output “Do you want to enter more numbers? (Y/N)” input moreNumbers return Solutions 6-7 finishUp() actualSize = index if actualSize > 0 then avg = sum/actualSize index = 0 while index < actualSize output numbers[index]. and residents’ ages range from 0 through 105. The output is a list of all 22 districts and the number of residents in each. The districts are numbered 1 through 22. a. The city of Cary is holding a special census. Design a program that allows a clerk to go through the cards. avg – numbers[index] index = index + 1 endwhile endif return 3. Answer: A sample solution follows Flowchart: . entering the district for each citizen until an appropriate sentinel value is entered. The city has collected data on cards that each hold the voting district and age of a citizen. 6e string CONTINUE = “Y” string moreNumbers = CONTINUE getReady() while index < SIZE AND moreNumbers = CONTINUE getNumbers() endwhile finishUp() stop getReady() index = 0 sum = 0 output “Do you want to enter a number? (Y/N)” input moreNumbers return getNumbers() output “Enter a number for position ”.

6e Solutions 6-8 Pseudocode: start Declarations num district num DIS_SIZE = 22 .Programming Logic and Design.

QUIT. and 65 and older. record not counted” endif output “Enter a district number or ”. 6e num districtCnt[SIZE] = 0 num QUIT = -1 getReady() while district not equal to QUIT collectData() endwhile finishUp() stop getReady() output “Enter a district number or ”. 46 through 64. Modify Exercise 3a so the clerk enters both the district and age on each card. districtCnt[district] district = district + 1 endwhile return b. “ to quit” input district return Solutions 6-9 collectData() if district >= 1 AND district <= SIZE then districtCnt[district-1] = districtCnt[district-1] + 1 else output “Invalid district. 18 through 30. 31 through 45. “ to quit” input district return finishUp() district = 0 while district < DIS_SIZE output district+1.Programming Logic and Design. Answer: A sample solution follows Flowchart: . Produce a count of the number of residents in each of the 22 districts and a count of residents in each of the following age groups: under 18. QUIT.

6e Solutions 6-10 Pseudocode: start Declarations .Programming Logic and Design.

record not counted” endif else output “Invalid district. QUIT. ageCnt[x] x = x + 1 . districtCnt[district] district = district+1 endwhile x = 0 while x < AGE_SIZE output AGE_GROUPS[x].Programming Logic and Design. 18. “18 through 30”. “46 through 64”. “ to quit” input district return finishUp() district = 0 while district < DIS_SIZE output district+1. 31. 65 string AGE_GROUPS = “under 18”. “ to quit” input district return countAges() if district >= 1 AND district <= SIZE then output “Enter the citizen’s age” input age if age >=0 AND age <= MAX_AGE then districtCnt[district-1] = districtCnt[district-1]+1 x = AGE_SIZE – 1 while age < AGE_RANGE[x] x = x – 1 endwhile ageCnt[x] = ageCnt[x] + 1 else output “Invalid age. QUIT. “65 and older” getReady() while district not equal to QUIT countAges() endwhile finishUp() stop getReady() output “Enter a district number or ”. “31 through 45”. 6e Solutions 6-11 num age num district num x num QUIT = -1 num MAX_AGE = 105 num AGE_SIZE = 5 num ageCnt[SIZE] = 0 num DIS_SIZE = 22 num districtCnt[DIS_SIZE] = 0 num AGE_RANGE = 0. 46. record not counted” endif output “Enter a district number or ”.

a. The Midville Park District maintains five soccer teams. Team Number 1 2 3 4 5 Team Name Goal Getters The Force Top Guns Shooting Stars Midfield Monsters Answer: A sample solution follows Flowchart: . as shown in the table. Design a program that accepts a player’s team number and displays the player’s team name.Programming Logic and Design. 6e endwhile return Solutions 6-12 4.

“The Force”. “ to quit” input teamNum return displayTeamName() if teamNum >= 1 AND teamNum <= SIZE then output teamNum. QUIT. “Shooting Stars”. “Top Guns”. 6e Solutions 6-13 Pseudocode: start Declarations num teamNum num QUIT = -1 num SIZE = 5 string teams[SIZE] = “Goal Getters”. “Midfield Monsters” getReady() while teamNum not equal to QUIT displayTeamName() endwhile finishUp() stop getReady() output “Enter the player’s team number or ”. “ to quit” input teamNum return finishUp() output “End of program” return b. teams[teamNum-1] else output “Invalid team number” endif output “Enter the player’s team number or ”. QUIT.Programming Logic and Design. the program displays a count of the number of players registered for each. Answer: A sample solution follows Flowchart: . Modify the Midville Park District program so that after the last player has been entered.

“Top Guns”. 6e Solutions 6-14 Pseudocode: start Declarations num teamNum num QUIT = -1 num SIZE = 5 num teamCnt[SIZE] = 0 string teams[SIZE] = “Goal Getters”. “The Force”. “Midfield Monsters” getReady() while teamNum not equal to QUIT countTeams() endwhile finishUp() . “Shooting Stars”.Programming Logic and Design.

Each classroom can contain any number of students up to 35. QUIT.Programming Logic and Design. Watson Elementary School contains 30 classrooms numbered 1 through 30. teams[teamNum-1] teamCnt[teamNum-1] = teamCnt[teamNum-1] + 1 else output “Invalid team number” endif output “Enter the player’s team number or ”. QUIT. “ to quit” input teamNum return countTeams() if teamNum >= 1 AND teamNum <= SIZE then output teamNum. Write a program that accepts data for each student in the school—student ID. a. “ to quit” input teamNum return finishUp() teamNum = 0 while teamNum < SIZE output teamNum+1. Answer: A sample solution follows Flowchart: . 6e stop Solutions 6-15 getReady() output “Enter the player’s team number or ”. teamCnt[teamNum] teamNum = teamNum + 1 endwhile return 5. Design a program that lists the total points scored for each of the 30 classrooms. classroom number. Each student takes an achievement test at the end of the school year and receives a score from 0 through 100. and score on the achievement test.

Programming Logic and Design. 6e Solutions 6-16 Pseudocode: start Declarations num stuID num classNum num score num SIZE = 30 num QUIT = 9999 num totalPnts[SIZE] = 0 getReady() while stuID not equal to QUIT collectStudentData() endwhile finishUp() stop .

“ to quit” input stuID return Solutions 6-17 collectStudentData() output “Enter the classroom number and score for student ”. QUIT. Modify the Watson Elementary School program so that each classroom’s average of the test scores is output. rather than each classroom’s total. 6e getReady() output “Enter a student ID or ”. stuID input classNum.Programming Logic and Design. QUIT. Answer: A sample solution follows Flowchart: . “ to quit” input stuID return finishUp() classNum = 0 while classNum < SIZE output classNum+1. score if classNum >= 1 AND classNum <= SIZE then totalPnts[classNum-1] = totalPnts[classNum-1] + score else output “Invalid classroom number” endif output “Enter a student ID or ”. totalPnts[classNum] classNum = classNum + 1 endwhile return b.

Programming Logic and Design. 6e Solutions 6-18 Pseudocode: start Declarations num stuID num classNum num score .

QUIT.Programming Logic and Design. stuID input classNum. avg classNum = classNum + 1 endwhile return 6.49 1. “ to quit” input stuID return finishUp() while classNum < SIZE if classCnt[classNum] = 0 then avg = 0 else avg =totalPnts[classNum]/classCnt[classNum] endif output classNum+1. “ to quit” input stuID return Solutions 6-19 collectStudentData() output “Enter the classroom number and score for student ”. The Billy Goat Fast-Food restaurant sells the following products: Product Cheeseburger Pepsi Chips Price ($) 2.00 0. score if classNum >= 1 AND classNum <= SIZE then totalPnts[classNum-1] = totalPnts[classNum-1] + score classCnt[classNum-1] = classCnt[classNum-1] + 1 else output “Invalid classroom number” endif output “Enter a student ID or ”. display its price or the message . 6e num avg num SIZE = 30 num QUIT = 9999 num totalPnts[SIZE] = 0 num classCnt[SIZE] = 0 getReady() while stuID not equal to QUIT collectStudentData() endwhile classNum = 0 finishUp() stop getReady() output “Enter a student ID or ”. QUIT.59 Design the logic for an application allows a user to enter an order item continuously until a sentinel value is entered. After each item.

Answer: A sample solution follows Flowchart: . we do not carry that” as output.Programming Logic and Design. After all items have been entered. display the total price for the order. 6e Solutions 6-20 “Sorry.

6e Solutions 6-21 .Programming Logic and Design.

Input includes each employee’s department number. 6e Solutions 6-22 Pseudocode: start Declarations string itemOrdered num x num found = 0 num total = 0 num SIZE = 3 string QUIT = “ZZZZ” num PRICE[SIZE] = 2. “Pepsi”. Design the application logic for a company that wants a report containing a breakdown of payroll by department. The department names are shown in the accompanying table. . total return 7. “ to complete your order” input itemOrdered return finishUp() output “Your order total is: $”. 1. hourly salary.Programming Logic and Design. 0. “ to complete your order” input itemOrdered return detailLoop() found = 0 x = 0 while x < SIZE if itemOrdered = PRODUCT[x] then output PRICE[x] x = SIZE found = 1 total = total + PRICE[x] else x = x + 1 endif endwhile if found = 0 then output ERROR_MSG endif output “Enter an item or ”. QUIT.59 string PRODUCT[SIZE] = “Cheeseburger”. “Chips” string ERROR_MSG = “Sorry. we do not carry that” getReady() while itemOrdered not equal to QUIT detailLoop() endwhile finishUp() stop getReady() output “Enter an item or ”. The output is a list of the seven departments in the company and the total gross payroll (rate times hours) for each department.49. and number of hours worked. QUIT.

Programming Logic and Design. 6e Solutions 6-23 Department Number 1 2 3 4 5 6 7 Department Name Personnel Marketing Manufacturing Computer Services Sales Accounting Shipping Answer: A sample solution follows Flowchart: .

“Sales”. “Computer Services”. “Marketing”. “Accounting”. “Manufacturing”. 6e Solutions 6-24 Pseudocode: start Declarations num deptNum num salary num hrsWorked num SIZE = 7 num totalGross[SIZE] = 0 string DEPTS[SIZE] = “Personnel”.Programming Logic and Design. “Shipping” .

the total withholding for all employees.Programming Logic and Design.01 – 350. Compute each employee’s gross pay (hours times rate).00 200. and number of hours worked” input deptNum. display the sum of all the hours worked. totalGross[deptNum] deptNum = deptNum + 1 endwhile return 8. Allow a user to continuously input employees’ names until an appropriate sentinel value is entered. the total gross payroll.00 500.00 – 200.000 Withholding Percent (%) 10 14 18 22 Answer: A sample solution follows Flowchart: . Weekly Gross Pay 0. and the total net payroll. hrsWorked return finishUp() deptNum = 0 while deptNum < SIZE output deptNum+1. withholding tax percentage (based on the accompanying table).01 – 500. Display all the results for each employee. salary. hrsWorked return detailLoop() if deptNum >= 1 AND deptNum <= SIZE then totalGross[deptNum-1] = totalGross[deptNum-1] + (hrsWorked * salary) else output “Invalid department number” endif output “Enter the department number. Design a program that computes pay for employees.00 350. DEPTS[deptNum]. Also input each employee’s hourly wage and hours worked. salary. After the last employee has been entered. hourly salary. and number of hours worked” input deptNum.01 – up. 6e getReady() while not eof detailLoop() endwhile finishUp() stop Solutions 6-25 getReady() output “Enter the department number. and net pay (gross pay minus withholding tax). withholding tax amount. hourly salary.

Programming Logic and Design. 6e Solutions 6-26 Pseudocode: start Declarations string empName num salary .

01 getReady() while empName not equal to QUIT detailLoop() endwhile finishUp() stop getReady() output “Enter an employee name or ”. 0.18.22 num WITH_RANGE[SIZE] = 0. totalWithTax. QUIT. totalNetPay return . totalGrossPay. netPay output “Enter an employee name or ”.14. withTax. 0. 350. “ to quit” input empName return finishUp() output totalHrsWorked. 500.10. WITH_RATE[x] * 100. 200. hoursWorked grossPay = hrsWorked * salary totalHrsWorked = totalHrsWorked + hrsWorked totalGrossPay = totalGrossPay + grossPay x = SIZE – 1 while grossPay < WITH_RANGE[x] x = x – 1 endwhile withTax = grossPay * WITH_RATE[x] totalWithTax = totalWithTax + withTax netPay = grossPay – withTax totalNetPay = totalNetPay + netPay output empName. 0. grossPay.01. “’s hourly wage and hours worked” input salary. “ to quit” input empName return Solutions 6-27 detailLoop() output “Enter ”. 6e num hrsWorked num x num grossPay num withTax num netPay num totalGrossPay = 0 num totalHrsWorked = 0 num totalWithTax = 0 num totalNetPay = 0 string QUIT = “zzzz” num SIZE = 4 num WITH_RATE[SIZE] = 0.01.Programming Logic and Design. empName. QUIT.

6e Solutions 6-28 9. Create an application that accepts an event number. The final price includes a discount based on the accompanying table. The Perfect Party Catering Company hosts events for clients. if any of these is not valid. day. and numeric month.00 16. day.00 Design the logic for an application that outputs each event number. and price for the party after discount. number of guests. The valid meal codes are shown in the accompanying table. year. continue to prompt the user until it is. The application should also accept the number of guests that will attend the event and a numeric meal code that represents the entrée the event hosts will serve.50 18. the event host’s last name. and year values representing the event date. meal code. The gross total price for the party is the meal price per guest times the number of guests. entrée name. validated date. gross total price for the party. As each client’s data is entered. verify that the month. Number of Guests 1-25 26-50 51-100 101-250 251 and over Discount ($) 0 75 125 200 300 Answer: A sample solution follows Flowchart: . and meal code are valid.50 19.Programming Logic and Design. host name. Code 1 2 3 4 Entrée Roast beef Salmon Linguine Chicken Price per Person ($) 24.

Programming Logic and Design. 6e Solutions 6-29 Pseudocode: start Declarations num eventNum string lastName num month num day num year .

day. 125.Programming Logic and Design. 251 num DISCOUNT[DSIZE] = 0. grossPrice. “/”. lastName. ENTREES[mealCode-1]. numOfGuests. 51. year endwhile return checkDate() invalidDate = 0 if (month < 1 OR month > 12 OR day < 1 OR day > 31 OR year < 2009 OR year > 2012) then . mealCode. 300 getReady() detail() finishUp() stop getReady() output “Enter the event number. host’s last name. mealCode return detail() validateMealCode() validateDate() grossPrice = PRICE[mealCode-1] * numOfGuests x = DSIZE – 1 while numOfGuests < DIS_RANGE[x] x = x – 1 endwhile output eventNum. “Chicken” num PRICE[MSIZE] = 24. 75. 101. “Salmon”. please reenter” input month. 19. lastName. 200.50. day. 6e Solutions 6-30 num numOfGuests num mealCode num x num invalidDate = 0 num grossPrice num MSIZE = 4 num DSIZE = 5 string ENTREES[MSIZE] = “Roast beef”.50. 26. month. “/”. (grossPrice – DISCOUNT[x]) return validateMealCode() while mealCode < 1 OR mealCode > MSIZE output “Invalid meal code. day. please reenter” input mealCode endwhile return validateDate() checkDate() while invalidDate = 1 ouput “Invalid date. and event date (as month. 16. and year)” input eventNum. “Linguine”. 18 num DIS_RANGE[DSIZE] = 1. day. month. year. year output “Enter the number of guests and the meal code” input numOfGuests.

Design an application that accepts reader data and. marital status. The Marketing department has collected reader survey records containing the age. 30–39. gender. and annual income of readers. 20–29. and 50 and older. 6e invalidDate = 1 Solutions 6-31 else if day = 31 AND (month = 4 OR month = 6 OR month = 9 OR month = 11) then invalidDate = 1 else if day > 28 AND month = 2 then invalidDate = 1 endif endif endif return finishUp() output “End of program” return 10. 40–49.Programming Logic and Design. Daily Life Magazine wants an analysis of the demographic characteristics of its readers. produces a count of readers by age groups as follows: under 20. a. when data entry is complete. Answer: A sample solution follows Flowchart: .

6e Solutions 6-32 Pseudocode: start Declarations num age num income num x string gender string maritalStatus num SIZE = 5 num ageCnt[SIZE] = 0 .Programming Logic and Design.

gender. maritalStatus. 6e Solutions 6-33 num AGE_RANGE[SIZE] = 0. 20. Answer: A sample solution follows Flowchart: . “30 through 39”. and income of a reader” input age. maritalStatus. and so on. “50 and older” getReady() while not eof detailLoop() endwhile finishUp() stop getReady() output “Enter the age. 30. marital status. ageCnt[x] x = x + 1 endwhile return b. income return detailLoop() x = SIZE – 1 while age < AGE_RANGE[x] x = x – 1 endwhile ageCnt[x] = ageCnt[x] + 1 output “Enter the age. gender. marital status. under 20 males. gender. gender. “40 through 49”. 50 string AGE_GROUPS[SIZE] = “under 20”. under 20 females. 40.Programming Logic and Design. income return finishUp() x = 0 while x < SIZE output AGE_GROUPS[x]. Modify the Daily Life Magazine program so that it produces a count of readers by gender within age group—that is. and income of a reader” input age. “20 through 29”.

Programming Logic and Design. 6e Solutions 6-34 Pseudocode: start Declarations num age num income num x string gender string maritalStatus num SIZE = 5 .

income return detailLoop() x = SIZE – 1 while age < AGE_RANGE[x] x = x – 1 endwhile if gender = “male” then maleCnt[x] = maleCnt[x] + 1 else femaleCnt[x] = femaleCnt[x] + 1 endif output “Enter the age. income return finishUp() x = 0 while x < SIZE output AGE_GROUPS[x]. 40. $30. gender. “30 through 39”. Modify the Daily Life Magazine program so that is produces a count of readers by annual income groups as follows: under $30. gender. femaleCnt[x] x = x + 1 endwhile return c. “20 through 29”. and $70. Answer: A sample solution follows Flowchart: . 50 string AGE_GROUPS[SIZE] = “under 20”. maritalStatus.999.000. maleCnt[x]. and income of a reader” input age. 50. marital status. gender. and income of a reader” input age.Programming Logic and Design.000–$69. gender. 6e Solutions 6-35 num maleCnt[SIZE] = 0 num femaleCnt[SIZE] = 0 num AGE_RANGE[SIZE] = 0. 20.000–$49. “50 and older” getReady() while not eof detailLoop() endwhile finishUp() stop getReady() output “Enter the age. “40 through 49”.000 and up. 30. marital status. maritalStatus.999.

6e Solutions 6-36 Pseudocode: start Declarations num age num income num x string gender string maritalStatus num SIZE = 4 num incomeCnt[SIZE] = 0 num INCOME_RANGE[SIZE] = 0. 30000. 70000 .Programming Logic and Design. 50000.

The sale amount is expressed in whole dollars. as shown in the accompanying table. gender.Programming Logic and Design.000-$49. getReady() while not eof detailLoop() endwhile finishUp() stop getReady() output “Enter the age. Salespeople earn a commission that differs for each sale.000 and up” 11. and income of a reader” input age. Glen Ross Vacation Property Sales employs seven salespeople. . incomeCnt[x] x = x + 1 endwhile return “$70. income return detailLoop() x = SIZE – 1 while age < INCOME_RANGE[x] x = x – 1 endwhile incomeCnt[x] = incomeCnt[x] + 1 output “Enter the age. ID Number 103 104 201 319 367 388 435 Salesperson Name Darwin Kratz Shulstad Fortune Wickert Miller Vick When a salesperson makes a sale. based on a 24hour clock. “$50. gender.999”. gender. a record is created including the date. marital status. based on the rate schedule in the accompanying table. time. income return finishUp() x = 0 while x < SIZE output INCOME_GROUPS[x]. The time is expressed in hours and minutes. “$30. gender.999”.000”. maritalStatus. maritalStatus.000$69. marital status. and dollar amount of the sale. 6e Solutions 6-37 string INCOME_GROUPS[SIZE] = “under $30. and income of a reader” input age.

125.000 .999 51. name. and total commissions Answer: A sample solution follows Flowchart: . 6e Solutions 6-38 Sale Amount ($) 0 – 50.999 126.Programming Logic and Design. total sales.200.000 . A list of each salesperson number.000 and up Commission Rate (%) 4 5 6 7 Design an application that produces each of the following: a.999 201.

Programming Logic and Design. 6e Solutions 6-39 Pseudocode: .

319. “Miller”. 201. 104. 201000 num COMM_RATE[SSIZE] = 0. saleAmt totalSales[person] = totalSales[person] + saleAmt x = SSIZE – 1 while saleAmt < SALE_AMT[x] x = x – 1 endwhile totalComm[person] = totalComm[person] + (COMM_RATE[x] * saleAmt) endif output “Enter the salesperson ID number” input idNum return finishUp() x = 0 while x < PSIZE output ID_NUM[x]. 388. 51000. 0. hours. “Wickert”. “Kratz”. “Vick” num SALE_AMT[SSIZE] = 0. 435 string NAME[PSIZE] = “Darwin”.05. 0. 6e Solutions 6-40 start Declarations num date num hours num minutes num saleAmt num idNum num person num x string date num SSIZE = 4 num PSIZE = 7 num totalSales[PSIZE] = 0 num totalComm[PSIZE] = 0 num ID_NUM[PSIZE] = 103. “Shulstad”. 126000.04. 0. totalComm[x] .Programming Logic and Design. “Fortune”.06. minutes.07 getReady() while not eof detailLoop() endwhile finishUp() stop getReady() output “Enter the salesperson ID number” input idNum return detailLoop() person = 0 while person < PSIZE AND idNum not equal to ID_NUM[person] person = person + 1 endwhile if person = PSIZE then output “An invalid number has been entered. NAME[x]. please try again” else input date. 367. totalSales[x].

and the total sales for the month for all salespeople Answer: A sample solution follows Flowchart: . 6e x = x + 1 endwhile return Solutions 6-41 b. A list of each each month of the year as both a number and a word (for example.Programming Logic and Design. “01 January”).

Programming Logic and Design. minutes num month. 6e Solutions 6-42 Pseudocode: start Declarations num hours. day. year .

“Miller”. day. 201000 num COMM_RATE[SSIZE] = 0. “July”. saleAmt while month < 1 OR month > 12 OR day < 1 OR day > 31 output “Invalid date. “March” “April”. day. “August”. 51000. 6e Solutions 6-43 num saleAmt num idNum num person.07 string MONTHS[MSIZE] = “January”. MONTHS[x]. please try again” else input month.05. please reenter” input month. “June”.Programming Logic and Design. totalSales[x] x = x + 1 endwhile return . “November”. “Wickert”. “December” getReady() while not eof detailLoop() endwhile finishUp() stop getReady() output “Enter the salesperson ID number” input idNum return detailLoop() person = 0 while person < PSIZE AND idNum not equal to ID_NUM[person] person = person + 1 endwhile if person = PSIZE then output “An invalid number has been entered. 435 string NAME[PSIZE] = “Darwin”. 126000. “Vick” num SALE_AMT[SSIZE] = 0. “September”. year endwhile totalSales[month-1] = totalSales[month-1] + saleAmt endif output “Enter the salesperson ID number” input idNum return finishUp() x = 0 while x < MSIZE output x+1. 367. 0. hours.04. 319. “Kratz”. year. 388. minutes. “October”. “February”.06. “May”. 0. 0. 104. “Fortune”. x num SSIZE = 4 num PSIZE = 7 num MSIZE = 12 num totalSales[MSIZE] = 0 num totalComm[PSIZE] = 0 num ID_NUM[PSIZE] = 103. 201. “Shulstad”.

please try again” else input month. “Miller”. “Fortune”. 435 string NAME[PSIZE] = “Darwin”.06. minutes. saleAmt . 0. year num saleAmt num idNum num time. day. 13. Pseudocode: start Declarations num hours. 201000 num COMM_RATE[SSIZE] = 0. x num SSIZE = 4 num PSIZE = 7 num TSIZE = 4 num totalSales[TSIZE] = 0 num totalComm[TSIZE] = 0 num ID_NUM[PSIZE] = 103. hours. “Shulstad”. “Vick” num SALE_AMT[SSIZE] = 0. 19 string TIMES[TSIZE] = “00-05". day. minutes num month. 126000. 0. “06-12”. “Wickert”. 201. 388. 104. “Kratz”. based on hour of the day: 00–05. “13-18”.05. 06–12.07 num TIME_RANGE[TSIZE] = 0. 367. “19-23” getReady() while not eof detailLoop() endwhile finishUp() stop getReady() output “Enter the salesperson ID number” input idNum return detailLoop() person = 0 while person < PSIZE AND idNum not equal to ID_NUM[person] person = person + 1 endwhile if person = PSIZE then output “An invalid number has been entered. 0. A list of total sales as well as total commissions earned by all salespeople for each of the following time frames. and 19–23 Answer: A sample solution follows Flowchart: The flowchart will be similar to those shown in parts a and b. 6e Solutions 6-44 c. 51000. year. 13–18.Programming Logic and Design. 6. 319.04.

and DEBUG06-03. 6e Solutions 6-45 while month < 1 OR month > 12 OR day < 1 OR day > 31 output “Invalid date. totalSales[x]. and DEBUG06-03.txt.txt. Each file starts with some comments that describe the problem. such as “It remains to be seen. totalComm[x] x = x + 1 endwhile return Find the Bugs 12.txt. Answer: Please see the DEBUG06-01. Following the comments.” start Declarations num LIMIT = 8 . DEBUG06-02. Game Zone 13. year endwhile time = TSIZE-1 while hours < TIME_RANGE[time] time = time – 1 endwhile totalSales[time] = totalSales[time] + saleAmt x = SSIZE – 1 while saleAmt < SALE_AMT[x] x = x – 1 endwhile totalComm[time] = totalComm[time] + (COMM_RATE[x]*saleAmt) endif output “Enter the salesperson ID number” input idNum return finishUp() x = 0 while x < TSIZE output TIME[x]. day. Create the logic for a Magic 8 Ball game in which the user enters a question such as “What does my future hold?” The computer randomly selects one of eight possible vague answers. Your student disk contains files named DEBUG06-01.Programming Logic and Design. please reenter” input month. each file contains pseudocode that has one or more bugs you must find and correct.txt. Comments are lines that begin with two slashes (//).txt. DEBUG06-02.txt solution files.

yes”. Create the logic for an application that contains an array of 10 multiple-choice questions related to your favorite hobby. B. or C. QUIT. After the user answers all the questions. Students should provide 10 questions. “Reply hazy. “Cannot predict now”. “ to quit” input userQuestion return detailLoop() index = random(LIMIT) output ANSWERS[index] output “Enter a question or ”. “ to quit” input userQuestion return finishUp() output “End of program” return Solutions 6-46 14. B. for brevity this solution contains only five questions. Answer: A sample solution follows Pseudocode (Please note. or C as the answer—if not. display “The correct answer is” and the letter of the correct answer. keep prompting the user until a valid response is entered. “Signs point to yes”. try again”. otherwise. “Outlook not so good”.): start . 6e num index string userQuestion string QUIT = “zzzz” string ANSWERS[LIMIT] = “As I see it. display “Correct!”. Each question contains three answer choices. “My sources say no” getReady() while userQuestion <> QUIT detailLoop() endwhile finishUp() stop getReady() output “Enter a question or ”.Programming Logic and Design. Also create a parallel array that holds the correct answer to each question— A. Display each question and verify that the user enters only A. If the user responds to a question correctly. QUIT. display the number of correct and incorrect answers. “It remains to be seen”. “It is certain”.

“What is the name of Earth’s moon?”. “How many moons does Pluto have?”. Dipper. 2. “C”. “A”. “B” getReady() while x < SIZE detailLoop() endwhile finishUp() stop getReady() x = 0 output QUESTIONS[x]. B. 10”. please enter either A.Programming Logic and Design. “A. 6e Solutions 6-47 Declarations num correct = 0 num incorrect = 0 num x num SIZE = 5 string guesses[SIZE] = “” string QUESTIONS[SIZE] = “What is the closest star to Earth?”. incorrect return . “How many miles (in trillions) are in a light year?”. CHOICES[x] input guesses[x] return finishUp() output “Number of correct = ”. C. Sol”. Europa. 6. correct output “Number of incorrect = ”. Jupiter. Luna. “A. B. 6. C. 93. Scopper. B. ANSWERS[x] incorrect = incorrect + 1 endif x = x + 1 output QUESTIONS[x]. B. 3”. C. B. CHOICES[x] input guesses[x] return detailLoop() while (guesses[x] not equal to “A” OR guesses[x] not equal to “B” OR guesses[x] not equal to “C”) output “Invalid answer. “What planet is the biggest?” string CHOICES[SIZE] = “A. Saturn. C. Earth” string ANSWERS[SIZE] = “C”. Sun”. C. B. or C” input guesses[x] endwhile if guesses[x] = ANSWERS[x] then output “Correct!” correct = correct + 1 else output “The correct answer is: “. “A. “A”. “A.

Additionally. Any higher combination beats a lower one. Figure 6-19 shows how the game might be played in a command-line environment. store it in an array. for example. the numeric dice values do not count. Decide the winner based on the following hierarchy of die values. Create the logic for a dice game. Answer: A sample solution follows Pseudocode: start Declarations num x num y num playerMatch num computerMatch num playerLargest num computerLargest num LIMIT = 6 num SIZE = 5 num playerDice[SIZE] = 0 num computerDice[SIZE] = 0 num playerValues[LIMIT] = 0 num computerValues[LIMIT] = 0 getReady() accumulateNums() findLargest() finishUp() stop getReady() // populate both arrays with random numbers x = 0 while x < SIZE playerDice[x] = random(LIMIT) computerDice[x] = random(LIMIT) x = x + 1 endwhile . if both players have three of a kind. which can be from 1 to 6 inclusive for each die. no matter what the values of the three dice are. The application displays all the values. For example. five of a kind beats four of a kind.Programming Logic and Design. the game does not recognize a full house (three of a kind plus two of a kind). 6e Solutions 6-48 15. • • • • Five of a kind Four of a kind Three of a kind A pair For this game. it’s a tie. As each random throw. The application randomly “throws” five dice for the computer and five dice for the player. a.

store these // values in arrays x = 0 while x < LIMIT y = 0 while y < SIZE if playerDice[y] = (x+1) then playerValues[x] = playerValues[x] + 1 endif if computerDice[y] = (x+1) then computerValues[x] = computerValues[x] + 1 endif y = y + 1 endwhile x = x + 1 endwhile return findLargest() // find the largest accumulated value – this will tell the // program the largest “of a kind” x = 0 computerLargest = 0 playerLargest = 0 while x < LIMIT-1 if computerValues[x+1] > computerValues[computerLargest] computerLargest = x + 1 endif if playerValues[x+1] > playerValues[playerLargest] playerLargest = x + 1 endif x = x + 1 endwhile computerMatch = computerValues[computerLargest] playerMatch = playerValues[playerLargest] return .Programming Logic and Design. 6e Solutions 6-49 // output what the computer rolled x = 0 output “Computer rolled: ” while x < SIZE output computerDice[x] x = x + 1 endwhile // output what player rolled x = 0 output “You rolled: ” while x < SIZE output playerDice[x] x = x + 1 endwhile return acculateNums() // accumulate how many of each number was rolled // by both the computer and the player.

Improve the dice game so that when both players have the same combination of dice. computerMatch. playerMatch. the higher value wins. For example. “ of a kind” output “You have ”. “ of a kind” if computerMatch > playerMatch then output “Computer wins” else if playerMatch > computerMatch then output “You win” else output “Tie” endif endif return b. two 6s beats two 5s. 6e Solutions 6-50 finishUp() output “Computer has ”.Programming Logic and Design. Answer: A sample solution follows Pseudocode: start Declarations num x num y num playerMatch num computerMatch num playerLargest num computerLargest num LIMIT = 6 num SIZE = 5 num playerDice[SIZE] = 0 num computerDice[SIZE] = 0 num playerValues[LIMIT] = 0 num computerValues[LIMIT] = 0 getReady() accumulateNums() findLargest() finishUp() stop getReady() x = 0 while x < SIZE playerDice[x] = random(LIMIT) computerDice[x] = random(LIMIT) x = x + 1 endwhile x = 0 .

“ of a kind” if computerMatch > playerMatch then output “Computer wins” else if playerMatch > computerMatch then . playerMatch. 6e output “Computer rolled: ” while x < SIZE output computerDice[x] x = x + 1 endwhile x = 0 output “You rolled: ” while x < SIZE output playerDice[x] x = x + 1 endwhile return Solutions 6-51 accumulateNums() x = 0 while x < LIMIT y = 0 while y < SIZE if playerDice[y] = x + 1 then playerValues[x] = playerValues[x] + 1 endif if computerDice[y] = x + 1 then computerValues[x] = computerValues[x] + 1 endif y = y + 1 endwhile x = x + 1 endwhile return findLargest() x = 0 computerLargest = 0 playerLargest = 0 while x < LIMIT-1 if computerValues[x+1] > computerValues[computerLargest] computerLargest = x + 1 endif if playerValues[x+1] > playerValues[playerLargest] playerLargest = x + 1 endif x = x + 1 endwhile computerMatch = computerValues[computerLargest] playerMatch = playerValues[playerLargest] return finishUp() output “Computer has ”.Programming Logic and Design. computerMatch. “ of a kind” output “You have ”.

Display a dash for each missing letter. display the word again.“-”. if the word is “banana” then when the user guesses “a”. Make sure that when a user makes a correct guess. “M”. “U”. “T”. “O”.“-”. filling in the guess if it was correct. After the user guesses “p”. 6e output “You win” Solutions 6-52 else endif endif return if computerLargest > playerLargest then output “Computer wins” else if playerLargest > computerLargest then output “You win” else output “Tie” endif endif 16.“-”.“-” string guess getReady() while numCorrect not equal to LENGTH detailLoop() endwhile finishUp() stop getReady() numCorrect = 0 output hiddenWord output “Please guess a letter” input guess .“-”. if the hidden word is “computer”. “E”. the display becomes “---p----”. all three “a” characters are filled in. For example. As the user enters each guess. in which the user guesses letters in a hidden word. For example. all the matching letters are filled in.Programming Logic and Design. “-”. Store the letters of a word in an array of characters.“-”. Answer: A sample solution follows Pseudocode: start Declarations num x num numCorrect num LENGTH = 8 string WORD[LENGTH] = “C”. first display “--------”. Design the logic for the game Hangman. Allow the user to continuously guess a letter until all the letters in the word are correctly guessed. “P”. “R” string hiddenWord[LENGTH] = “-”.

The other array is a string array and holds suits (“Clubs”. dealing a full deck with no repeated cards. select the card at position 49 (both the numeric value and the string). the number of times the computer wins. and “Spades”). The game should play for 26 rounds of War. 2 through 10. so the available card array is smaller by one. Jack. Create a War 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 the numeric value of the two cards. “Hearts”. compare their values. Move every higher-positioned card in the deck “down” one to fill in the gap.Programming Logic and Design. Queen. Only 51 cards remain in the deck after the player’s first card is dealt. and move the card that was in position 51 to position 50. Here are some hints: • • • Start by creating an array of all 52 playing cards. display a count of the number of times the player wins. Select a random number for the deck position of the player’s first card and assign the card at that array position to the player. In other words. 6e return detailLoop() x = 0 while x < LENGTH if WORD[x] = guess then numCorrect = numCorrect + 1 hiddenWord[x] = guess endif x = x + 1 endwhile output hiddenWord if numCorrect not equal to LENGTH output “Please guess a letter” input guess endif return finishUp() output “You guessed the word!” output hiddenWord return Solutions 6-53 17. and the number of ties. One array is numeric and holds the values 1 through 13 (representing Ace. move the card that was in position 50 to position 49. Create two parallel arrays that represent a standard deck of 52 playing cards. For this game. if the player’s first random number is 49. assume the lowest card is the Ace. When all the cards in the deck are exhausted. Create the arrays so that all 52 cards are represented. and King). • . Display the values of the player’s and computer’s cards. randomly select a card for the computer and “remove” the card from the deck. and determine the winner. “Diamonds”. In the same way.

26. 35. 42. 2. 13. 21. 25. 52 string SUITS[GROUPS] = “Clubs”. 4. 50. 51. 43. 45. 7. 20. 19. 28. 18. 14. 40. 3. 33. 8. 47. 15.“Hearts”. 16. 36. 39 num cards[LENGTH] = 1. 27. 37. 32. 13. 6. 44. 46. 31.“Diamonds”. 17.“Spades” getReady() while y < ROUNDS detailLoop() endwhile finishUp() stop getReady() limit = LENGTH y = 0 return detailLoop() // input the player’s card index = random(limit) playerCard = cards[index] x = GROUPS-1 while playerCard < BOUNDS[x] x = x – 1 endwhile playerCardNum = playerCard – BOUNDS[x] playerCardSuit = SUITS[x] // move the cards up (take out player card) x = index while x < (limit – 1) . 26. 41. 38. 11.Programming Logic and Design. 10. 12. 6e Solutions 6-54 Answer: A sample solution follows Pseudocode: start Declarations num x num y num limit num index num playerCard num playerCardNum num computerCard num computerCardNum num playerWin = 0 num computerWin = 0 num tie = 0 string playerCardSuit string computerCardSuit num LENGTH = 52 num GROUPS = 4 num ROUNDS = 26 num BOUNDS[GROUPS] = 0. 49. 24. 5. 30. 48. 23. 9. 29. 39. 34. 22.

A train schedule is an everyday. computerCardNum. playerCardNum. computerCardSuit if playerCardNum > computerCardNum output “Player wins hand!” playerWin = playerWin + 1 else if computerCardNum > playerCardNum output “Computer wins hand!” computerWin = computerWin + 1 else output “Tie!” tie = tie + 1 endif endif y = y + 1 return finishUp() output “End of game” return Solutions 6-55 Up for Discussion 18.Programming Logic and Design. “ of ”. 6e cards[x] = cards[x+1] x = x + 1 endwhile limit = limit – 1 // input the computer’s card index = random(limit) computerCard = cards[index] x = GROUPS-1 while computerCard < BOUNDS[x] x = x – 1 endwhile computerCardNum = computerCard – BOUNDS[x] computerCardSuit = SUITS[x] // move the cards up (take out computer’s card) x = index while x < (limit – 1) cards[x] = cards[x+1] x = x + 1 endwhile limit = limit – 1 output “Player’s card: ”. Think of at least four more. “ of ”. Answer: . playerCardSuit output “Computer’s card: ”. real-life example of an array.

if you use a data type that occupies exactly four bytes of memory then the array element at subscript 0 is 0 bytes away from the beginning of the array.Programming Logic and Design. the element at subscript 1 is 4 bytes away. 6e Solutions 6-56 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 19. Why is this necessary? Answer: Every element in an array has the same data type which forces every element in an array to be the same size in bytes. For example. Every element in an array always has the same data type. and so on. Therefore. . it becomes possible to use a subscript to evenly measure an element's distance from the start of an array. the element at subscript 2 is 8 bytes away.