avr39ripe

cppSortInsertInput

Jul 21st, 2021
949
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.     const int arrSize{ 10 };
  6.     int arr[arrSize]{};
  7.     int tmp;
  8.     //int arr[arrSize]{ 1,2,3,4,5,6,7,8,9,10 };
  9.     //int arr[arrSize]{};
  10.     //int arr[arrSize]{ 1,1,2,2,3,4,6,8,9,11 };
  11.  
  12.     std::cout << "Enter FIRST element of array: \n";
  13.     std::cin >> arr[0];
  14.  
  15.     for (int i{ 0 }; i < arrSize; ++i) { std::cout << arr[i] << ' '; } std::cout << '\n';
  16.  
  17.     for (int insPos{ 1 }; insPos < arrSize; ++insPos)
  18.     {
  19.         std::cout << "Enter NEXT element of array: \n";
  20.         std::cin >> arr[insPos];
  21.  
  22.         for (int testPos{ insPos }; testPos > 0; --testPos)
  23.         {
  24.             if (arr[testPos] < arr[testPos - 1])
  25.             {
  26.                 tmp = arr[testPos];
  27.                 arr[testPos] = arr[testPos - 1];
  28.                 arr[testPos - 1] = tmp;
  29.  
  30.                 for (int i{ 0 }; i < arrSize; ++i) { std::cout << arr[i] << ' '; } std::cout << '\n';
  31.             }
  32.         }
  33.     }
  34.  
  35.     for (int i{ 0 }; i < arrSize; ++i) { std::cout << arr[i] << ' '; } std::cout << '\n';
  36.  
  37.     return 0;
  38. }
RAW Paste Data