Advertisement
YouKnowWho07

Linear Search user input

Jun 9th, 2023 (edited)
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #include<iostream>
  3. using namespace std;
  4.  
  5. bool linearSearch(int arr[], int size, int target)
  6. {
  7.     for(int i=0;i<size;i++)
  8.     {
  9.         if(arr[i]==target)
  10.         {
  11.             return true;
  12.         }
  13.     }
  14.     return false;
  15. }
  16.  
  17. void searchNumberInArray()
  18. {
  19.     int numbers[100], n;
  20.     cout << "Enter the number of elements in the array: ";
  21.     cin >> n;
  22.  
  23.     cout << "Enter " << n << " numbers: ";
  24.     for(int i = 0; i < n; i++)
  25.     {
  26.         cin >> numbers[i];
  27.     }
  28.  
  29.     int searchNumber;
  30.     cout << "Enter a number to search: ";
  31.     cin >> searchNumber;
  32.  
  33.     if(linearSearch(numbers, n, searchNumber))
  34.     {
  35.         cout << "Yes, the number is found in the array." << endl;
  36.     }
  37.     else
  38.     {
  39.         cout << "No, the number is not found in the array." << endl;
  40.     }
  41. }
  42.  
  43. int main()
  44. {
  45.     searchNumberInArray();
  46.     return 0;
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement