recursive routine [message #83828] |
Mon, 08 April 2013 02:21  |
Thibault Garel
Messages: 55 Registered: October 2009
|
Member |
|
|
Hi,
I am having a hard time to figure out how to write a recursive routine
for the case below.
I have 2 arrays of similar size that contain integers.
For the value 2 of A:
(a) I want to find the corresponding elements of B, B[i].
(b) Then, I search for the values of A that are equal to the B[i]
values found in step (a)
(c) repeat step (a) with new A values
and so on
Example:
A = [2, 7, 6, 2, 0]
B = [6, 3, 1, 4, 6]
(a) A[0] and A [3] have value 2 => the corresponding B elements are
B[0]=6 and B[3]=4
(b) the value 4 is not found in A
(b) the value 6 is found in A : A[2]=1
(c) value 1 does not exist in B
I could easily write the algorithm in this simple example (using the
where function), but cannot make it work with much larger arrays in
case of N "levels" , i.e. all the steps have to be repeated N times
because several elements match the condition at each step.
I guess that I should use a recursive routine... (?)
Any help much appreciated !
Thanks
|
|
|
|
|