0% found this document useful (0 votes)
74 views

Linear and Binary Search in Array

This C program performs a linear search on an integer array. It defines a function called lsearch that takes the array, size, and item as parameters. The function loops through the array and returns the index if the item is found or -1 if not found. The main function prompts the user to enter the array size and elements, the item to search for, and prints the result of calling lsearch.

Uploaded by

ravi_5227
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views

Linear and Binary Search in Array

This C program performs a linear search on an integer array. It defines a function called lsearch that takes the array, size, and item as parameters. The function loops through the array and returns the index if the item is found or -1 if not found. The main function prompts the user to enter the array size and elements, the item to search for, and prints the result of calling lsearch.

Uploaded by

ravi_5227
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

LINEAR SEARCH IN ARRAY

#include<conio.h>
#include<stdio.h>

int lsearch(int ar[],int size,int item) //function to perform linear search


{
for(int i=0;i<size,i++)
{
if(ar[i]==item)
return I;
}
return -1; //the control will reach here only if the item is not found

void main()
{
in tar[50],item,n,index;
clrscr();
printf(“\n ENTER DESIRED ARRAY SIZE(MAX 50)-:”);
scanf(“%d”,&n);
printf(“\n ENTER ARRAY ELEMENTS-:\n”);
for(int i=0;i<n;i++)
{
scanf(“%d”,ar[i]);
}
printf(“\n ENTER ELEMENT TO BE SEARCHED FOR-:”);
scanf(“%d”,&item);
index=lsearch(ar,n,item);
if(index==-1)
printf(“\n SORRY!GIVEN ELEMENT COULD NOT BE FOUND.\n”);
else
printf(“\n ELEMENT FOUND AT INDEX :%d, POSITION :%d”,index,(index+1));

getch();
}

OUTPUT
ENTER DESIRED ARRAY SIZE(MAX 50)-:7
ENTER ARRAY ELEMENTS-:
1 5 2 8 6 10 7
ENTER ELEMENT TO BE SEARCHED FOR-:6
ELEMENT FOUNT AT INDEX :4, POSITION :5

You might also like