arrays - What is this trying to tell me? -
i reading book "how solve computer" rg dromey. stuck on sentence trying explain termination of loops. here goes problem:
suppose wish establish array of
n
elements in strictly ascending order (i.e.a[1] < a[2] < ... < a[n]
) . can use following instructions:a[n+1] := a[n]; := 1; while a[i] < a[i+1] := i+1
(now if n
number of elements, i
stand in case? stand values?)
if
n
assigned value 5 , data set 2, 3, 5, 11, 14, first assignment prior loop result in array configuration below:
(this confused.)
a[1] a[2] a[3] a[4] a[5] a[6] 2 3 5 11 14 14
the 2 14's guarantee test
a[i] < a[i+1]
false wheni = n
, loop terminate correctly wheni = n
if not before.
(this confusing.)
i index
:= 1; means equal 1
:= i+1 means add 1
n = 5
a[5] = 14
a[5+1] = a[6] = 14
14 < 14 false - loop terminates
Comments
Post a Comment