Answer DC24

#include <stdio.h>
#include <stdlib.h>

/* Puzzle D24 -- linear search: look for the first target element in an array, 
|                starting at an index 
|
|  Return the first index at which x is found, or -1 if not found.
|
*/
int linearSearchMid( int size, int arr[], int target, int start )
{
  int j;
  
  for ( j=start; j < size; j++ )
  {
    if ( arr[j] == target ) return j;
  }
  
  return -1;
}


Comments: What happens if the starting index is less than zero? The above program will probably crash.



Back to Puzzle Home