BlackWolfy

Check when is the first time an element appears in an array

Feb 10th, 2023
1,041
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.62 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. int main()
  4. {
  5.     int b, n, index=0;
  6.     cout << "How many numbers will you check: "<<endl;
  7.     cin >> n;
  8.     int* a;
  9.     a = new int[n];
  10.     cout << "What is the element you want to check: "<<endl;
  11.     cin >> b;
  12.     for (int i = 0; i < n; i++) { //loop for element input
  13.         cout << "Enter an element: " << endl;
  14.         cin >> a[i];
  15.     }
  16.     for (int i = 0; i < n; i++) {//loop for checking the key element
  17.         if (a[i] == b) {
  18.             index = i;
  19.             break;
  20.         }
  21.     }
  22.     cout << "The element appears for the first time at number: " << index + 1;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment