Linear search in array for search term presence and location – Computer Sir Ki Class
  

Login
[lwa]



Code Learning #CPP#1630 siteicon   siteicon   siteicon  

Linear search in array for search term presence and location

Search for presence of element in an integer array for search term presence and location of element where it is found first.

Source Code

TC++ #1630

Source Code

#include <iostream>
using namespace std;
int main()
{
int st,found=0,pos=0;
int ar[]={25,10,32,44,99};
int size=sizeof(ar)/sizeof(ar[0]);
cout<<"Enter search term: ";
cin>>st;
for(int i=0;i<size;i++)
{
if(ar[i]==st)
{
found=1;
pos=i+1;
}
}
if(found==1)
cout<<"Search term "
<<st<<" found at position "<<pos<<endl;
else
cout<<"Search term not found"<<endl;
return 0;
}

Test it !

Run Output

Enter search term: 32
Search term 32 found at position 3

- or -

Enter search term: 19
Search term not found


Suggested Filename(s): linsearch.cpp, linpossearch.cpp



Share

sunmitra| Created: 4-Dec-2017 | Updated: 8-Dec-2017|






Back