Advertisement
avr39ripe

cpp1DArrTo2DArr

Apr 7th, 2021
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.50 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.    
  6.     const int arrSizeY{ 4 };
  7.     const int arrSizeX{ 4 };
  8.  
  9.     const int arrSize{ arrSizeY * arrSizeX };
  10.  
  11.     int arrFlat[arrSize]{ 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16 };
  12.  
  13.     int arr[arrSizeY][arrSizeX]{ {1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16} };
  14.  
  15.     for (int y{ 0 }; y < arrSizeY; ++y)
  16.     {
  17.         for (int x{ 0 }; x < arrSizeX; ++x)
  18.         {
  19.             //std::cout << arr[y][x] << '\t';
  20.             std::cout << arrFlat[y * arrSizeX + x] << '\t';
  21.         }
  22.         std::cout << '\n';
  23.     }
  24.    
  25.     return 0;
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement