fork download
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5. const int dimension = 3;
  6.  
  7. const int arraySize = dimension * dimension;
  8.  
  9. int array[ arraySize ] = { 1, 2, 3,
  10. 4, 5, 6,
  11. 7, 8, 9 };
  12.  
  13. for(int i = 0; i < dimension; ++i)
  14. {
  15. for(int j = 0; j < dimension; ++j)
  16. {
  17. std::cout << array[ i * dimension + j ] << " " ;
  18. }
  19. std::cout << std::endl;
  20. }
  21.  
  22.  
  23. int array2[ arraySize ] = { 0 };
  24.  
  25. for(int i = 0; i < dimension; ++i)
  26. {
  27. for(int j = 0; j < dimension; ++j)
  28. {
  29. array2[ j * dimension + i ] = array[ i * dimension + j ];
  30. }
  31. }
  32.  
  33. std::cout << std::endl;
  34.  
  35. for(int i = 0; i < dimension; ++i)
  36. {
  37. for(int j = 0; j < dimension; ++j)
  38. {
  39. std::cout << array2[ i * dimension + j ] << " " ;
  40. }
  41. std::cout << std::endl;
  42. }
  43.  
  44. return 0;
  45. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
1 2 3 
4 5 6 
7 8 9 

1 4 7 
2 5 8 
3 6 9