Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Array first Occurence

#include  <stdio.h>
 
int rLookupAr(int *ar, int n, int target);
 
int main()
{
    int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
 
    printf("rLookupAr() with 0: %d
",  rLookupAr(a, 10, 0));
    printf("rLookupAr() with 3: %d
",  rLookupAr(a, 10, 3));
    printf("rLookupAr() with 9: %d
",  rLookupAr(a, 10, 9));
    printf("rLookupAr() with 20: %d
", rLookupAr(a, 10, 20));
    printf("rLookupAr() with -1: %d
", rLookupAr(a, 10, -1));
 
    return 0;
}
 
int rLookupAr(int *ar, int n, int target)
{
    if (n == 0) return -1;
    if (ar[0] == target) return 0;
 
    int _ret = rLookupAr(ar + 1, n - 1, target);
 
    if(_ret != -1) return ++_ret;
    else          return -1;
}
Source by ideone.com #
 
PREVIOUS NEXT
Tagged: #Array #Occurence
ADD COMMENT
Topic
Name
6+7 =