Write a pseudocode

I am trying to write a pseudocode function R1(key, A, B, N) that takes a non-negative integer key, and arrays A and B of length N as inputs: the function should return an index where the value key is stored in the same location in both A and B; it should return -1 if the value cannot be found in either array; and it should return -2 if there is an index j where A[i] is not equal to B[i].

I am not sure this is correct. I have tried this :
1
2
3
4
5
6
7
8
9
10
Function R1(key, A, B, N):
    for 0<= i < N:
        if(B[i] or A[i] == key):
            return i
   // Item not found in the array
    return -1
    if(B[i] ≠ A[i]):
        return -2
    end for
end function




can someone help me with that ?

thanks
Last edited on
What you have is mostly right:
1
2
3
4
5
6
7
8
9
10
Function R1(key, A, B, N):
  int ret = -1

  for 0 <= i < N:
    if (B[i] and A[i] == key):
      return i
    if (A[i] or B[i] == key)
      ret = -2

  return ret
Last edited on
@kbw
what does mean ret? return ret? i dont understand it. I need a help. thank you very much.
Last edited on
In kbw's example he has an int named ret. In c++ when you want to return the value of an int, short for integer, you use the name you have given it.
Topic archived. No new replies allowed.