Answer:
c. The procedure returns the number of times adjacent items are in ascending order.
Step-by-step explanation:
Lets dry run the procedure to explain the answer. This is the given procedure.
PROCEDURE mystery (data) {
count = 0
i = 1
REPEAT UNTIL (i = LENGTH(data)) {
IF (data[i] < data[i+1]) {
count = count + 1 }
i = i+1 }
RETURN (count) }
- In the procedure mystery, there is an array named data . Lets say we have [1,2,5,8] elements of this array. and there are two variables: count variable, and a variable i that will point to the array elements or we can say i represents the index values of the array data.
- Initially the values of the above variables in the given procedure are
count = 0 and i =1
- Now the procedure has a loop which starts by pointing at the first element of array and will move through the array until it reaches the end of the array.
- There is an IF condition which compares two adjacent items in the array.
- If the current item (on which i points) is less than its adjacent item (item one element ahead of current item) then the value of count variable is incremented by 1. This gives us an idea that the procedure is considering the ascending order because with each iteration the condition will be checked that if a data item is less than the item adjacent to it.
- This condition will be checked with each iteration and i will move through the array.
- When the condition (data[i] < data[i+1]) is false, then the value of the count is not incremented and i will be incremented and i moves to next array index. If it reaches the end of the array, the loop will end and the value of the count recorded throughout these iterations is returned as output.
lets see the changes in the values throughout the procedure for the given array data={1, 2, 5, 8}
- count=0 i=1. i is pointing to item 1 . Now the first iteration starts. i is not equal to maximum size (LENGTH) of array (as i=1 for now) so control is entered to the body of the loop. It has an if condition which checks if the item in the current array index i is less than its adjacent item i+1. This checks if 1<2. Yes its true so the count variable value is incremented by 1. It becomes count=1. This is the first time when adjacent items are in ascending order. Next i is incremented and it becomes i=2. Now this points at 2nd array item.
- Now in second iteration. count=1 i=2 i is still less than LENGTH so if condition is checked now, 2<5 its true so now value of count is incremented again and it becomes count=2 and i is now 3.
- 3rd iteration count=2 i=3 i is still less than LENGTH of array. If condition is also true as 5<8. So value of count is again incremented. It becomes count=3 and i becomes 4.
- Now with count=3 i=4, i has reached the end of the array so the loop ends here. Also there is no comparison left as each array item has been compared to its adjacent one. RETURN count will display the value of count in the output i.e 3 (count=3).
Hence this procedure returns number of times adjacent items are in ascending order.