fork download
  1. #include <stdio.h>
  2. #define SIZE 100
  3.  
  4. int linearSearch(const int array[], int key, size_t size);
  5.  
  6. int main(void) {
  7. int a[SIZE] = {0};
  8.  
  9. for (size_t x = 0; x < SIZE; ++x) {
  10. a[x] = 2 * x;
  11. }
  12.  
  13. int searchKey = 36;
  14. int subscript = linearSearch(a, searchKey, SIZE);
  15.  
  16. if (subscript != -1) {
  17. printf("Found value at subscript %d\n", subscript);
  18. } else {
  19. printf("Value not found\n");
  20. }
  21.  
  22. return 0;
  23. }
  24.  
  25. int linearSearch(const int array[], int key, size_t size) {
  26. if (size == 0) {
  27. return -1;
  28. }
  29. if (array[size - 1] == key) {
  30. return size - 1;
  31. }
  32. return linearSearch(array, key, size - 1);
  33. }
Success #stdin #stdout 0s 5324KB
stdin
Standard input is empty
stdout
Found value at subscript 18