avr39ripe

twoArrCopy

Nov 5th, 2020 (edited)
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.     const int bigSize{ 10 };
  6.     const int smallSize{ bigSize / 2 };
  7.  
  8.     int bigArr[bigSize];
  9.     int smallA[smallSize];
  10.     int smallB[smallSize];
  11.  
  12.     for (int method{ 2 }; method; --method) //2 - A, 1 - B
  13.     {
  14.         // fill and output bigArr
  15.         std::cout << "bigArr: ";
  16.         for (int bigIdx{ 0 }; bigIdx < bigSize; ++bigIdx)
  17.         {
  18.             bigArr[bigIdx] = rand() % 10;
  19.             std::cout << bigArr[bigIdx] << ' ';
  20.         }
  21.         std::cout << '\n';
  22.  
  23.         // copy from bigArr to two smallA and smallB array
  24.         // there are two methods to copy - A and B
  25.         if (method == 2)
  26.         {
  27.             // method A
  28.             for (int smallIdx{ 0 }; smallIdx < bigSize; ++smallIdx)
  29.             {
  30.  
  31.                 (smallIdx < smallSize ? smallA[smallIdx] : smallB[smallIdx - smallSize]) = bigArr[smallIdx];
  32.             }
  33.  
  34.         }
  35.         else
  36.         {
  37.             // method B
  38.             for (int smallIdx{ 0 }; smallIdx < smallSize; ++smallIdx)
  39.             {
  40.                 smallA[smallIdx] = bigArr[smallIdx];
  41.                 smallB[smallIdx] = bigArr[smallSize + smallIdx];
  42.             }
  43.         }
  44.  
  45.         std::cout << "smallA: ";
  46.         for (int smallIdx{ 0 }; smallIdx < smallSize; ++smallIdx)
  47.         {
  48.             std::cout << smallA[smallIdx] << ' ';
  49.         }
  50.         std::cout << '\n';
  51.  
  52.         std::cout << "smallB: ";
  53.         for (int smallIdx{ 0 }; smallIdx < smallSize; ++smallIdx)
  54.         {
  55.             std::cout << smallB[smallIdx] << ' ';
  56.         }
  57.         std::cout << '\n';
  58.  
  59.     }
  60.  
  61.     return 0;
  62. }
  63.  
Add Comment
Please, Sign In to add comment