SHARE
TWEET

Linear Search

Rakibul_Ahasan Oct 14th, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int search(int arr[], int n, int x)
  5. {
  6.     int i;
  7.     for (i = 0; i < n; i++)
  8.         if (arr[i] == x)
  9.             return i;
  10.     return -1;
  11. }
  12.  
  13. int main()
  14. {
  15.     int arr[] = { 2, 3, 4, 10, 40 };
  16.     int x = 10;
  17.     int n = sizeof(arr) / sizeof(arr[0]);
  18.     int result = search(arr, n, x);
  19.    (result == -1)? cout<<"Element is not present in array"
  20.                   : cout<<"Element is present at index " <<result;
  21.    return 0;
  22. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top