How to declare and initialize a three-dimensional (3D) array in C++

2 Answers

0 votes
#include <iostream>

/*
    FUNCTION: fill3DArray
    ----------------------
    Fills a 3D array with values.

    Explanation of 3D array structure:
    ----------------------------------
    A 3D array arr[X][Y][Z] can be visualized as:

        X = number of layers (depth)
        Y = number of rows per layer
        Z = number of columns per row

    We fill the array with a simple pattern:
        value = layer + row + column
*/
void fill3DArray(int arr[][3][4], int layers) {
    for (int x = 0; x < layers; x++) {
        for (int y = 0; y < 3; y++) {
            for (int z = 0; z < 4; z++) {
                arr[x][y][z] = x + y + z;   // Example formula
            }
        }
    }
}

/*
    FUNCTION: print3DArray
    -----------------------
    Prints the 3D array layer by layer.
*/
void print3DArray(int arr[][3][4], int layers) {
    for (int x = 0; x < layers; x++) {
        std::cout << "Layer " << x << ":\n";
        for (int y = 0; y < 3; y++) {
            for (int z = 0; z < 4; z++) {
                std::cout << arr[x][y][z] << " ";
            }
            std::cout << std::endl;
        }
        std::cout << std::endl;
    }
}

int main() {
    /*
        DECLARING A 3D ARRAY
        ---------------------
        Syntax:
            type name[depth][rows][columns];

        Here:
            depth   = 2 layers
            rows    = 3 rows per layer
            columns = 4 columns per row
    */
    int arr[2][3][4] = {{{0}}};

    fill3DArray(arr, 2);

    print3DArray(arr, 2);
}



/*
run:

Layer 0:
0 1 2 3 
1 2 3 4 
2 3 4 5 

Layer 1:
1 2 3 4 
2 3 4 5 
3 4 5 6 

*/

 



answered Jun 20 by avibootz
0 votes
#include <iostream>

/*
    FUNCTION: print3DArray
    ----------------------
    Prints a 3D array layer by layer.

    A 3D array arr[L][R][C] is structured as:
        L = number of layers (depth)
        R = number of rows per layer
        C = number of columns per row

    We loop through:
        layer → row → column
*/
void print3DArray(int arr[][3][4], int layers) {
    for (int layer = 0; layer < layers; layer++) {
        std::cout << "Layer " << layer << ":\n";
        for (int row = 0; row < 3; row++) {
            for (int col = 0; col < 4; col++) {
                std::cout << arr[layer][row][col] << " ";
            }
            std::cout << std::endl;
        }
        std::cout << std::endl;
    }
}

/*
    FUNCTION: sum3DArray
    ---------------------
    Example function that computes the sum of all elements
    in the 3D array. This demonstrates how to *use* a 3D array
    inside a function.
*/
int sum3DArray(int arr[][3][4], int layers) {
    int sum = 0;
    for (int layer = 0; layer < layers; layer++)
        for (int row = 0; row < 3; row++)
            for (int col = 0; col < 4; col++)
                sum += arr[layer][row][col];
    return sum;
}

int main() {

    // 2 layers, 3 rows, 4 columns
    // Full initialization of a 3D array
    int arr[2][3][4] = {
        {
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10, 11, 12}
        },
        {
            {13, 14, 15, 16},
            {17, 18, 19, 20},
            {21, 22, 23, 24}
        }
    };

    // Print the 3D array
    print3DArray(arr, 2);

    // Compute and print the sum of all elements
    int total = sum3DArray(arr, 2);
    std::cout << "Sum of all elements = " << total << std::endl;
}



/*
run:

Layer 0:
1 2 3 4 
5 6 7 8 
9 10 11 12 

Layer 1:
13 14 15 16 
17 18 19 20 
21 22 23 24 

Sum of all elements = 300

*/

 



answered Jun 20 by avibootz
...