fork download
  1. #include<iostream>
  2.  
  3. void func(int x, int y)
  4. {
  5. // initialising
  6. int **matrix = new int *[x];
  7. for (int i = 0; i < x; i++)
  8. {
  9. matrix[i] = new int[y];
  10. }
  11. // filling with 0
  12. for (int row = 0; row < x; row++)
  13. {
  14. for (int cols = 0; cols < y; cols++)
  15. {
  16. matrix[row][cols] = 0;
  17. }
  18. }
  19. // printing
  20. for (int i = 0; i < (x); ++i)
  21. {
  22. for (int j = 0; j < (y); ++j)
  23. {
  24. std::cout << (matrix[i][j]) << ", ";
  25. }
  26. std::cout << std::endl;
  27. }
  28.  
  29. std::cout << "test2" << std::endl;
  30.  
  31. for(int i = 0; i < x; i++)
  32. delete[]matrix[i]; // clean up each y
  33.  
  34. delete[]matrix; // clean up x
  35. }
  36.  
  37. int main()
  38. {
  39. func(5, 5);
  40. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 
test2