Programming Logic and Design Question
Programming Logic and Design, 9th Edition
Chapter 6
Review Questions
THE REVIEW QUESTIONS HAVE BEEN OMITTED FROM THE AVAILABLE RESOURCES FOR STUDENTS BECAUSE SOME OF THE QUESTIONS MAY BE USED ON THE QUIZ.
Programming Exercises
- Design the logic for a program that allows a user to enter 20 numbers, then displays them in the reverse order of entry.
Answer:
A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
num index
num SIZE = 20
num numbers[SIZE] = 0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0
getReady()
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
- Modify the reverse-display program so that the user can enter any amount of numbers up to 20 until a sentinel value is entered.
Answer:
A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
num index
num SIZE = 20
num numbers[SIZE] = 0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0
string CONTINUE = “Y”
string moreNumbers
getReady()
while index < SIZE AND moreNumbers = 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
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
- Design the logic for a program that allows a user to enter 20 numbers, then displays each number and its difference from the numeric average of the numbers entered.
Answer:
A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
num index
num sum
num avg
num SIZE = 20
num numbers[SIZE] = 0,0,0,0,0,0,0,0,0,0,
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
- Modify the program in Exercise 2a so that the user can enter any amount of numbers up to 20 until a sentinel value is entered.
Answer:
A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
num index
num sum
num avg
num actualSize
num SIZE = 20
num numbers[SIZE] = 0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0
string CONTINUE = “Y”
string moreNumbers
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 ”, index
input numbers[index]
sum = sum + numbers[index]
index = index + 1
output “Do you want to enter more numbers? (Y/N)”
input moreNumbers
return
finishUp()
actualSize = index
if actualSize > 0 then
avg = sum/actualSize
index = 0
while index < actualSize
output numbers[index], avg – numbers[index]
index = index + 1
endwhile
endif
return
- Design the logic for a program that allows a user to enter 20 numbers, then displays all of the numbers, the largest number, and the smallest.
Answer:
A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
num index
num largest
num smallest
num SIZE = 20
num numbers[SIZE] = 0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0
getReady()
while index < SIZE
getNumbers()
endwhile
finishUp()
stop
getReady()
index = 0
largest = 0
smallest = 0
return
getNumbers()
output “Enter a number for position ”, index
input numbers[index]
if numbers[index] > largest then
largest = numbers[index]
else
if numbers[index] < smallest then
smallest = numbers[index]
endif
endif
index = index + 1
return
finishUp()
index = 0
while index < SIZE
output numbers[index]
index = index + 1
endwhile
output largest, smallest
return
- Modify the program in Exercise 3a so that the user can enter any amount of numbers up to 20 until a sentinel value is entered.
Answer:
A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
num index
num largest
num smallest
num actualSize
num SIZE = 20
num numbers[SIZE] = 0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0
string CONTINUE = “Y”
string moreNumbers
getReady()
while index < SIZE AND moreNumbers = CONTINUE
getNumbers()
endwhile
finishUp()
stop
getReady()
index = 0
largest = 0
smallest = 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]
if numbers[index] > largest then
largest = numbers[index]
else
if numbers[index] < smallest then
smallest = numbers[index]
endif
endif
index = index + 1
output “Do you want to enter more numbers? (Y/N)”
input moreNumbers
return
finishUp()
actualSize = index
if actualSize > 0 then
index = 0
while index < actualSize
output numbers[index]
index = index + 1
endwhile
output largest, smallest
endif
return
- Trainers at Tom’s Athletic Club are encouraged to enroll new members. Write an application that allows Tom to enter the names of each of his 25 trainers and the number of new members the trainer has enrolled this year. Output is the number of trainers who have enrolled 0 to 5 members, 6 to 12 members, 13 to 20 members, and more than 20 members.
Answer:
A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
num numMembers
num numTrainers
num index
num x
num MAX_TRAINERS = 25
num COUNT_SIZE = 4
num memberCnts[COUNT_SIZE] = 0
num MEMBER_RANGES[COUNT_SIZE] = 0, 6, 13, 20
string name
string MEMBER_GROUPS[COUNT_SIZE] = “0 to 5 members”,
“6 to 12 members”, “13 to 20 members”,
“more than 20 members”
getReady()
while numTrainers < MAX_TRAINERS
countMembers()
endwhile
finishUp()
stop
getReady()
numTrainers = 0
return
countMembers()
output “Enter the trainer’s name ”
input name
output “Enter the number of new members this trainer enrolled ”
input numMembers
x = COUNT_SIZE – 1
while numMembers < MEMBER_RANGES[x]
x = x – 1
endwhile
memberCnts[x] = memberCnts[x] + 1
index = index + 1
return
finishUp()
x = 0
while x < COUNT_SIZE
output MEMBER_GROUPS[x], memberCnts[x]
x = x + 1
endwhile
return
- The Downdog Yoga Studio offers five types of classes, as shown in Table 6-1. Design a program that accepts a number representing a class and then displays the name of the class.
Class Number |
Class Name |
1 |
Yoga 1 |
2 |
Yoga 2 |
3 |
Children’s Yoga |
4 |
Prenatal Yoga |
5 |
Senior Yoga |
Table 6-1 Downdog Yoga Studio classes
Answer:
A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
num classNum
num SIZE = 5
string classes[SIZE] = “Yoga 1”, “Yoga 2”,
“Children’s Yoga”, “Prenatal Yoga”,
“Senior Yoga”
getReady()
displayClassName()
finishUp()
stop
getReady()
output “Enter a class number >> ”
input classNum
return
displayClassName()
if classNum >= 1 AND classNum <= SIZE then
output classNum, classes[classNum-1]
else
output “Invalid class number”
endif
return
finishUp()
output “End of program”
return
- Modify the Downdog Yoga Studio program so that numeric class requests can be entered continuously until a sentinel value is entered. Then, display each class number, name, and a count of the number of requests for each class.
Answer:
A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
num classNum
num QUIT = -1
num SIZE = 5
num classCnt[SIZE] = 0
string classes[SIZE] = “Yoga 1”, “Yoga 2”,
“Children’s Yoga”, “Prenatal Yoga”,
“Senior Yoga”
getReady()
while classNum <> QUIT
countClasses()
endwhile
finishUp()
stop
getReady()
output “Enter a class number or ”, QUIT, “ to quit”
input classNum
return
countClasses()
if classNum >= 1 AND classNum <= SIZE then
classCnt[classNum-1] = classCnt[classNum-1] + 1
else
output “Invalid class number”
endif
output “Enter a class number or ”, QUIT, “ to quit”
input classNum
return
finishUp()
classNum = 0
while classNum < SIZE
output classNum+1, classes[classNum], classCnt[classNum]
classNum = classNum + 1
endwhile
return
- Search the web to discover the ten most common user-selected passwords, and store them in an array. Design a program that prompts a user for a password, and continue to prompt the user until the user has not chosen one of the common passwords.
Answer:
A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
num index
num SIZE = 10
string pw
string match
string PASSWORD[SIZE] = “password”, “123456”, “12345678”,
“1234”, “qwerty”, “12345”, “dragon”, “baseball”,
“football”, “letmein”
getReady()
matchPasswords()
finishUp()
stop
getReady()
match = “Y”
return
matchPasswords()
while match = “Y”
“Enter a password ”
input pw
index = 0
match = “N”
while index < SIZE AND match = “N”
if pw = PASSWORD[index] then
match = “Y”
else
index = index + 1
endif
endwhile
endwhile
return
finishUp()
output “End of program”
return
- The Jumpin’ Jive coffee shop charges $2.00 for a cup of coffee, and offers the add-ins shown in Table 6-2.
Product |
Price ($) |
Whipped cream |
0.89 |
Cinnamon |
0.25 |
Chocolate sauce |
0.59 |
Amaretto |
1.50 |
Irish whiskey |
1.75 |
Table 6-2 Add-in list for Jumpin’ Jive coffee shop
Design the logic for an application that allows a user to enter ordered add-ins continuously until a sentinel value is entered. After each item, display its price or the message Sorry, we do not carry that as output. After all items have been entered, display the total price for the order.
Answer:
A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
string itemOrdered
num x
num found = 0
num total = 2
num SIZE = 5
string QUIT = “ZZZZ”
num PRICES[SIZE] = 0.89, 0.25, 0.59, 1.50, 1.75
string PRODUCTS[SIZE] = “Whipped cream”, “Cinnamon”,
“Chocolate sauce”, “Amaretto”, “Irish whiskey”
string ERROR_MSG = “Sorry, we do not carry that”
getReady()
while itemOrdered <> QUIT
detailLoop()
endwhile
finishUp()
stop
getReady()
output “Enter an item or ”, QUIT, “ to complete your order”
input itemOrdered
return
detailLoop()
found = 0
x = 0
while x < SIZE
if itemOrdered = PRODUCTS[x] then
output PRICES[x]
x = SIZE
found = 1
total = total + PRICES[x]
else
x = x + 1
endif
endwhile
if found = 0 then
output ERROR_MSG
endif
output “Enter an item or ”, QUIT, “ to complete your order”
input itemOrdered
return
finishUp()
output “Your order total is: $”, total
return
- Design the application logic for a company that wants a report containing a breakdown of payroll by department. Input includes each employee’s department number, hourly salary, and number of hours worked. The output is a list of the seven departments in the company and the total gross payroll (rate times hours) for each department. The department names are shown in Table 6-3.
Department Number |
Department Name |
1 |
Personnel |
2 |
Marketing |
3 |
Manufacturing |
4 |
Computer Services |
5 |
Sales |
6 |
Accounting |
7 |
Shipping |
Table 6-3 Department numbers and names
Answer:
A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
num deptNum
num salary
num hrsWorked
num SIZE = 7
num grossTotals[SIZE] = 0
string DEPTS[SIZE] = “Personnel”, “Marketing”,
“Manufacturing”,
“Computer Services”, “Sales”,
“Accounting”, “Shipping”
getReady()
while not eof
detailLoop()
endwhile
finishUp()
stop
getReady()
output “Enter the department number, hourly salary, and number of
hours worked”
input deptNum, salary, hrsWorked
return
detailLoop()
if deptNum >= 1 AND deptNum <= SIZE then
grossTotals[deptNum - 1] = grossTotals [deptNum - 1] + (hrsWorked * salary)
else
output “Invalid department number”
endif
output “Enter the department number, hourly salary, and number of
hours worked”
input deptNum, salary, hrsWorked
return
finishUp()
deptNum = 0
while deptNum < SIZE
output deptNum + 1, DEPTS[deptNum], grossTotals [deptNum]
deptNum = deptNum + 1
endwhile
return
- Design a program that computes pay for employees. Allow a user to continuously input employees’ names until an appropriate sentinel value is entered. Also input each employee’s hourly wage and hours worked. Compute each employee’s gross pay (hours times rate), withholding tax percentage (based on Table 6-4), withholding tax amount, and net pay (gross pay minus withholding tax). Display all the results for each employee. After the last employee has been entered, display the sum of all the hours worked, the total gross payroll, the total withholding for all employees, and the total net payroll.
Weekly Gross Pay ($) |
Withholding Percent (%) |
0.00 – 300.00 |
10 |
300.01 – 550.00 |
13 |
550.01 – 800.00 |
16 |
800.01 – up.000 |
20 |
Table 6-4 Withholding percentage based on gross pay
Answer: A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
string empName
num salary
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_RATES[SIZE] = 0.10, 0.13, 0.16, 0.20
num WITH_RANGES[SIZE] = 0, 300.01, 550.01, 800.01
getReady()
while empName <> QUIT
detailLoop()
endwhile
finishUp()
stop
getReady()
output “Enter an employee name or ”, QUIT, “ to quit”
input empName
return
detailLoop()
output “Enter ”, empName, “’s hourly wage and hours worked”
input salary, hoursWorked
grossPay = hrsWorked * salary
totalHrsWorked = totalHrsWorked + hrsWorked
totalGrossPay = totalGrossPay + grossPay
x = SIZE – 1
while grossPay < WITH_RANGES[x]
x = x – 1
endwhile
withTax = grossPay * WITH_RATES[x]
totalWithTax = totalWithTax + withTax
netPay = grossPay – withTax
totalNetPay = totalNetPay + netPay
output empName, grossPay, WITH_RATES[x] * 100, withTax, netPay
output “Enter an employee name or ”, QUIT, “ to quit”
input empName
return
finishUp()
output totalHrsWorked, totalGrossPay, totalWithTax, totalNetPay
return
- Daily Life Magazine wants an analysis of the demographic characteristics of its readers. The Marketing department has collected reader survey records containing the age, gender, marital status, and annual income of readers. Design an application that accepts reader data and, when data entry is complete, produces a count of readers by age groups as follows: under 20, 20–29, 30–39, 40–49, and 50 and older.
Answer: A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
num age
num income
num x
string gender
string maritalStatus
num SIZE = 5
num ageCnts[SIZE] = 0
num AGE_RANGES[SIZE] = 0, 20, 30, 40, 50
string AGE_GROUPS[SIZE] = “under 20”, “20 through 29”,
“30 through 39”, “40 through 49”,
“50 and older”
getReady()
while not eof
detailLoop()
endwhile
finishUp()
stop
getReady()
output “Enter the age, gender, marital status, and income of a
reader”
input age, gender, maritalStatus, income
return
detailLoop()
x = SIZE – 1
while age < AGE_RANGES[x]
x = x – 1
endwhile
ageCnts[x] = ageCnts[x] + 1
output “Enter the age, gender, marital status, and income of a
reader”
input age, gender, maritalStatus, income
return
finishUp()
x = 0
while x < SIZE
output AGE_GROUPS[x], ageCnts[x]
x = x + 1
endwhile
return
- Modify the Daily Life Magazine program so that it produces a count of readers by gender within age group—that is, under-20 females, under-20 males, and so on.
Answer: A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
num age
num income
num x
string gender
string maritalStatus
num SIZE = 5
num maleCnts[SIZE] = 0
num femaleCnts[SIZE] = 0
num AGE_RANGES[SIZE] = 0, 20, 30, 40, 50
string AGE_GROUPS[SIZE] = “under 20”, “20 through 29”,
“30 through 39”, “40 through 49”,
“50 and older”
getReady()
while not eof
detailLoop()
endwhile
finishUp()
stop
getReady()
output “Enter the age, gender, marital status, and income of a
reader”
input age, gender, maritalStatus, income
return
detailLoop()
x = SIZE – 1
while age < AGE_RANGES[x]
x = x – 1
endwhile
if gender = “male” then
maleCnts[x] = maleCnts[x] + 1
else
femaleCnts[x] = femaleCnts[x] + 1
endif
output “Enter the age, gender, marital status, and income of a
reader”
input age, gender, maritalStatus, income
return
finishUp()
x = 0
while x < SIZE
output AGE_GROUPS[x], maleCnts[x], femaleCnts[x]
x = x + 1
endwhile
return
- Modify the Daily Life Magazine program so that is produces a count of readers by annual income groups as follows: under $30,000, $30,000–$49,999, 50,000–$69,999, and $70,000 and up.
Answer: A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
num age
num income
num x
string gender
string maritalStatus
num SIZE = 4
num incomeCnts[SIZE] = 0
num INCOME_RANGES[SIZE] = 0, 30000, 50000, 70000
string INCOME_GROUPS[SIZE] = “under $30,000”, “$30,000-$49,999”, “$50,000-$69,999”,
“$70,000 and up”
getReady()
while not eof
detailLoop()
endwhile
finishUp()
stop
getReady()
output “Enter the age, gender, marital status, and income of a
reader”
input age, gender, maritalStatus, income
return
detailLoop()
x = SIZE – 1
while income < INCOME_RANGES[x]
x = x – 1
endwhile
incomeCnts[x] = incomeCnts[x] + 1
output “Enter the age, gender, marital status, and income of a
reader”
input age, gender, maritalStatus, income
return
finishUp()
x = 0
while x < SIZE
output INCOME_GROUPS[x], incomeCnts[x]
x = x + 1
endwhile
return
- Glen Ross Vacation Property Sales employs seven salespeople, as shown in the Table 6-5.
ID Number |
Salesperson Name |
103 |
Darwin |
104 |
Kratz |
201 |
Shulstad |
319 |
Fortune |
367 |
Wickert |
388 |
Miller |
435 |
Vick |
Table 6-5 Glen Ross salespeople
When a salesperson makes a sale, a record is created including the date, time, and dollar amount of the sale. The time is expressed in hours and minutes, based on a 24-hour clock. The sale amount is expressed in whole dollars. Salespeople earn a commission that differs for each sale, based on the rate schedule in the Table 6-6.
Sale Amount ($) |
Commission Rate (%) |
0 – 50,999 |
4 |
51,000 - 125,999 |
5 |
126,000 - 200,999 |
6 |
201,000 and up |
7 |
Table 6-6 Glen Ross commission schedule
Design an application that produces each of the following:
A list of each salesperson number, name, total sales, and total commissions
Answer: A sample solution follows
Flowchart:
Pseudocode:
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 totalComms[PSIZE] = 0
num ID_NUMS[PSIZE] = 103, 104, 201, 319, 367, 388, 435
string NAMES[PSIZE] = “Darwin”, “Kratz”, “Shulstad”,
“Fortune”,“Wickert”, “Miller”, “Vick”
num SALE_AMTS[SSIZE] = 0, 51000, 126000, 201000
num COMM_RATES[SSIZE] = 0.04, 0.05, 0.06, 0.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_NUMS[person]
person = person + 1
endwhile
if person = PSIZE then
output “An invalid number has been entered,
please try again”
else
input date, hours, minutes, saleAmt
totalSales[person] = totalSales[person] + saleAmt
x = SSIZE – 1
while saleAmt < SALE_AMTS[x]
x = x – 1
endwhile
totalComms[person] = totalComms[person] +
(COMM_RATES[x] * saleAmt)
endif
output “Enter the salesperson ID number”
input idNum
return
finishUp()
x = 0
while x < PSIZE
output ID_NUMS[x], NAMES[x], totalSales[x], totalComms[x]
x = x + 1
endwhile
return
- A list of each month of the year as both a number and a word (for example, 01 January), and the total sales for the month for all salespeople
Answer: A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
num hours, minutes
num month, day, year
num saleAmt
num idNum
num person, x
num SSIZE = 4
num PSIZE = 7
num MSIZE = 12
num totalSales[MSIZE] = 0
num totalComms[PSIZE] = 0
num ID_NUMS[PSIZE] = 103, 104, 201, 319, 367, 388, 435
string NAMES[PSIZE] = “Darwin”, “Kratz”, “Shulstad”,
“Fortune”,“Wickert”, “Miller”, “Vick”
num SALE_AMTS[SSIZE] = 0, 51000, 126000, 201000
num COMM_RATES[SSIZE] = 0.04, 0.05, 0.06, 0.07
string MONTHS[MSIZE] = “January”, “February”, “March”
“April”, “May”, “June”, “July”, “August”,
“September”, “October”, “November”, “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_NUMS[person]
person = person + 1
endwhile
if person = PSIZE then
output “An invalid number has been entered,
please try again”
else
input month, day, year, hours, minutes, saleAmt
while month < 1 OR month > 12 OR day < 1 OR day > 31
output “Invalid date, please reenter”
input month, day, 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, MONTHS[x], totalSales[x]
x = x + 1
endwhile
return
- A list of total sales as well as total commissions earned by all salespeople for each of the following time frames, based on hour of the day: 00–05, 06–12, 13–18, and 19–23
Answer: A sample solution follows
Flowchart: The flowchart will be similar to those shown in parts a and b.
Pseudocode:
start
Declarations
num hours, minutes
num month, day, year
num saleAmt
num idNum
num time, x
num SSIZE = 4
num PSIZE = 7
num TSIZE = 4
num totalSales[TSIZE] = 0
num totalComms[TSIZE] = 0
num ID_NUMS[PSIZE] = 103, 104, 201, 319, 367, 388, 435
string NAMES[PSIZE] = “Darwin”, “Kratz”, “Shulstad”,
“Fortune”, “Wickert”, “Miller”, “Vick”
num SALE_AMTS[SSIZE] = 0, 51000, 126000, 201000
num COMM_RATES[SSIZE] = 0.04, 0.05, 0.06, 0.07
num TIME_RANGES[TSIZE] = 0, 6, 13, 19
string TIMES[TSIZE] = “00-05", “06-12”, “13-18”, “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_NUMS[person]
person = person + 1
endwhile
if person = PSIZE then
output “An invalid number has been entered,
please try again”
else
input month, day, year, hours, minutes, saleAmt
while month < 1 OR month > 12 OR day < 1 OR day > 31
output “Invalid date, please reenter”
input month, day, year
endwhile
time = TSIZE - 1
while hours < TIME_RANGES[time]
time = time – 1
endwhile
totalSales[time] = totalSales[time] + saleAmt
x = SSIZE – 1
while saleAmt < SALE_AMTS[x]
x = x – 1
endwhile
totalComms[time] = totalComms[time] +
(COMM_RATES[x]*saleAmt)
endif
output “Enter the salesperson ID number”
input idNum
return
finishUp()
x = 0
while x < TSIZE
output TIMES[x], totalSales[x], totalComms[x]
x = x + 1
endwhile
return
- Design an application in which the number of days for each month in the year is stored in an array. (For example, January has 31 days, February has 28, and so on. Assume that the year is not a leap year.) Display 12 sentences in the same format for each month; for example, the sentence displayed for January is Month 1 has 31 days.
Answer: A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
num month
num SIZE = 12
num DAYS[SIZE] = 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
displayDays()
finishUp()
stop
displayDays()
month = 0
while month < SIZE
output “Month ”, month+1, “ has ”, DAYS[month], “ days”
month = month + 1
endwhile
return
finishUp()
output “End of program”
return
- Modify the months and days program to contain a parallel array that stores month names. Display 12 sentences in the same format; for example, the first sentence is January has 31 days.
Answer: A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
num month
num SIZE = 12
num DAYS[SIZE] = 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
num MONTHS[SIZE] = “January”, “February”, “March”, “April”,
“May”, “June”, “July”, “August”, “September”,
“October”, “November”, “December”
displayDays()
finishUp()
stop
displayDays()
month = 0
while month < SIZE
output “Month ”, MONTHS[month], “ has ”,
DAYS[month], “ days”
month = month + 1
endwhile
return
finishUp()
output “End of program”
return
- Modify the months and days program to prompt the user for a month number and display the corresponding sentence, for example, January has 31 days.
Answer: A sample solution follows
Flowchart:
Pseudocode:
start
Declarations
num month
num SIZE = 12
num DAYS[SIZE] = 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
num MONTHS[SIZE] = “January”, “February”, “March”, “April”,
“May”, “June”, “July”, “August”, “September”,
“October”, “November”, “December”
displayDays()
finishUp()
stop
displayDays()
output “Enter a month number ”
input month
output “Month ”, MONTHS[month], “ has ”,
DAYS[month], “ days”
return
finishUp()
output “End of program”
return
- Prompt a user to enter a birth month and day, and continue to prompt until the day entered is in range for the month. Compute the day’s numeric position in the year. (For example, February 2 is day 33.) Then, using parallel arrays, find and display the traditional Zodiac sign for the date. For example, the sign for February 2 is Aquarius.
Answer: A sample solution follows
Flowchart: The flowchart will be similar to those shown in parts a through c.
Pseudocode:
start
Declarations
num day
num month
num numPosition = 0
num x
num DAY_SIZE = 12
num SIGN_SIZE = 13
num DAYS[DAY_SIZE] = 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31
string SIGNS[SIGN_SIZE] = “Capricorn”, “Aquarius“, “Pisces”,
“Aries”, “Taurus”, “Gemini”, “Cancer”, “Leo”,
“Virgo”, “Libra”, “Scorpio”, “Sagittarius”,
“Capricorn”
num SIGN_RANGES[SIGN_SIZE] = 0, 21, 51, 80, 111, 142, 173,
204, 234, 267, 297, 327, 357
getReady()
while not eof
detailLoop()
endwhile
finishUp()
stop
getReady()
output “Enter a month and a day >> ”
input month, day
return
detailLoop()
while day < 1 OR day > DAYS[month - 1]
output “Day out of range, please reenter >> ”
input day
endwhile
x = month – 2
while x >= 0
numPosition = numPosition + DAYS[x]
x = x – 1
endwhile
numPosition = numPosition + day
x = SIGN_SIZE – 1
while numPosition < SIGN_RANGES[x]
x = x – 1
endwhile
output “The Zodiac sign for ”, month, “/”, day, “ is ”, SIGNS[x]
output “Enter a month and a day >> ”
input month, day
return
finishUp()
output “End of program”
return
Performing Maintenance
- A file named MAINTENANCE06-01.txt is included with your downloadable student files. Assume that this program is a working program in your organization and that it needs modifications as described in the comments (lines that begin with two slashes) at the beginning of the file. Your job is to alter the program to meet the new specifications.
Answer:
// Sunrise Freight charges standard
// per-pound shipping prices to the five states they serve
// –- IL IN OH MI WI
// -- 0.60 0.55 0.70 0.65 0.67
// Modify this program to reduce its size
// by using arrays
start
Declarations
string state
num pounds
num SIZE = 5
string STATES[SIZE] = “IL”, “IN”, “OH”, “MI”, “WI”
num PRICES[SIZE] = 0.60, 0.55, 0.70, 0.65, 0.67
num sub
string foundIt
string BAD_STATE_MSG = "Sorry, we do not ship to ”
string FINISH = “XXX”
getReady()
while state <> FINISH
findPrice()
endwhile
finishUp()
stop
getReady()
output "Enter state or ", FINISH, " to quit"
input state
return
findPrice()
foundIt = "N"
sub = 0
while sub < SIZE
if state = STATES[sub] then
foundIt = "Y"
endif
sub = sub + 1
endwhile
if foundIt = "N" then
output BAD_STATE_MSG, state
else
price = PRICES[sub]
output “Enter pounds “
input pounds
output “Cost per pound to ship to ”, state, “ is ”, price
output “Total cost is ”, price * pounds
endif
output "Enter next state or ", FINISH, " to quit"
input state
return
finishUp()
output "End of job"
return
Find the Bugs
- Your downloadable files for Chapter 6 include DEBUG06-01.txt, DEBUG06-02.txt, and DEBUG06-03.txt. Each file starts with some comments that describe the problem. Comments are lines that begin with two slashes (//). Following the comments, each file contains pseudocode that has one or more bugs you must find and correct.
Answer:
Debug06-01
// A high school is holding a recycling competition,
// and this program allows a user to enter a student's
// year in school (1 through 4) and number of cans collected
// for recycling. Data is entered continuously until the user
// enters 9 for the year.
// After headings, output is four lines --
// one for each school year class.
start
Declarations
num year
num cans
num SIZE = 4
num QUIT = 9
num collectedArray[SIZE] = 0, 0, 0, 0
// need additional initialization value
string HEAD1 = "Can Recycling Report"
string HEAD2 = "Year Cans Collected"
output "Enter year of student or ", QUIT, " to quit "
input year
while year <> QUIT
output "Enter number of cans collected "
input cans
collectedArray[year - 1] = collectedArray[year - 1] + cans
// subscripts should be year - 1
output "Enter year of student or ", QUIT, " to quit "
input year
endwhile
output HEAD1
output HEAD2
year = 1
while year <= SIZE
// year should be compared <= SIZE
output year, collectedArray[year - 1]
// subscripts should be year - 1
year = year + 1
endwhile
stop
Debug06-02
// Program lets user input scores on four tests
// Average is computed and letter grade is determined
// Letter grades are based on 90 for an A, 80 for a B, and so on
start
string name
num score
num NUM_TESTS = 4
num NUM_RANGES = 5
num RANGES[NUM_RANGES] = 90, 80, 70, 60, 0
string QUIT = "ZZZZZ"
string GRADES[NUM_RANGES] = "A", "B", "C", "D", "F"
num total
num average
num sub
output "Enter student name or ", QUIT, " to quit "
input name
while name <> QUIT
sub = 0
total = 0
while sub < NUM_TESTS
output "Enter score "
input score
total = total + score
// score must be added to total
sub = sub + 1
// sub must be incremented
endwhile
average = total / NUM_TESTS
// average must be calculated
sub = 0
while average < RANGES[sub]
// RANGES requires a subscript
sub = sub + 1
endwhile
letterGrade = GRADES[sub]
output name, letterGrade
output "Enter student name or ", QUIT, " to quit "
input name
// next name must be input
endwhile
stop
Debug06-03
// This program counts how many sales are made
// in each of five categories of products
start
Declarations
num category
num SIZE = 5
num QUIT = 9
num sales[SIZE] = 0, 0, 0, 0, 0
string HEAD1 = "Sales"
string HEAD2 = "Category Number of Sales"
output "Enter category or ", QUIT, " to quit "
input category
while category <> QUIT
// category must be not equal to quit
if category >= 1 AND category <= SIZE then
// category can be equal to 1 or SIZE and still be valid
sales[category - 1] = sales[category - 1] + 1
else
output "Invalid category"
endif
output "Enter category ", QUIT, " to quit "
input category
// category must be input
endwhile
output HEAD1
output HEAD2
category = 0
// category should start at 0 if it is used as sales subscript
while category < SIZE
output category + 1, sales[category]
category = category + 1
endwhile
stop
- Your downloadable files for Chapter 6 include a file named DEBUG06-04.jpg that contains a flowchart with syntax and/or logical errors. Examine the flowchart, and then find and correct all the bugs.
Answer:
Game Zone
- 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, such as It remains to be seen.
start
Declarations
num LIMIT = 8
num index
string userQuestion
string QUIT = “zzzz”
string ANSWERS[LIMIT] =
“As I see it, yes”,
“It is certain”,
“Signs point to yes”,
“It remains to be seen”,
“Reply hazy, try again”,
“Outlook not so good”,
“Cannot predict now”,
“My sources say no”
getReady()
while userQuestion <> QUIT
detailLoop()
endwhile
finishUp()
stop
getReady()
output “Enter a question or ”, QUIT, “ to quit”
input userQuestion
return
detailLoop()
index = random(LIMIT)
output ANSWERS[index]
output “Enter a question or ”, QUIT, “ to quit”
input userQuestion
return
finishUp()
output “End of program”
return
- Create the logic for an application that contains an array of 10 multiple-choice questions related to your favorite hobby. Each question contains three answer choices. Also create a parallel array that holds the correct answer to each question—A, B, or C. Display each question and verify that the user enters only A, B, or C as the answer—if not, keep prompting the user until a valid response is entered. If the user responds to a question correctly, display Correct!; otherwise, display The correct answer is and the letter of the correct answer. After the user answers all the questions, display the number of correct and incorrect answers.
Answer:
A sample solution follows
Pseudocode (Please note, for brevity this solution contains only five questions. Students should provide 10 questions.):
start
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?”,
“What is the name of Earth’s moon?”,
“How many miles (in trillions) are in a light year?”,
“How many moons does Pluto have?”,
“What planet is the biggest?”
string CHOICES[SIZE] =
“A. Scopper, B. Dipper, C. Sun”,
“A. Luna, B. Europa, C. Sol”,
“A. 6, B. 93, C. 10”,
“A. 6, B. 2, C. 3”,
“A. Saturn, B. Jupiter, C. Earth”
string ANSWERS[SIZE] = “C”, “A”, “A”, “C”, “B”
getReady()
while x < SIZE
detailLoop()
endwhile
finishUp()
stop
getReady()
x = 0
output QUESTIONS[x], 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, please enter either A, B, or C”
input guesses[x]
endwhile
if guesses[x] = ANSWERS[x] then
output “Correct!”
correct = correct + 1
else
output “The correct answer is: “, ANSWERS[x]
incorrect = incorrect + 1
endif
x = x + 1
output QUESTIONS[x], CHOICES[x]
input guesses[x]
return
finishUp()
output “Number of correct = ”, correct
output “Number of incorrect = ”, incorrect
return
- Create the logic for a dice game. The application randomly “throws” five dice for the computer and five dice for the player. As each random throw, store it in an array. The application displays all the values, which can be from 1 to 6 inclusive for each die. Decide the winner based on the following hierarchy of die values. Any higher combination beats a lower one; for example, five of a kind beats four of a kind.
- Five of a kind
- Four of a kind
- Three of a kind
- A pair
For this game, the numeric dice values do not count. For example, if both players have three of a kind, it’s a tie, no matter what the values of the three dice are. Additionally, the game does not recognize a full house (three of a kind plus two of a kind). Figure 6-20 shows how the game might be played in a command-line environment.
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
// 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
accumulateNums()
// accumulate how many of each number was rolled
// by both the computer and the player, 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
finishUp()
output “Computer has ”, computerMatch, “ of a kind”
output “You have ”, playerMatch, “ of a kind”
if computerMatch > playerMatch then
output “Computer wins”
else
if playerMatch > computerMatch then
output “You win”
else
output “Tie”
endif
endif
return
- Improve the dice game so that when both players have the same combination of dice, the higher value wins. For example, two 6s beats two 5s.
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
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
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 ”, computerMatch, “ of a kind”
output “You have ”, playerMatch, “ of a kind”
if computerMatch > playerMatch then
output “Computer wins”
else
if playerMatch > computerMatch then
output “You win”
else
if computerLargest > playerLargest then
output “Computer wins”
else
if playerLargest > computerLargest then
output “You win”
else
output “Tie”
endif
endif
endif
endif
return
- Design the logic for the game Hangman, in which the user guesses letters in a hidden word. Store the letters of a word in an array of characters. Display a dash for each missing letter. Allow the user to continuously guess a letter until all the letters in the word are guessed correctly. As the user enters each guess, display the word again, filling in the guess if it was correct. For example, if the hidden word is computer, first display a series of eight dashes: --------. After the user guesses p, the display becomes ---p---. Make sure that when a user makes a correct guess, all the matching letters are filled in. For example, if the word is banana and the user guesses a, all three a characters should be filled in.
Answer:
A sample solution follows
Pseudocode:
start
Declarations
num x
num numCorrect
num LENGTH = 8
string WORD[LENGTH] = “C”, “O”, “M”, “P”, “U”,
“T”, “E”, “R”
string hiddenWord[LENGTH] = “-”,“-”,“-”,“-”,
“-”,“-”,“-”,“-”
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
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
- Create two parallel arrays that represent a standard deck of 52 playing cards. One array is numeric and holds the values 1 through 13 (representing Ace, 2 through 10, Jack, Queen, and King). The other array is a string array that holds suits (Clubs, Diamonds, Hearts, and Spades). Create the arrays so that all 52 cards are represented. Then, 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. The game should last for 26 rounds and use a full deck with no repeated cards. For this game, assume that the lowest card is the Ace. Display the values of the player’s and computer’s cards, compare their values, and determine the winner. When all the cards in the deck are exhausted, display a count of the number of times the player wins, the number of times the computer wins, and the number of ties.
Here are some hints:
- Start by creating an array of all 52 playing cards.
- 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.
- Move every higher-positioned card in the deck “down” one to fill in the gap. In other words, if the player’s first random number is 49, select the card at position 49 (both the numeric value and the string), move the card that was in position 50 to position 49, and move the card that was in position 51 to position 50. Only 51 cards remain in the deck after the player’s first card is dealt, so the available card array is smaller by one.
- In the same way, randomly select a card for the computer and “remove” the card from the deck.
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, 13, 26, 39
num cards[LENGTH] =
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52
string SUITS[GROUPS] = “Clubs”,“Diamonds”,“Hearts”,“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)
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: ”, playerCardNum, “ of ”,
playerCardSuit
output “Computer’s card: ”, computerCardNum, “ of ”,
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
Buy Programming Logic and Design Question Assessment Answers Online
Talk to our expert to get the help with Programming Logic and Design Question Answers from Assignment Hippo Experts to complete your assessment on time and boost your grades now
The main aim/motive of the finance assignment help services is to get connect with a greater number of students, and effectively help, and support them in getting completing their assignments the students also get find this a wonderful opportunity where they could effectively learn more about their topics, as the experts also have the best team members with them in which all the members effectively support each other to get complete their diploma assignment help Australia. They complete the assessments of the students in an appropriate manner and deliver them back to the students before the due date of the assignment so that the students could timely submit this, and can score higher marks. The experts of the assignment help services at www.assignmenthippo.com are so much skilled, capable, talented, and experienced in their field and use our best and free Citation Generator and cite your writing assignments, so, for this, they can effectively write the best economics assignment help services.
Get Online Support for Programming Logic and Design Assignment Help Online
Want to order fresh copy of the Sample Programming Logic and Design Question Answers? online or do you need the old solutions for Sample Programming Logic and Design Question, contact our customer support or talk to us to get the answers of it.
Assignment Help Australia