Showing posts with label Linear Search. Show all posts
Showing posts with label Linear Search. Show all posts

Saturday, August 22, 2015

Linear Search in an Array


Q. 106. Program to accept 'n' numbers into an array and Search for a value using the Linear Search method.


Copy Code:

#include <iostream>
using namespace std;

int main()
{
  int a[20], n, i, s;
  cout << "How many Numbers would you enter ? ";
  cin >> n;
  cout << "\nEnter " << n << " Numbers\n";
  for ( i =0 ; i<n ; ++i)
      cin >> a[ i ];

  cout << "\nWhich Number to Search ? ";
  cin >> s;
  for ( i=0 ; i<n ; ++i )
  if ( s == a[ i ] )
      {
        cout << "\n" << s << " is PRESENT at position " << i+1;
        exit (0);
      }
   cout << s << " - Your Search Number is NOT FOUND";
   
   return 0;
}