78.2k views
4 votes
What is the error in the following pseudocode?

// This program displays the highest value in the array.

Declare Integer SIZE = 3

Declare Integer values[SIZE] = 1, 3, 4

Declare Integer index

Declare Integer highest



For index = 0 To SIZE − 1

If values[index] > highest Then

Set highest = values[index]

End If

End For

Display "The highest number is ", highest

User Anrajme
by
3.8k points

2 Answers

2 votes

Answer:

The error in the pseudocode is that the variable "highest" is not initialized to any value before the for loop starts. This means that when the first comparison is made in the if statement, the value of "highest" will be undefined and could potentially cause unexpected behavior. To fix this error, the variable "highest" should be initialized to a value before the for loop, such as the first element in the array:

Declare Integer SIZE = 3

Declare Integer values[SIZE] = 1, 3, 4

Declare Integer index

Declare Integer highest = values[0] // Initialize highest to the first element in the array

For index = 1 To SIZE - 1 // Start loop from index 1, since highest is already initialized to index 0

If values[index] > highest Then

Set highest = values[index]

End If

End For

Display "The highest number is ", highest

Step-by-step explanation:

User Aalap
by
4.4k points
3 votes
The error is in line 4: Declare Integer highest
What is the error in the following pseudocode? // This program displays the highest-example-1
User Pasevin
by
4.5k points