Shortcut for chapter specific information

Tuesday, May 10, 2011

Linear Search I wrote.

Seems to be a reasonable implementation for a simple program. 

#include <stdio.h>
#define N 5
int linear_search(int a[],int key)
{
  int i;
  for(i=0;i<N;i++)
    if(a[i]==key)
      return i;
  return -1;
}
int main(int c,char *v[])
{
  int A[N]={44,22,33,41,50};
  printf("33? %d\n",linear_search(A,33));
  printf("99? %d\n",linear_search(A,99));
  return 0;
}

No comments:

Post a Comment