avr39ripe

cppLinearSearch

Mar 24th, 2021
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.     const int arrSize{ 10 };
  6.     int arr[arrSize]{10,12,3,8,15,2,17,1,3,10};
  7.     int key{ 100 };
  8.     int foundIdx{ -1 };
  9.  
  10.     for (int i{ 0 }; i < arrSize; ++i)
  11.     {
  12.         std::cout << arr[i] << ' ';
  13.     }
  14.     std::cout << '\n';
  15.  
  16.  
  17.     for (int i{ 0 }; i < arrSize; ++i)
  18.     {
  19.         if (arr[i] == key) { foundIdx = i; break; }
  20.     }
  21.  
  22.     if (foundIdx >= 0)
  23.     {
  24.         std::cout << "Key " << key << " was found on index " << foundIdx << '\n';
  25.     }
  26.     else
  27.     {
  28.         std::cout << "Key " << key << " was not found!\n";
  29.     }
  30.    
  31.     return 0;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment