The following procedure is intended to return the number of times the value val appears in the list nylist. The procedure does not work as intended, Line 1: PROCEDURE countNumoccurences (mylist, val) Line 2: Line 3: FOR EACH item IN mylist Line 4: Line 5: count = 0 Line 6: IF(item = val) Line 7: tine 8: count count + 1 tine 9: Line 10: Eine 11: RETURN (count) Line 12:) Which of the following changes can be made so that the procedure will work as intended?
A. Changing line 6 to IF(item = count)
B. Changing line 6 to IF(myList[item) - val)
C. Moving the statement in line 5 so that it appears between lines 2 and 3
D. Moving the statement in line 11 so that it appears between lines 9 and 10 ->

Respuesta :

Answer:

C. Moving the statement in line 5 so that it appears between lines 2 and 3

Explanation:

Given

The attached procedure

Required

What should be modified

The problem in the procedure is on line 5

i.e. count = 0

This line is within the loop, and this means that the count variable is initialized to 0 each time the loop is repeated.

To solve this, the count variable has to be removed from the loop to somewhere before the loop.

Hence, option (c) is correct

Throughout our procedure, even though we proclaimed count = 0 inside the for loop, the count has been valued at 0.  

  • After that, When a certain value is found equal to Val, the count has been incremented by 1.
  • The count is set to 0 again in the next iteration, so at the end, if we found an element equivalent to Val 1, its real count has been returned.
  • To get around this, we'll define count outside of the for loop to acquire the true count of instances of that specific value.

Therefore, the answer is "Option C".

Learn more:

brainly.com/question/14941418