Dimitar23308

Untitled

Nov 28th, 2025 (edited)
991
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <limits>
  5. using namespace std;
  6.  
  7. int main() {
  8.  
  9.     srand(time(0));
  10.  
  11.     const int N = 25;      
  12.     int arr[N];          
  13.     bool taken[N] = {0};  
  14.     int checked = 0;      
  15.  
  16.  
  17.     cout << "ALL ARRRAY: ";
  18.     for (int i = 0; i < N; i++) {
  19.         arr[i] = rand() % 50 + 1;
  20.         cout << arr[i] << " ";
  21.     }
  22.     cout << "\n";
  23.  
  24.     int M;
  25.     while (true) {
  26.         cout << "ENTER M SEARCHED: ";
  27.         if (!(cin >> M)) {
  28.             cout << "Invalid input. Please enter an inteager.\n";
  29.             cin.clear();
  30.             cin.ignore(numeric_limits<streamsize>::max(), '\n');
  31.             continue;
  32.         }
  33.  
  34.         for (int i = 0; i < N; ++i) taken[i] = false;
  35.         checked = 0;
  36.  
  37.         bool found = false;
  38.         while (checked < N) {
  39.             int index = rand() % N;
  40.             if (taken[index]) continue;
  41.             taken[index] = true;
  42.             checked++;
  43.  
  44.             cout << "CHECKING " << index << " VALUE " << arr[index] << "...\n";
  45.  
  46.             if (arr[index] == M) {
  47.                 cout << "=== FOUND! ===\n";
  48.                 cout << "M = " << M << " IS AT INDEX " << index << "\n";
  49.                 found = true;
  50.                 break;
  51.             }
  52.         }
  53.  
  54.         if (!found) {
  55.             cout << "=== NOT FOUND ===\n";
  56.             cout << "ALL " << N << " ELEMENTS WERE CHECKED.\n";
  57.         }
  58.  
  59.         cout << "\n";
  60.     }
  61.  
  62.     system("pause");
  63.  
  64.     return 0;
  65. }
Advertisement
Add Comment
Please, Sign In to add comment