ROBERT, BORIS, BRAD, GEORGE, DAVID

N = 5 // the number of elements in the array
K = 0 // this is the first index in the array
loop while K < N – 1
  TEMP = NAMES[K]
  NAMES [K] = NAMES [N – K – 1]
  NAMES [N – K – 1] = TEMP
  if K>=2 then K = N – 1 else K = K + 1
end loop

Refer to the above pseudo code/ algorithm, answer these questions!

  1. How many loops has occurred until program ends? (2 marks)
  2. What exactly the purpose of the algorithm? (2 marks)
  3. If we get rid the statement K = K + 1, what will happen? (2 marks)
  4. Trace the algorithm, showing the contents of the array after each execution of the loop! (4 marks)

Answer:

1. 3 times
2. reverse sorting
3.  infinite loop
4. K=0, NAMES[0] = DAVID,  NAMES[4] = ROBERT
K=1, NAMES[1] = GEORGE, NAMES[3] = BORIS

Tags: