Saturday, August 27, 2011

How to linearly search an element in array using C++.


//Program to linearly search an element in array using C++.
#include<iostream.h>
#include<conio.h>
void main()
{
 int i, k, loc, item, data[50], n;
 clrscr();
 cout<<"Enter the number of elements in the array:";
 cin>>n;
 cout<<"Enter the value of "<<n<<" elements:";
 for(i=0; i<n; i++)
 {
  cin>>data[i];
 }
 loc=-1;
 cout<<endl<<"Enter the given element";
 cin>>item;
 for(k=0; k<n; k++)
 {
  if(data[k]==item)
  {
   loc=k;
  }
 }
 if(loc==-1)
 {
  cout<<endl<<"Given element is not present";
 }
 else
 {
  cout<<endl<<"Given element is present at location="<<loc;
 }
 getch();
}

Previous Next Home
0 Comments
Comments

Leave a Comment

:)) ;)) ;;) :D ;) :p :(( :) :( :X =(( :-o :-/ :-* :| 8-} :)] ~x( :-t b-( :-L x( =))

Twitter Delicious Facebook Favorites More