The Student Room Group

computer science help urgernt

how do you work out the arr[mid] in this trace table? question is below.



Capture.PNG
Firstly, you have an array, called arr which contains the following items:

3, 5, 13, 43, 655, 872

The question states that the array indexing starts at 1 (i.e. the "index" or "position number" for the first element is index 1).

So:
- arr[1] is 3
- arr[2] is 5
- arr[3] is 13
etc.

If you can calculate 'mid' then 'arr[mid]' is the value in the array which corresponds to index (position) mid.

the calculation for 'mid' is pretty clear -- (left + right) DIV 2
(remember that DIV means integer division so no fractions or remainders, e.g. 1 DIV 2 is zero, 9 DIV 2 is 4, etc. )

Just fill in the trace table one row at a time (each row is a 'cycle' of the while loop) - start with the easy values of 'left' and 'right', afterwards, calculate 'mid', and then you'll be able to get arr[mid].
(edited 5 years ago)

Quick Reply

Latest

Trending

Trending