1. - Find The Bugs in the pseudocode below (you should find at least 5 bugs): //
ID: 673604 • Letter: 1
Question
1. - Find The Bugs in the pseudocode below (you should find at least 5 bugs):
// This program counts how many sales are made in each of 5 categories of products.
// The user continuously enters a category number (1-5) and then enters a 9 to quit.
// At the end the program prints a list of the category names and the number
// of sales that were made for that category.
// Categories are: 1=Infants; 2=Children; 3=Teens; 4=Men; 5=Women
start
Declarations
num category
num SIZE = 5
num QUIT = 9
num sales[category] = 0, 0, 0, 0, 0
housekeeping()
while category = QUIT
mainLoop()
endwhile
endOfJob()
stop
housekeeping()
output "Enter category or ", QUIT, " to quit "
input category
return
mainLoop()
if category >= 1 AND category <= SIZE then
sales[category - 1] = sales[category] + 1
else
output "Invalid category"
endif
output "Enter category or ", QUIT, " to quit "
input category
return
endOfJob()
Declarations
string HEAD1 = "Sales"
string HEAD2 = "Category Number of Sales"
string CATEGORYNAME[SIZE] = "Infants", "Children", "Teens", "Men", "Women"
output HEAD1
output HEAD2
while category < SIZE
output CATEGORYNAME[category], sales[category]
category = category + 1
endIf
return
Explanation / Answer
The following code does not have any errors.
start
Declarations
num category
num SIZE = 5
num QUIT = 9
num sales[category] = 0, 0, 0, 0, 0
housekeeping()
while category = QUIT
mainLoop()
endwhile
endOfJob()
stop
In this code also there are no errors were found.
housekeeping()
output "Enter category or ", QUIT, " to quit "
input category
return
In this, code, while assigning and incrementing the particular array value then the statement
sales[category-1] = sales[category] + 1
must be changed to
sales[category-1] = sales[category-1] + 1
Because, here the code is adding the present sales value +1 to previous sales value. So in-order to increment the actual value at the required position then it should add 1 to the required position value.
mainLoop()
if category >= 1 AND category <= SIZE then
sales[category - 1] = sales[category-1] + 1
else
output "Invalid category"
endif
output "Enter category or ", QUIT, " to quit "
input category
return
In this code also, there is an error. In the code at loop statement, the category value is checked against the SIZE. From the previous code, the category value has been changing all the time by the user input. So, while displaying the condition at time satisfy but does not produce the actual output.
In-order to get the required output, either make the category value to zero before the loop statement or declare a new variable initialized to zero and inside the loop statements increment the newly declare variable. The statements that are used to replace the code are:
1.
category = 0
or
2.
count =0
while count < SIZE
output CATEGORYNAME[category], sales[category]
count = count + 1
endIf
In the below code, the first statement is added as follows:
endOfJob()
Declarations
string HEAD1 = "Sales"
string HEAD2 = "Category Number of Sales"
string CATEGORYNAME[SIZE] = "Infants", "Children", "Teens", "Men", "Women"
output HEAD1
output HEAD2
category = 0
while category < SIZE
output CATEGORYNAME[category], sales[category]
category = category + 1
endIf
return
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.