85.5k views
2 votes
Declare Integer values[SIZE] = 1, 2, 3, 4, Debugging Exercises 1. What is the error in the following pseudocode? // This program uses an array to display five names Constant Integer SIZE = 5 Declare String names [SIZE] = "Meg", "Jack", "Steve "Bill", "Lisa" Declare Integer index For index = 0 To SIZE Display names[index] End For ​

1 Answer

2 votes

Final answer:

The error in the loop is that it runs one iteration too many, leading to an out-of-bounds error. The for loop should run from 0 to SIZE - 1 to avoid accessing an index that doesn't exist in the array.

Step-by-step explanation:

The error in the pseudocode provided lies within the loop structure used to display the names stored in the array. The for loop iterates from index 0 to SIZE (which is 5), but array indices typically start at 0 and end at one less than the size of the array. In this pseudocode, attempting to access names[SIZE] will result in an out-of-bounds error, as the valid indices for the array are 0 to 4 (inclusive).

To correct this error, the loop should iterate one less than the value of SIZE, as shown below:

For index = 0 To SIZE - 1
Display names[index]
End For

User Rfrittelli
by
4.7k points