Advertisement
Rakibul_Ahasan

Linear Search

Oct 14th, 2019
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.48 KB | None | 0 0
  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. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement