Linear search using arrays

Program Description

Linear search in C using arrays. The program shows an implementation of linear search algorithm in C language on integer array.
The program initially takes n number of integer elements from the user and stores them in an array. It then asks the user for an element to search, and then performs linear search on the input data given by the user.
Algorithm: Linear search is one of the simplest search algrithms.In linear search, we look at each element one after the other in the array untill the key to be searched is found or we reach the end of the array.
If the key to be searched is found before reaching the end or at the end, we immediately return with success.However if we reach the end of the array and still couldn't locate the key we return with failure.

Program
/**********************************
*
*  Linear search using arrays
*
*    code.cheraus.com
*
***********************************/

#include<stdio.h>

//function declaration for linear search
int search(int a[],int n,int key);

int main()
{
  int i;         //loop variable
  int n;         //number of terms
  int key;       //search key
  
  
  printf("\nEnter the number of terms : ");
  scanf("%d",&n);
  
  //taking array of apprpriate size
  int a[n];
  
  //taking user inputs
  printf("\nEnter the elements\n");
  for(i=0;i<n;i++)
   {
     printf("Enter element %d: ",i+1);
     scanf("%d",&a[i]);
   }
   
   //displaying the current array
   printf("\nThe numbers in the list are : ");
   for(i=0;i<n;i++)
   {
    printf("%d,",a[i]);
   }
   
   
   //prompting for search key
   printf("\nEnter the value to search : ");
   scanf("%d",&key);
   
   //calling the linear search function with search key
   int status=search(a,n,key);
   
   
   
   if(status>=0)
    {
	   printf("Element found at position %d in the array\n",status);
	 }
   else
     {
        printf("Element not found\n");
     }
   
   return 0;
   
}


/*
Function definition for search() function

Function: search()
    This function performs linear search on the given integer array for the given key and returns the
position of the key if the element is present in the array otherwise it returns -1.

Inputs: 1) Integer array
        2) size of the array
		3) key to be searched in the array

Returns: +ve integer representing the position of the key in the array if the element is found in the
          array
		  
		 OR
		  
		 -1 : if the key is not found in the array

*/
int search(int a[],int n,int key)
{
     
   for(int i=0;i<n;i++)
    {
	  //if key is found in array
      if(a[i]==key)
       {
		//returning the position of the key in the array
        return i;       
       }
    }
    
	//if key not found till the end of the array; element not present in array
	return -1;
	
}

Output
Enter the number of terms : 7

Enter the elements
Enter element 1: 1
Enter element 2: 2
Enter element 3: 3
Enter element 4: 4
Enter element 5: 5
Enter element 6: 6
Enter element 7: 7

The numbers in the list are : 1,2,3,4,5,6,7,
Enter the value to search : 4
Element found at position 3 in the array

Enter the number of terms : 7

Enter the elements
Enter element 1: 1
Enter element 2: 2
Enter element 3: 4
Enter element 4: 7
Enter element 5: 8
Enter element 6: 9
Enter element 7: 0

The numbers in the list are : 1,2,4,7,8,9,0,
Enter the value to search : 6
Element not found

Comments
comments powered by Disqus