Horje
last index of number using recursion Code Example
last index of number using recursion
// // Last index

#include <iostream>
using namespace std;

// Another Approach

/*
int LastIndex(int arr[], int size, int n)
{
    if (size == 0)
        return -1;
    if (arr[size - 1] == n)
        return size - 1;
    else
        return LastIndex(arr, size - 1, n);
}
*/

int LastIndex(int arr[], int size, int n)
{
    if (size == 0)
        return -1;

    int index = LastIndex(arr + 1, size - 1, n);

    if (index != -1)
        return index + 1;

    if (arr[0] == n)
        return 0;
    else
        return -1;
}

int main()
{
    int size, x;

    cin >> size;

    int arr[size];

    for (int i = 0; i < size; i++)
        cin >> arr[i];
    cout << endl;
    cout << "Enter Key : " << endl;

    cin >> x;

    cout << LastIndex(arr, size, x) << endl;

    return 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;
}
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

}




Whatever

Related
animals that use echolocation Code Example animals that use echolocation Code Example
alle overkapping bedrijven in nederland Code Example alle overkapping bedrijven in nederland Code Example
kikufuku Code Example kikufuku Code Example
Pure Capitalism Code Example Pure Capitalism Code Example
BDPSK Code Example BDPSK Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
6