fork download
  1. #include <stdio.h>
  2.  
  3. int rLookupAr(int *ar, int n, int target);
  4.  
  5. int main()
  6. {
  7. int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  8.  
  9. printf("rLookupAr() with 0: %d\n", rLookupAr(a, 10, 0));
  10. printf("rLookupAr() with 3: %d\n", rLookupAr(a, 10, 3));
  11. printf("rLookupAr() with 9: %d\n", rLookupAr(a, 10, 9));
  12. printf("rLookupAr() with 20: %d\n", rLookupAr(a, 10, 20));
  13. printf("rLookupAr() with -1: %d\n", rLookupAr(a, 10, -1));
  14.  
  15. return 0;
  16. }
  17.  
  18. int rLookupAr(int *ar, int n, int target)
  19. {
  20. if (n == 0) return -1;
  21. if (ar[0] == target) return 0;
  22.  
  23. int _ret = rLookupAr(ar + 1, n - 1, target);
  24.  
  25. if(_ret != -1) return ++_ret;
  26. else return -1;
  27. }
  28.  
Success #stdin #stdout 0s 2160KB
stdin
Standard input is empty
stdout
rLookupAr() with 0: 0
rLookupAr() with 3: 3
rLookupAr() with 9: 9
rLookupAr() with 20: -1
rLookupAr() with -1: -1