fork download
  1. #include <iostream>
  2. #include <iomanip>
  3.  
  4. using namespace std;
  5.  
  6. template <typename T>
  7. void print_matrix(T *matrix, size_t rows, size_t cols)
  8. {
  9. for (size_t i = 0; i < rows; i++, cout << '\n')
  10. for (size_t j = 0; j < cols; j++)
  11. cout << setw(7) << matrix[j + i * cols];
  12. }
  13.  
  14. int main()
  15. {
  16. float matrix1[][3] =
  17. {
  18. {1.2f, 2.3f, 3.4f},
  19. {4.5f, 5.6f, 6.7f},
  20. {7.8f, 8.9f, 9.0f}
  21. };
  22.  
  23. int matrix2[] = {10, 20, 30, 40, 20, 30, 40, 50, 30, 40, 50, 60, 40, 50, 60, 70};
  24. string matrix3[] = {"Dwane", "Alice", "Bruce", "Ellen", "Tom", "Kane"};
  25.  
  26. print_matrix(&matrix1[0][0], 3, 3);
  27. print_matrix(matrix2, 4, 4);
  28. print_matrix(matrix3, 2, 3);
  29.  
  30. return 0;
  31. }
  32.  
  33.  
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
    1.2    2.3    3.4
    4.5    5.6    6.7
    7.8    8.9      9
     10     20     30     40
     20     30     40     50
     30     40     50     60
     40     50     60     70
  Dwane  Alice  Bruce
  Ellen    Tom   Kane