Answers for "last index of a number using recursion"

0

last index of a number using recursion

int lastIndex(int input[], int size, int x)
{
    if (size == 0)
    {
      return -1;
    }

    int answer = lastIndex(input + 1, size - 1, x);

    if (answer != -1)
    {
      return answer + 1;
    }

    if (input[0] == x)
    {
      return 0;
    }
    else
    {
      return -1;
    }
}

int main()
{
  int input[] = {9, 8, 10, 8};
  int x = 8;
  int size = 4;
  cout << lastIndex(input, size, x);
  return 0;
}
Posted by: Guest on April-03-2021
0

last index of a number using recursion

int rLookupAr (int array[], int size, int target)
{
    if(size<=0)   return -1;

    if(array[size-1] == target)
        return size-1;
    else
        return rLookupAr (array, size-1, target); //recurse

}
Posted by: Guest on April-03-2021
0

last index of a number using recursion

int rLookupAr(int array[], int size, int target)
{
    if(size < 1) return -1;

    size--;
    if(array[size] == target) return size;

    return rLookupAr(array, size,target);
}
Posted by: Guest on April-03-2021

Code answers related to "last index of a number using recursion"

Browse Popular Code Answers by Language