hackerearth4 code example

Example: hackerearth4

#include <stdio.h> 
#include <stdbool.h> 

bool match (const void *m, const void *n) 
{ 
  return ( *(int*)m == *(int*)n ); 
} 
int testhack(void *val, int size_array, int size_elem, void *x, 
           bool match (const void * , const void *)) 
{ 
    char *ptr = (char *)val; 
      int i; 
    for (i=0; i<size_array; i++) 
        if (match(ptr + i*size_elem, x)) 
           return i; 
    return -1; 
}  
int main() 
{ 
    int val[] = {7, 14, 7, 90, 70}; 
    int n = sizeof(val)/sizeof(val[0]); 
    int x = 7; 
    printf ("value is %d", testhack(val, n, 
                               sizeof(int), &x, match)); 
    return 0; 
}

Tags:

C Example