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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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:
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
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
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
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
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
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
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
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
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
Answer:
Game Zone
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
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
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
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
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
Here are some hints:
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
hihi
To export a reference to this article please select a referencing stye below.
Assignment Hippo (2022) . Retrive from https://assignmenthippo.com/sample-assignment/programming-logic-and-design-question
"." Assignment Hippo ,2022, https://assignmenthippo.com/sample-assignment/programming-logic-and-design-question
Assignment Hippo (2022) . Available from: https://assignmenthippo.com/sample-assignment/programming-logic-and-design-question
[Accessed 10/08/2022].
Assignment Hippo . ''(Assignment Hippo,2022) https://assignmenthippo.com/sample-assignment/programming-logic-and-design-question accessed 10/08/2022.
Want to order fresh copy of the Sample Template Answers? online or do you need the old solutions for Sample Template, contact our customer support or talk to us to get the answers of it.
Our motto is deliver assignment on Time. Our Expert writers deliver quality assignments to the students.
Get reliable and unique assignments by using our 100% plagiarism-free.
Get connected 24*7 with our Live Chat support executives to receive instant solutions for your assignment.
Get Help with all the subjects like: Programming, Accounting, Finance, Engineering, Law and Marketing.
Get premium service at a pocket-friendly rate at AssignmentHippo
I was struggling so hard to complete my marketing assignment on brand development when I decided to finally reach to the experts of this portal. They certainly deliver perfect consistency and the desired format. The content prepared by the experts of this platform was simply amazing. I definitely owe my grades to them.
Get instant assignment help