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

Ex - No:02 Linear Search: Aim: Sample Input Amd Output: Output

This document describes a C++ program to perform a linear search on an array. The program takes in the size of the array and its elements, then takes in the element to search for. It calls a function Lsearch which iterates through the array and returns the index if the element is found or -1 if not found. It displays the index and position if found. The program works as expected and verifies the output.

Uploaded by

Gautham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Ex - No:02 Linear Search: Aim: Sample Input Amd Output: Output

This document describes a C++ program to perform a linear search on an array. The program takes in the size of the array and its elements, then takes in the element to search for. It calls a function Lsearch which iterates through the array and returns the index if the element is found or -1 if not found. It displays the index and position if found. The program works as expected and verifies the output.

Uploaded by

Gautham
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Ex.

No:02

LINEAR SEARCH
AIM: To write a C++ program to search a given elements using fuctions.

SAMPLE INPUT AMD OUTPUT:

OUTPUT:

#include<iostream.h>

#include<conio.h>

int Lsearch(int [],int,int);

void main()

int AR[20],ITEM,N,index;

clrscr();

cout<<"\nEnter Array size: ";

cin>>N;

cout<<"\NEnter Array Elements : ";

for(int i=0;i<N;i++)

cin>>AR[i];

cout<<"\nEnter Element to be searched : ";

cin>>ITEM;

index=Lsearch(AR,N,ITEM);

if(index==-1)

cout<<"\nSORRY!!! ELEMENT NOT FOUND";

}
else

cout<<"\nElement found at : (1)INDEX : "<<index<<"\n\t\t (2)POSITION : "<<index+1;

getch();

int Lsearch(int AR[],int SIZE,int ITEM)

for(int i=0;i<SIZE;i++)

if(AR[i]==ITEM)

return i;

return -1;

RESULT: Thus the given program is executed successfully and the output is verified.
OUTPUT:

Enter Array Size :

10

Enter Array Elements :

10

Enter Elements to be Searched : 6

ELEMENT FOUND AT : (1)INDEX : 5

(2)POSITION : 6

You might also like