You are on page 1of 3

Program To Perform A Linear Search In

A 1D Array
SOURCE
#include<stdio.h>
#include<iostream.h>
#include<conio.h>

//Program12.5 - To Perform a Linear Search in a 1D Array

void main( )
{
clrscr( );

// TOPIC

cout<<"------------------------------------------------"<<endl;
cout<<"Program to Perform a Linear Search in 1D
Array"<<endl;
cout<<"------------------------------------------------"<<endl;

// DECLARATION OF VARIABLES

int A[20],size,i,flag=0,num,pos;

// INPUT

cout<<"Enter the number of elements you want in the


array "<<endl;
cin>>size;
cout<<"Enter the elements of the array [ In Ascending
Order] "<<endl;
for(i=0;i<size;i++)
{
cin>>A[i];
}
cout<<"Enter the element to be searched for = ";
cin>>num;

// LOGIC - SEARCH ALGORITHM

for(i=0;i<size;i++)
{
if(A[i]==num)
{
flag=1;
pos=i;
break;
}
} if(flag==0)

{
cout<<endl<<"Element not found";
} else
{
cout<<endl<<"Element found at Position =
"<<(pos+1);
}
getch( );
}OUTPUT

You might also like