Programming Basics (Pseudocode Tracing)

÷ is division; when the result is assigned to an integer variable (including a function argument), the fractional part is discarded. The following program finds in lo the index of the first value that is at least x in the integer array data, which is sorted in ascending order (indices start at 1). x is no greater than the last element of data. What should replace 【a】 in the program?

integer: lo ← 1, hi ← length(data), mid
while (lo < hi)
  mid ← (lo + hi) ÷ 2
  if (data[mid] < x)
    lo ← 【a】
  else
    hi ← mid
  endif
endwhile
  1. Amid
  2. Bmid − 1
  3. Cmid + 1
Show answer and explanation

Answer: C. mid + 1

If data[mid] < x, then data[mid] and every element before it are less than x, so the answer is at mid + 1 or later. Otherwise data[mid] ≧ x and the answer is at mid or earlier, so hi ← mid. With lo ← mid + 1 the range always shrinks, and the answer is where lo = hi. With lo ← mid, when lo and hi are adjacent mid = lo, and if data[mid] < x the range does not shrink and the loop never ends.

  • A. When lo and hi are adjacent and data[lo] < x, mid stays equal to lo, the range does not shrink and the loop never ends. For example, with data = {3, 8, 12, 17, 23, 31} and x = 31, lo = 5 and hi = 6 forever.
  • B. This can move lo back before the answer, so the range may not shrink. With data = {3, 8, 12, 17, 23, 31} and x = 4, lo becomes 0 and the loop never ends.
  • C. Correct. It removes mid, now known to be less than x, so the range always shrinks.
Practice this topic Back to topic

Related calculators

Books on Programming Basics (Pseudocode Tracing) Ad

As an Amazon Associate I earn from qualifying purchases.

More Programming Basics (Pseudocode Tracing) questions

  1. A stack is represented by the 5-element integer array st and the integer variable top. All elements of st start at 0 and top starts at 0. push(v) adds 1 to top and then does st[top] ← v; pop() returns the value of st[top] and then subtracts 1 from top (the elements of st are not changed). What are top and st after the following program runs?
  2. A queue uses the 4-element string array q as a ring. head and tail start at 1. enq(v) does q[tail] ← v and then tail ← tail mod 4 + 1; deq() returns the value of q[head] and then does head ← head mod 4 + 1. When the following program runs, in which element of q is "E" stored?
  3. The following program outputs a linked list, whose elements are linked by the array next, by following it from the element at index 1. Array indices start at 1. A next value of 0 marks the end of the list. What sequence of values is output?
  4. The following program inserts "F", in val[6], right after "C" (index 3) in the linked list A → C → E → D → B, linked by the array next, and then outputs the list from the element at index 1. Array indices start at 1. A next value of 0 marks the end of the list. What should replace 【a】 so that the output is A, C, F, E, D, B?
  5. The following for loop increases its control variable by 5. What is the value of c after it runs?