Advertisement
namkongkirat

Linear Searching

Aug 1st, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <conio.h>
  3.  
  4. int linearsearch(int a[], int key)
  5. {
  6.     int i, location = -1;
  7.     for (i = 0; i < 10 ; i++)
  8.     {
  9.         if (a[i] == key)
  10.         {
  11.             location = i + 1;
  12.             return location;
  13.         }
  14.     }
  15.     return location;
  16. }
  17.  
  18. int main()
  19. {
  20.     int key, loc;
  21.     int a[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  22.     printf("Enter the Key: ");
  23.     scanf("%d", &key);
  24.     loc = linearsearch (a, key);
  25.     if (loc == -1)
  26.     {
  27.         printf("Search Unsuccessful. Input a correct key.\n");
  28.     }
  29.     else
  30.     {
  31.         printf("The location of Key %d is %d", key, loc);
  32.     }
  33.     getch();
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement