LINEAR SEARCH USING ARRAY
Algorithm:
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
-----------------------------------------------------------------------------------------------------------
FLOW CHART
-----------------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------------------
C++ IMPLEMENTATION
------------------------------------------------------------------------------------------------------------
//sample program for Linier 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 ELMENT TO BE SEARCHED ?\n";
cin>>item;
do
{
while(arr[a]!=item)
{
a=a+1;
}
if(a>no)
{
cout<<"\n Search is unsucces full...\n";
}
else
{
int loc=a;
cout<<"\n Item is found in loation...: "<<loc<<endl;
}
}
while(i<no);
cout<<"\n DO YOU FINISHED...?\n OR YOU WANT TO USE PROGRAM again ?(press Y / N)\n";
cin>>ch;
}
while(ch=='y');
}
//*************ENJOY PROGRAMMING*************
//out put
//enter how many element u want to search...?
//3
//enter your nos...:
//15
//10
//20
//enter ur elemnt to search...?
//10
//the element is found in the position : 2
//do u want the program run again (y/n)
//y
//enter ur element to search...?
//30
//sorry...! your element is not found...:
No comments:
Post a Comment