avr39ripe

cppArrMerge

Mar 24th, 2021
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.64 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.     const int smallSize{ 5 };
  6.     const int bigSize{ smallSize * 2 };
  7.  
  8.     int arrA[smallSize]{1, 3, 5, 7, 9};
  9.     int arrB[smallSize]{1, 4, 6, 7, 10};
  10.     int arrC[bigSize]{};
  11.  
  12.     int idxA{ 0 };
  13.     int idxB{ 0 };
  14.     int idxC{ 0 };
  15.  
  16.     while (idxA < smallSize and idxB < smallSize)
  17.     {
  18.         arrC[idxC++] = arrA[idxA] < arrB[idxB] ? arrA[idxA++] : arrB[idxB++];
  19.     }
  20.  
  21.     while (idxA < smallSize)
  22.     {
  23.         arrC[idxC++] = arrA[idxA++];
  24.     }
  25.  
  26.     while (idxB < smallSize)
  27.     {
  28.         arrC[idxC++] = arrB[idxB++];
  29.     }
  30.  
  31.     for (int idxC{ 0 }; idxC < bigSize; ++idxC)
  32.     {
  33.         std::cout << arrC[idxC] << ' ';
  34.     }
  35.     std::cout << '\n';
  36.     return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment