Puzzle DC26


Look thru an array for a place where value x is immediately followed by value y

[E-5] Write a function that looks for x and y in sequence among the elements of an integer array. Return the index of the first element x, or -1 if the sequence is not found. Here is the output of a testing program. The testing program is based on the previous one:

  -5   -3    0    6    4   16   -3    0    7    9
x y: 6 4
6, 4 found at index 3
x y: 4 16
4, 16 found at index 4
x y: 6 0
  sequence not found
x y: -3 0
-3, 0 found at index 1
x y: 16 -3
16, -3 found at index 5
x y: q

Modifying the testing program is harder than writing the actual search function.



Answer         Next Page         Previous Page Home