Tuesday 24 April 2012

9th Experiment LINIER SEARCH


ALGORITH forLINEAR SEARCH USING ARRAY
---------------------------------------------------------------------------------------------------------------------------------------


Step-1: Start
Step-2: Accept no. of the values
Step-3: Accept the values and store it in an array
Step-4: Accept the no. to be searched as ‘num’
Step-5: If a value is found equal to ‘num’, output, ‘Element found’.
Step-6: If the value is not found, output, ‘Element not found’.
Step-7: Stop

--------------------------------------------------------------------------------------------------------------------------------------
                                                                          FLOWCHART
--------------------------------------------------------------------------------------------------------------------------------------

--------------------------------------------------------------------------------------------------------------------------------------
C++ IMPLEMENTATION
------------------------------------------------------------------------------------------------------------------------------------
//program for Linear search in Array
//*****************************************
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int arr[50],temp[50],i,j,item,no,t;
char ch;
cout<<"\n ENTER HOW MANY ELMENT TO BE PROCESSED:?\n";
cin>>no;
cout<<"\n ENTER YOUR ELEMENT\n";
for(i=1;i<=no;i++)
{
cin>>arr[i];
}
cout<<"\n THE ARRAY ELEMENTS ARE: \n";
for(j=1;j<=no;j++)
{
cout<<"\t"<<arr[j]<<endl;
}


int a=1;
do
{
cout<<"\n\n\n\n\n ENTER YOUR ELEMENT TO BE SEARCHED ?\n";
cin>>item;

do
{
while(arr[a]!=item)
{
a=a+1;
}
if(a>no)
{
cout<<"\n Search is unsuccessful...\n";
}
else
{
int loc=a;
cout<<"\n Item is found in loation...: "<<loc<<endl;
}
}
while(i<no);

cout<<"\nDO YOU WANT TO CONTINUE ?(Press Y/N)";
cin>>ch;
}
while(ch=='y');
}
OUTPUT:
enter how many element u want to search...?
3
enter your nos...:
15
10
20
enter ur element to search...?
10
the element is found in the position : 2
do you want to continue?(press Y/N)
y
enter ur element to search...?
30
search is unsuccessful...



No comments:

Post a Comment