Tusohian

Linear Search in C++

Jul 27th, 2017
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.68 KB | None | 0 0
  1. #include<iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int arr[100];
  6.     int i, loc=0,item, n, k=1;
  7.     cout<<"Enter the value of range: ";
  8.     cin>>n;
  9.     cout<<"Enter the values: "<<endl;
  10.  
  11.     for(i=1; i<=n; i++)
  12.     {
  13.         cin>>arr[i];
  14.     }
  15.  
  16.     /*cout<<"The value of set is: ";
  17.     for(i=0; i<n; i++)
  18.     {
  19.         cout<<arr[i];
  20.     }*/
  21.  
  22.  
  23.     cout<<"Enter the value for searching: "<<endl;
  24.     cin>>item;
  25.  
  26.     while(k<=n && loc==0)
  27.  
  28.     {
  29.         if(item==arr[k])
  30.         {
  31.           loc=k;
  32.         }
  33.         k++;
  34.     }
  35.  
  36.     if (loc==0)
  37.         cout<<"The value is not found."<<endl;
  38.     else
  39.         cout<<"The value is number "<<loc<<endl;
  40.  
  41. }
Add Comment
Please, Sign In to add comment