Find Bugs in the pseudocode

// A high school is holding a recycling competition
// This program allows a user to enter a student's
// year in school (1 through 4)
// and number of cans collected
// Data is entered continuously until the user wnats to quit
// 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 collected[SIZE] = 0, 0, 0, 0
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
collected[year] = collected[year] + cans
endwhile
output HEAD1
output HEAD2
while year <= SIZE
output year, collected[year - 1]
year = year + 1
endwhile
stop

DEBUG06-02.txt

// 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
num 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
while sub < NUM_TESTS
output "Enter score "
input score
total = total + score
endwhile
average = total / NUM_TESTS
sub = 0
while average < RANGES[NUM_TESTS]
sub = sub + 1
endwhile
letterGrade = GRADES[NUM_TESTS]
output name, letterGrade
output "Enter student name or ", QUIT, " to quit "
input name
endwhile
stop

DEBUG06-03.txt

// This program counts how many sales are made in each of 5 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 ", QUIT, " to quit "
input category
while category = QUIT
if category >= 1 AND category < SIZE then
sales[category + 1] = sales[category - 1] + 1
else
output "Invalid category"
endif
output "Enter category ", QUIT, " to quit "
input category
endif
output HEAD1
output HEAD2
category = 0
while category <= SIZE
output category + 1, sales[category]
category = category + 1
endwhile
stop

To debug, always keep in your mind the value of variables at any time as the program proceeds, and think of exceptional values a user can enter.

Your program must expect users to enter anything because of human errors.
For example, for the recycling program:

// A high school is holding a recycling competition
// This program allows a user to enter a student's
// year in school (1 through 4)
// and number of cans collected
// Data is entered continuously until the user wnats to quit
// 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 collected[SIZE] = 0, 0, 0, 0
string HEAD1 = "Can Recycling Report"
string HEAD2 = "Year Cans Collected"
output "Enter year of student or ", QUIT, " to quit "
input year what if user enters 6? -5?
while year <> QUIT
output "Enter number of cans collected "
input cans
collected[year] = collected[year] + cans
endwhile
output HEAD1
output HEAD2
what is the value of "year" at this point, (logic requires it to be 0)
while year <= SIZE
output year, collected[year - 1]
year = year + 1
endwhile
stop

DEBUG06-02.txt

// 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
num 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
while sub < NUM_TESTS
output "Enter score "
input score
total = total + score
endwhile
average = total / NUM_TESTS
sub = 0
while average < RANGES[NUM_TESTS]
sub = sub + 1
endwhile
letterGrade = GRADES[NUM_TESTS]
output name, letterGrade
output "Enter student name or ", QUIT, " to quit "
input name
endwhile
stop

DEBUG06-03.txt

// This program counts how many sales are made in each of 5 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 ", QUIT, " to quit "
input category
while category = QUIT
if category >= 1 AND category < SIZE then
sales[category + 1] = sales[category - 1] + 1
else
output "Invalid category"
endif
output "Enter category ", QUIT, " to quit "
input category
endif
output HEAD1
output HEAD2
category = 0
while category <= SIZE
output category + 1, sales[category]
category = category + 1
endwhile
stop

1. In the first pseudocode, there is a typo in the variable name "wnats" instead of "wants" in the comment.

2. In the first pseudocode, the array "collected" should be initialized with curly brackets instead of square brackets. So, it should be "num collected[SIZE] = {0, 0, 0, 0}".
3. In the first pseudocode, the variable "year" is not being updated within the while loop. So, it should be added within the while loop: "input year".
4. In the second pseudocode, the variable "total" is not being initialized to zero before calculating the average. So, it should be initialized within the while loop: "total = 0".
5. In the second pseudocode, the variable "sub" is not being updated within the while loop. So, it should be added within the while loop: "sub = sub + 1".
6. In the second pseudocode, the variable "letterGrade" is not declared before assigning a value to it. So, it should be declared before the while loop: "string letterGrade".
7. In the third pseudocode, the "endif" statement at the end of the while loop should be "endwhile" instead.
8. In the third pseudocode, the if condition in the while loop should use an equality operator (==) instead of an assignment operator (=): "while category == QUIT".

Bugs in the pseudocode:

1. In the first pseudocode, inside the while loop, the input for the "year" variable is missing. It should be added inside the while loop before the calculation of collected[year].

2. In the second pseudocode, the variable "total" is not initialized to 0 before the inner while loop, where scores are input and added to it. This will lead to incorrect results for the average calculation.

3. In the second pseudocode, the variable "sub" is not incremented inside the inner while loop, resulting in an infinite loop.

4. In the third pseudocode, the condition for the while loop is incorrect. It should be "while category != QUIT" instead of "while category = QUIT". This will prevent the loop from terminating when the user wants to quit.

5. In the third pseudocode, the if statement is not properly nested within the while loop. The closing 'endif' is missing and the closing 'endwhile' is misplaced. It should be moved to right before the 'output HEAD1' statement. Additionally, the 'elseif' keyword should be used instead of 'else' to handle the case of an invalid category.

These are the bugs found in the pseudocode.

In the first pseudocode:

1. The initialization of the `collected` array is incorrect. It should be `num collected[SIZE] = {0, 0, 0, 0}` instead of `num collected[SIZE] = 0, 0, 0, 0`.
2. The `while` loop condition in line 28 should be `while year < SIZE` instead of `while year <= SIZE`.
3. There is no user input statement for the `year` variable inside the `while` loop. It should be included before line 28.

In the second pseudocode:

1. The initialization of the `RANGES` array is incorrect. It should be `num RANGES[NUM_RANGES] = {90, 80, 70, 60, 0}` instead of `num RANGES[NUM_RANGES] = 90, 80, 70, 60, 0`.
2. The `while` loop condition in line 21 should be `while sub <= NUM_TESTS` instead of `while sub < NUM_TESTS`.
3. There is no update statement for the `sub` variable inside the `while` loop. It should be included before line 24.
4. The `letterGrade` variable is used without declaration and initialization. It should be declared and initialized before line 25.

In the third pseudocode:

1. The first `while` loop condition should be `while category != QUIT` instead of `while category = QUIT`.
2. The `endif` statement in line 16 should be `endwhile`.
3. The variable `category` should not be incremented in line 22. It should start from 1 and iterate up to `SIZE`.
4. The `output` statement in line 25 should be inside the `if` condition.
5. The `endif` statement in line 33 should be removed.

These are the bugs found in the pseudocodes and the correct code snippets to fix them.