Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- #include <ctime>
- #include <limits>
- using namespace std;
- int main() {
- srand(time(0));
- const int N = 25;
- int arr[N];
- bool taken[N] = {0};
- int checked = 0;
- cout << "ALL ARRRAY: ";
- for (int i = 0; i < N; i++) {
- arr[i] = rand() % 50 + 1;
- cout << arr[i] << " ";
- }
- cout << "\n";
- int M;
- while (true) {
- cout << "ENTER M SEARCHED: ";
- if (!(cin >> M)) {
- cout << "Invalid input. Please enter an inteager.\n";
- cin.clear();
- cin.ignore(numeric_limits<streamsize>::max(), '\n');
- continue;
- }
- for (int i = 0; i < N; ++i) taken[i] = false;
- checked = 0;
- bool found = false;
- while (checked < N) {
- int index = rand() % N;
- if (taken[index]) continue;
- taken[index] = true;
- checked++;
- cout << "CHECKING " << index << " VALUE " << arr[index] << "...\n";
- if (arr[index] == M) {
- cout << "=== FOUND! ===\n";
- cout << "M = " << M << " IS AT INDEX " << index << "\n";
- found = true;
- break;
- }
- }
- if (!found) {
- cout << "=== NOT FOUND ===\n";
- cout << "ALL " << N << " ELEMENTS WERE CHECKED.\n";
- }
- cout << "\n";
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment