fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void foo(unsigned int height, unsigned int width, unsigned int depth, const int& value) {
  5.  
  6. std::cout << "size of the matrix: " << height * width * depth << '\n';
  7. int* _3D_matrix = nullptr;
  8. try {
  9. _3D_matrix = new int[height * width * depth];
  10. for (int z = 0; z < depth; z++) {
  11. for (int j = 0; j < height; j++) {
  12. for (int k = 0; k < width; k++) {
  13. auto index = j * height * depth + k * depth + z;
  14. std::cout << "index at: " << index << '\n';
  15. _3D_matrix[index] = value;
  16. }
  17. }
  18. }
  19. }
  20. catch(...) {
  21. delete[] _3D_matrix;
  22. throw;
  23. }
  24.  
  25. }
  26. int main() {
  27. foo(2, 4, 8, 42);
  28. // your code goes here
  29. return 0;
  30. }
Success #stdin #stdout 0s 4516KB
stdin
Standard input is empty
stdout
size of the matrix: 64
index at: 0
index at: 8
index at: 16
index at: 24
index at: 16
index at: 24
index at: 32
index at: 40
index at: 1
index at: 9
index at: 17
index at: 25
index at: 17
index at: 25
index at: 33
index at: 41
index at: 2
index at: 10
index at: 18
index at: 26
index at: 18
index at: 26
index at: 34
index at: 42
index at: 3
index at: 11
index at: 19
index at: 27
index at: 19
index at: 27
index at: 35
index at: 43
index at: 4
index at: 12
index at: 20
index at: 28
index at: 20
index at: 28
index at: 36
index at: 44
index at: 5
index at: 13
index at: 21
index at: 29
index at: 21
index at: 29
index at: 37
index at: 45
index at: 6
index at: 14
index at: 22
index at: 30
index at: 22
index at: 30
index at: 38
index at: 46
index at: 7
index at: 15
index at: 23
index at: 31
index at: 23
index at: 31
index at: 39
index at: 47