Ch05 Logic6e Solutions

March 26, 2018 | Author: SophiaParm | Category: Input/Output, Control Flow, Text File, String (Computer Science), Software Bug


Comments



Description

Programming Logic and Design, 6eSolutions 5-1 Programming Logic and Design, 6th Edition Chapter 5 Exercises 1. What is output by each of the pseudocode segments in Figure 5-22? Answer: a. b. c. d. e. f. 2. 5, 22, 5 4, 6, 7 5, 6 Goodbye Goodbye Goodbye Goodbye Goodbye Goodbye Goodbye Goodbye Goodbye Hello Hello Hello Adios Adios Adios Adios Adios Adios Adios Adios Design the logic for a program that outputs every number from 1 through 10. Answer: A sample solution follows Flowchart: Programming Logic and Design, 6e Pseudocode: start Declarations num number housekeeping() number = 1 while number <= 10 detailLoop() endwhile finishUp() stop housekeeping() number = 1 return detailLoop() output number number = number + 1 return finishUp() output “End of program” return Solutions 5-2 Programming Logic and Design, 6e 3. Solutions 5-3 Design the logic for a program that outputs every number from 1 through 10 along with its square and cube. Answer: A sample solution follows Flowchart: Pseudocode: start Declarations num number num square num cube housekeeping() while number <= 10 detailLoop() endwhile finishUp() stop cube number = number + 1 return finishUp() output “End of program” return 4. Answer: A sample solution follows Flowchart: . square.Programming Logic and Design.Design the logic for a program that outputs every even number from 2 through 30. 6e Solutions 5-4 housekeeping() number = 1 return detailLoop() square = number * number cube = square * number output number. Programming Logic and Design. 6e Pseudocode: start Declarations num number housekeeping() while number <= 30 detailLoop() endwhile finishUp() stop housekeeping() number = 2 return detailLoop() output number number = number + 2 return finishUp() output “End of program” Solutions 5-5 . Programming Logic and Design. 6e Solutions 5-6 return 5. Answer: A sample solution follows Flowchart: Pseudocode: start Declarations num number housekeeping() while number >= 1 detailLoop() endwhile . Design the logic for a program that outputs numbers in reverse order from 10 down to 1. which are 10 percent of the original bill. and that the customer pays off the balance with equal monthly payments. Output the account number and name. that the customer makes no new purchases. Design an application that gets customer account data. a. The No Interest Credit Company provides zero-interest loans to customers. then output the customer’s projected balance each month for the next 10 months. Answer: A sample solution follows Flowchart: . customer name.1 return finishUp() output “End of program” return 6. and balance due. including an account number.Programming Logic and Design. Assume that there is no finance charge on this account. 6e Solutions 5-7 finishUp() stop housekeeping() number = 10 return detailLoop() output number number = number . Programming Logic and Design.10 string PROMPT1 = “Enter an account number” string PROMPT2 = “Enter the customer name and balance due” housekeeping() detail() finishUp() stop housekeeping() output PROMPT1 . 6e Solutions 5-8 Pseudocode: start Declarations num accountNum num balanceDue num month num paymentAmt string custName num PAY_RATE = 0. Modify the No Interest Credit Company application so it executes continuously for any number of customers until a sentinel value is supplied for the account number. balanceDue output accountNum. Answer: A sample solution follows Flowchart: . balanceDue month = month + 1 endwhile output PROMPT1 input accountNum return finishUp() output “End of program” return b.Programming Logic and Design. custName month = 1 paymentAmt = balanceDue * PAY_RATE while month <= 10 balanceDue = balanceDue – paymentAmt output month. 6e Solutions 5-9 input accountNum return detail() output PROMPT2 input custName. 6e Solutions 5-10 Pseudocode: start Declarations num accountNum num balanceDue num month num paymentAmt string custName num PAY_RATE = 0.10 string PROMPT1 = “Enter an account number or 999 to exit” string PROMPT2 = “Enter the customer name and balance due” housekeeping() while accountNum not equal to 999 detailLoop() endwhile finishUp() stop .Programming Logic and Design. The Some Interest Credit Company provides loans to customers at 1. and then the customer makes a payment equal to 5 percent of the current balance. including an account number. balanceDue output accountNum. Design an application that gets customer account data. Assume that when the balance reaches $10 or less. At the beginning of every month. Assume the customer makes no new purchases. Output the account number and name. Answer: A sample solution follows Flowchart: .5 percent interest is added to the balance. 6e Solutions 5-11 housekeeping() output PROMPT1 input accountNum return detailLoop() output PROMPT2 input custName. then output the customer’s projected balance each month for the next 10 months. the customer can pay off the account. customer name. a. 1. and balance due. custName month = 1 paymentAmt = balanceDue * PAY_RATE while month <= 10 balanceDue = balanceDue – paymentAmt output month. balanceDue month = month + 1 endwhile output PROMPT1 input accountNum return finishUp() output “End of program” return 7.5 percent interest per month.Programming Logic and Design. 6e Pseudocode: start Declarations num accountNum num balanceDue num month num paymentAmt string custName Solutions 5-12 .Programming Logic and Design. balanceDue output accountNum.Programming Logic and Design.05 num INTEREST_RATE = 0. balanceDue month = 11 endif endwhile output PROMPT1 input accountNum return finishUp() output “End of program” return b. Answer: A sample solution follows Flowchart: . Modify the Some Interest Credit Company application so it executes continuously for any number of customers until a sentinel value is supplied for the account number.015 string PROMPT1 = “Enter an account number” string PROMPT2 = “Enter the customer name and balance due” housekeeping() detail() finishUp() stop housekeeping() output PROMPT1 input accountNum return detail() output PROMPT2 input custName. custName month = 1 while month <= 10 balanceDue = balanceDue * (1 + INTEREST_RATE) if balanceDue > 10 then paymentAmt = balanceDue * PAY_RATE balanceDue = balanceDue – paymentAmt output month. 6e Solutions 5-13 num PAY_RATE = 0. balanceDue month = month + 1 else balanceDue = 0 output month. Programming Logic and Design. 6e Pseudocode: start Declarations Solutions 5-14 . or $9.00. Design an application that allows a user to input a price until an appropriate sentinel value is entered. the same item is 10 percent less than $9.015 string PROMPT1 = “Enter an account number or 999 to exit” string PROMPT2 = “Enter the customer name and balance due” housekeeping() while accountNum not equal to 999 detailLoop() endwhile finishUp() stop housekeeping() output PROMPT1 input accountNum return detailLoop() output PROMPT2 input custName. On the third day. balanceDue month = month + 1 else balanceDue = 0 output month. custName month = 1 while month <= 10 balanceDue = balanceDue * (1 + INTEREST_RATE) if balanceDue > 10 then paymentAmt = balanceDue * PAY_RATE balanceDue = balanceDue – paymentAmt output month. balanceDue month = 11 endif endwhile output PROMPT1 input accountNum return finishUp() output “End of program” return 8. on the second day. 6e Solutions 5-15 num accountNum num balanceDue num month num paymentAmt string custName num PAY_RATE = 0. an item that costs $10.00 on the first day costs 10 percent less. .05 num INTEREST_RATE = 0. Output is the price of every item on each day. For example.00. Secondhand Rose Resale Shop is having a seven-day sale during which the price of any unsold item drops 10 percent each day. or $8.10. balanceDue output accountNum.Programming Logic and Design. one through seven. Programming Logic and Design.10 string PROMPT1 = “Enter an item number or 999 to exit” string PROMPT2 = “Enter the description and price” housekeeping() while itemNumber not equal to 999 detailLoop() . 6e Solutions 5-16 Answer: A sample solution follows Flowchart: Pseudocode: start Declarations num itemNumber num price num day string description num TERM = 7 num REDUCTION = 0. if you deposit $100 for two years at 4 percent interest. at the end of one year you will have $104. The Howell Bank provides savings accounts that compound interest on a yearly basis. In other words.16. At the end of two years. Output the projected running total balance for each year for the next 20 years. you will have the $104 plus 4 percent of that. the account owner’s first and last names. price day = 0 while day < TERM day = day + 1 output day. Answer: A sample solution follows Flowchart: . price price = price * (1 – REDUCTION) endwhile output PROMPT1 input itemNumber return finishUp() output “End of program” return 9. and a balance. The program operates continuously until an appropriate sentinel value is entered for the account number. 6e Solutions 5-17 endwhile finishUp() stop housekeeping() output PROMPT1 input itemNumber return detailLoop() output PROMPT2 input description. Design a program that accepts an account number.Programming Logic and Design. or $108. Programming Logic and Design. and balance” housekeeping while accountNum does not equal 999 detailLoop() endwhile finishUp() stop housekeeping() output PROMPT1 . 6e Solutions 5-18 Pseudocode: start Declarations num accountNum num balance num year string firstName string lastName num INTEREST_RATE = 0. last name.04 num TERM = 20 string PROMPT1 = “Enter an account number or 999 to exit” string PROMPT2 = “Enter the first name. Answer: A sample solution follows Flowchart: . firstName. the apartment number (1 through 15). Each building contains 15 units that he rents for $800 per month each.Programming Logic and Design. the month (1 through 12). lastName year = 0 while year < TERM balance = balance * (1 + INTEREST_RATE) year = year + 1 output year. Design the application that would print 12 payment coupons for each of the 15 apartments in each of the 20 buildings. Roper owns 20 apartment buildings. balance output accountNum. and the amount of rent due. balance endwhile output PROMPT1 input accountNum return finishUp() output “End of program” return 10. Each coupon should contain the building number (1 through 20). Mr. 6e Solutions 5-19 input accountNum return detailLoop() output PROMPT2 input firstName. lastName. Programming Logic and Design. 6e Pseudocode: start Declarations num buildingNum num aptNum num month num TOTAL_BUILDS = 20 num TOTAL_UNITS = 15 num TOTAL_MONTHS = 12 num RENT = 800 string COUPON_HEADING = “Payment Coupon” housekeeping() while buildingNum <= TOTAL_BUILDS Solutions 5-20 . display the average star rating for the movie. The program executes continuously until a user enters a negative number to quit. 6e Solutions 5-21 detailLoop() endwhile finishUp() stop housekeeping() buildingNum = 1 return detailLoop() aptNum = 1 while aptNum <= TOTAL_UNITS month = 1 while month <= TOTAL_MONTHS output COUPON_HEADING output “Building Number: “. month output “Rent Due: “. reprompt the user continuously until a correct value is entered. Design a program for the Hollywood Movie Rating Guide. RENT month = month + 1 endwhile aptNum = aptNum + 1 endwhile buildingNum = buildingNum + 1 return finishUp() output “End of program” return 11. At the end of the program. aptNum output “Month: “. Answer: A sample solution follows Flowchart: . buildingNum output “Apartment Number: “. If a user enters a star value that does not fall in the correct range. in which users continuously enter a value from 0 to 4 that indicates the number of stars they are awarding to the Guide’s featured movie of the week.Programming Logic and Design. a. Programming Logic and Design. 6e Solutions 5-22 Pseudocode: start Declarations num numOfStars num count = 0 num total = 0 num avg string PROMPT = “Enter the star rating or a negative number to quit” housekeeping() while numOfStars >= 0 detailLoop() endwhile finishUp() stop housekeeping() output PROMPT input numOfStars return . 6e Solutions 5-23 detailLoop() while numOfStars > 4 OR numOfStars < 0 output “Please enter a value from 0 to 4” input numOfStars endwhile count = count + 1 total = total + numOfStars output PROMPT input numOfStars return finishUp() avg = total / count output “The average star rating is: ”. the program issues an appropriate message and continues with a new user. avg output “End of program” return b. Answer: A sample solution follows Flowchart: .Programming Logic and Design. Modify the movie-rating program so that a user gets three tries to enter a valid rating. After three incorrect entries. Programming Logic and Design. 6e Solutions 5-24 Pseudocode: start Declarations num numOfStars num count = 0 num total = 0 num attempts num avg num LIMIT = 3 string PROMPT = “Enter the star rating or a negative number to quit” . ” endif return finishUp() avg = total / count output “The average star rating is: ”. for each movie. Then. continuously accept starrating values until a negative number is entered. 6e Solutions 5-25 housekeeping() while numOfStars >= 0 AND attempts < LIMITS detailLoop() endwhile finishUp() stop housekeeping() output PROMPT input numOfStars attempts = 0 return detailLoop() while (numOfStars > 4 OR numOfStars < 0) AND attempts < LIMIT output “Please enter a value from 0 to 4” input numOfStars attempts = attempts + 1 endwhile if attempts < LIMIT then count = count + 1 total = total + numOfStars output PROMPT input numOfStars else output “You have failed to enter a valid rating. Answer: A sample solution follows Flowchart: .” output “The program will now end. Modify the movie-rating program so that the user is prompted continuously for a movie title until “ZZZZZ” is entered. avg output “End of program” return c.Programming Logic and Design. Display the average rating for each movie. 6e Solutions 5-26 .Programming Logic and Design. Programming Logic and Design.” .” output “The program will now end. 6e Solutions 5-27 Pseudocode: start Declarations string movieTitle num numOfStars num count = 0 num total = 0 num attempts num avg num LIMIT = 3 string STAR_PROMPT = “Enter a star rating or a negative number for a new movie” string MOVIE_PROMPT = “Enter a movie title or ZZZZZ to quit” housekeeping() while movieTitle not equal to “ZZZZZ” AND attempts < LIMITS detailLoop() endwhile finishUp() stop housekeeping() output MOVIE_PROMPT input movieTitle attempts = 0 return detailLoop() output STAR_PROMPT input numOfStars while numOfStars >= 0 AND attempts < LIMIT getStarRating() endwhile calcAverage() if attempts < LIMIT then output MOVIE_PROMPT input movieTitle attempts = 0 endif return getStarRating() while (numOfStars > 4 OR numOfStars < 0) AND attempts < LIMIT output “Please enter a value from 0 to 4” input numOfStars attempts = attempts + 1 endwhile if attempts < LIMIT then count = count + 1 total = total + numOfStars output PROMPT input numOfStars else output “You have failed to enter a valid rating. an error message is displayed and the order is not counted. If the clerk enters a high value on the third attempt. The clerk enters that data as well as the number of items the customer orders. avg return finishUp() output “End of program” return 12.Programming Logic and Design. movieTitle. Also display the average customer age. The Café Noir Coffee Shop wants some market research on its customers. the program reprompts the clerk continuously. When the clerk enters an invalid zip code (more than 5 digits) or an invalid age (defined as less than 10 or more than 110). the program accepts the high value. At the end of the program. The program operates continuously until the clerk enters a 0 for zip code at the end of the day. display a count of the number of items ordered by customers from the same zip code as the coffee shop (54984). the program reprompts the clerk two more times. but if the clerk enters a negative value on the third attempt. and a count from other zip codes. When the clerk enters fewer than 1 or more than 12 items. “ is: ”. as well as counts of the number of items ordered by customers under 30 and by customers 30 and older. When a customer places an order. 6e Solutions 5-28 endif return calcAverage() avg = total / count output “The average star rating for ”. a clerk asks for the customer’s zip code and age. Answer: A sample solution follows Pseudocode: start Declarations num custZip num custAge num numItems = 0 num validNumItems num countOfOrders = 0 num totalCustAge = 0 num numItemsUnder30 = 0 num numItems30AndOlder = 0 num numItemsInStoreZip = 0 num numItemsInOtherZips = 0 num STORE_ZIP = 54984 string ZIP_PROMPT = “Enter the zip code” string AGE_PROMPT = “Enter the customer’s age” string ITEM_PROMPT = “Enter the number of items” housekeeping() while custZip not equal to 0 detailLoop() endwhile . Programming Logic and Design. 6e Solutions 5-29 finishUp() stop validateZipCode() while custZip > 99999 output “Please enter a valid zip code” input custZip endwhile return validateCustAge() while custAge < 10 OR custAge > 110 output “Please enter a valid customer age” input custAge endwhile return validateNumItems() Declarations num attempts = 0 num LIMIT = 2 validNumItems = 1 while (numItems < 1 OR numItems > 12) AND attempts < LIMIT output “Please reenter the number of items” input numItems attempts = attempts + 1 endwhile if attempts = 2 AND numItems < 0 then output “An invalid number of items was entered” output “This order will not be counted” validNumItems = 0 endif return housekeeping() output ZIP_PROMPT input custZip return detailLoop() output AGE_PROMPT input custAge output ITEM_PROMPT input numItems validateZipCode() validateCustAge() validateNumItems() countOfOrders = countOfOrders + 1 totalCustAge = totalCustAge + custAge if custZip = STORE_ZIP numItemsInStoreZip = numItemsInStoreZip + numItems else numItemsInOtherZips = numItemsInOtherZips + numItems endif . “ zip code = ”. In Chapter 2. 6e Solutions 5-30 if custAge < 30 then numItemsUnder30 = numItemsUnder30 + numItems else numItems30AndOlder = numItems30AndOlder + numItems endif output ZIP_PROMPT input custZip return finishUp() Declarations avgCustAge if countOfOrders > 0 then output “Total items ordered in the ”. and DEBUG05-03. “ items were ordered by customers under 30” output numItems30AndOlder. numItemsInStoreZip output “Total items ordered in other zip codes = ”. Following the comments. DEBUG05-02. Each file starts with some comments that describe the problem. Answer: Please see the DEBUG05-01. you learned that in many programming languages you can generate a random number between 1 and a limiting value named LIMIT by using a statement similar to randomNumber = random(LIMIT).txt solution files. and DEBUG05-03. Game Zone 14. Comments are lines that begin with two slashes (//). Your student disk contains files named DEBUG05-01. DEBUG05-02. each file contains pseudocode that has one or more bugs you must find and correct. “ items were ordered by customers 30 and older” endif output “End of program” return Flowchart: The flowchart’s structure will be very similar to that given for previous exercises. numItemsInOtherZips avgCustAge = totalCustAge / countOfOrders output “The average customer age is: ”. Find the Bugs 13.txt. avgCustAge output numItemsUnder30. In Chapter 4.txt.Programming Logic and Design.txt. STORE_ZIP.txt. you created the logic for a guessing game in which the application generates a random number and the .txt. or too low. display a count of the number of guesses that were required. When the player eventually guesses the correct number. After each guess. too high. 6e Solutions 5-31 player tries to guess it. create the guessing game itself. display a message indicating whether the player’s guess was correct. Now. Answer: A sample solution is as follows: Flowchart: Pseudocode: start Declarations num myRandomNumber num guess num count num LIMIT string PROMPT = “Enter a number between 1 and “ string CORRECT = “You guessed correctly!” string HIGH = “You guessed too high!” .Programming Logic and Design. the user wins and the game ends. LIMIT input guess count = 1 return detailLoop() if guess > myRandomNumber then output HIGH else output LOW endif input guess count = count + 1 return finishUp() output CORRECT output “It took ”. The player then “rolls” two dice up to three times. If the number does not come up within three rolls. count. Create the logic for a game that simulates rolling two dice by generating two numbers between 1 and 6 inclusive. 6e Solutions 5-32 string LOW = “You guessed too low!” housekeeping() while guess not equal to myRandomNumber detailLoop() endwhile finishUp() stop housekeeping() myRandomNumber = random(LIMIT) output PROMPT. If the number chosen by the user comes up. “ guesses!” output “End of program” return 15.Programming Logic and Design. the computer wins. Answer: A sample solution is as follows: Flowchart: . The player chooses a number between 2 and 12 (the lowest and highest totals possible for two dice). 6e Pseudocode: start Declarations num dice1 num dice2 Solutions 5-33 .Programming Logic and Design. each player rolls two dice. If a 1 appears on one of the dice. If no 1 appears. in which a player can compete with the computer. in other words. and the player can choose whether to roll again or pass the turn to the other player. 6e Solutions 5-34 num guess num count num won num LIMIT = 6 string PROMPT = “Choose a number between 1 and 12“ string WIN = “You win!” string LOSE = “You lose!” housekeeping() while count < 3 AND won = 0 detailLoop() endwhile finishUp() stop housekeeping() count = 0 won = 0 dice1 = random(LIMIT) dice2 = random(LIMIT) output PROMPT input guess return detailLoop() count = count + 1 if guess = dice1 + dice2 then output WIN won = 1 endif dice1 = random(LIMIT) dice2 = random(LIMIT) return finishUp() if count = 3 AND won = 0 then output LOSE endif output “End of program” return 16. and it becomes the other player’s turn. Create the logic for the dice game Pig. If a 1 appears on both of the dice. but the player’s entire accumulated total is reset to 0.Programming Logic and Design. the dice values are added to a running total for the turn. The object of the game is to be the first to score 100 points. not only is the player’s turn over. the player’s turn total becomes 0. The user and computer take turns rolling a pair of dice following these rules:    On a turn. nothing more is added to the player’s game total for that turn. . 6e  Solutions 5-35 When the computer does not roll a 1 and can choose whether to roll again. Answer: A sample solution is as follows: Pseudocode: start Declarations num dice1 num dice2 num userCount num computerCount num userTurn num LIMIT = 2 num WIN_NUM =100 housekeeping() while computerCount < WIN_NUM AND userCount < WIN_NUM detailLoop() endwhile stop housekeeping() output ”The first to 100 wins the game of Pig” userCount = 0 computerCount = 0 userTurn = 1 return detailLoop() if userTurn = 1 then output “User’s turn to roll the dice” else output “Computer’s turn to roll the dice” endif dice1 = random(LIMIT) dice2 = random(LIMIT) if userTurn = 1 then if dice1 = 1 OR dice2 = 1 then output “Computer's turn” userTurn = 0 if dice1 = 1 AND dice2 = 1 then userCount = 0 endif else userCount = userCount + dice1 + dice2 if userCount >= WIN_NUM output “User wins!” else output “Enter a 1 to play again or 0 to pass” .Programming Logic and Design. generate a random value of 1 or 2. The computer will then decide to continue when the value is 1 and decide to quit and pass the turn to the player when the value is not 1. Are there any circumstances in which you should try to guess another employee’s ID number? . A loop would be useful to guess every combination of seven digits in an ID.Programming Logic and Design. helping you to locate the source of the error. You could also add decisions that display a message or variable values at key points in the process. 6e endif else Solutions 5-36 input userTurn endif if dice1 = 1 OR dice2 = 1 output “User's turn” userTurn = 1 if dice1 = 1 AND dice2 = 1 THEN computerCount = 0 endif else computerCount = computerCount + dice1 + dice2 if computerCount >= WIN_NUM output “Computer wins!” else if random(2) = 1 then userTurn = 0 else userTurn = 1 endif endif endif endif return finishUp() output “End of program” return Flowchart: The flowchart’s structure will be very similar to that given for previous exercises. 18. What would you add to your program to help you discover the origin of the problem? Answer: Adding output statements at key locations would show you which variables were changing and which were not. Suppose you wrote a program that you suspect is in an infinite loop because it just keeps running for several minutes with no output and without ending. Suppose you know that every employee in your organization has a seven-digit ID number used for logging on to the computer system. Up for Discussion 17. Typical reasons a students might try to guess another worker’s ID might be to investigate if they suspect the other employee or one of the other employee’s customers is doing harm to the company or to another person or engaging in illegal activity. . Another is to add a check digit. 6e Solutions 5-37 Answer: Students' answers should vary. Perhaps the student would investigate another worker if he or she wants to start a personal relationship with the worker. 19. guessing all the possible combinations would be a relatively easy programming task. If every employee in an organization had a seven-digit ID number. and the remainder can be used as the seventh digit. How could you alter the construction of employee IDs to make guessing them more difficult? Answer: One technique that helps is to add letters or other punctuation marks to ID numbers. such as 7. For example. the number can be divided by a prime number. after a six-digit ID number is assigned. Other students will assert that no one should attempt to guess another employee’s number under any circumstances.Programming Logic and Design.
Copyright © 2024 DOKUMEN.SITE Inc.