233k views
3 votes
The following procedure is intended to return true if the list of numbers myList contains only positive numbers and is intended to return false otherwise. The procedure does not work as intended. PROCEDURE allPositive(myList) { index ←← 1 len ←← LENGTH(myList) REPEAT len TIMES { IF(myList[index] > 0) { RETURN(true) } index ←← index + 1 } RETURN(false) } For which of the following contents of myList does the procedure NOT return the intended result?

1 Answer

4 votes

Answer:

[-1, 0, 1]

Step-by-step explanation:

Given

The above procedure

Required

Which list does not work as intended

The question has missing options (the options are available online).

From the question, we understand the procedure is to return true for lists that contain only positive numbers.

However, the list does not work for [-1, 0, 1]

The reason is that; the procedure starts checking from index 1.

i.e. index ←← 1

In [-1, 0, 1], the element at index 0 is negative (-1).

Since the procedure starts checking from index 1, the procedure will not check the element at index 0 (i.e. -1).

Hence, it will return true, instead of false

User Stundji
by
6.6k points