#include <stdio.h>
int Linear_search(int array[], int size, int v) //linear search to find the value in array
{
int i;
for (i=0; i<size; i++) //one by one seach every index for value
if (array[i] == v) //if array matches the value to be seached
return i; //return the index of that value
return -1; //if value not found in array
}
int main()
{
int array[]={10,3,6,12,3,6}; //initilizing array
int v,index; //v=value to be seached in array and index is the index of the value to be seached
int size=sizeof(array)/sizeof(array[0]); //calculating size of array
printf(“array size: %d”,size);
printf(“nEnter value to seach in array: “);
scanf(“%d”,&v); //asking user to enter the value to be seached in array
index=Linear_search(array, size, v); //calling function linear seach and saving the output of function in index variable
if(index==-1) //checking the value of index
{
//if index is -1 then the value is not found in array
printf(“nUnsucessful search!!!nValue: %d not found in array!!!”,v);
}
else //else the value is founs in array and print its index
printf(“nSearch is sucessful!!!nValue: %d found at index: %d”,v,index);
return 0;
}
output 1:
Enter value to seach in array: 12
Search is sucessful!!!
Value: 12 found at index: 3
output 2:
Enter value to seach in array: 56
Unsucessful search!!!
Value: 56 not found in array!!!