/*==========================================================*/
/*  TwoThreeD.c - Passing 2-D and 3-D Arrays to Functions   */
/*==========================================================*/

#include <stdio.h>

#define LENGTH 3
#define WIDTH  2
#define DEPTH  4

int twoD[][WIDTH] =
{ 
    { 1, 11 },
    { 2, 22 },
    { 3, 33 }
};

int threeD[][WIDTH][DEPTH] =
{ 
    { { 1, 11, 111, 1111 }, { 2, 22, 222, 2222} },
    { { 3, 33, 333, 3333 }, { 4, 44, 444, 4444} },
    { { 5, 55, 555, 5555 }, { 6, 66, 666, 6666} },
};

/*==========================================================*/

void twoDargument (char *title, int twoD[LENGTH][WIDTH], int length)
{
    int i, j;

    printf ("%s\n", title);
    for (i = 0; i < length; i++)
    {
        for (j = 0; j < WIDTH; j++)
            printf (" %d", twoD[i][j]);
        printf ("\n");
    }
    printf ("\n");
}

/*==========================================================*/

void threeDargument (char *title, int threeD[][WIDTH][DEPTH], int length)
{
    int i, j, k;

    printf ("%s\n", title);
    for (i = 0; i < length; i++)
    {
        for (j = 0; j < WIDTH; j++)
        {
            for (k = 0; k < DEPTH; k++)
                printf (" %d", threeD[i][j][k]);
            printf ("  ");
        }
        printf ("\n");
    }
    printf ("\n");
}

/*==========================================================*/

void twoDpointer (char *title, int *twoD, int length, int width)
{
    int i, j;

    printf ("%s\n", title);
    for (i = 0; i < length; i++)
    {
        for (j = 0; j < width; j++)
            printf (" %d", *twoD++);
        printf ("\n");
    }
    printf ("\n");
}

/*==========================================================*/

int main (void)
{
    printf ("\nDemonstrate how to pass 2-D and 3-D arrays to functions\n\n");
    twoDargument ("Two-D Array", twoD, LENGTH);
    threeDargument ("Three-D Array", threeD, LENGTH);
    twoDpointer ("Two-D Array via Pointer", &(twoD[0][0]), LENGTH, WIDTH);

    return 0;
}

/*==========================================================*/

/* ===== Execution Output: =====

Demonstrate how to pass 2-D and 3-D arrays to functions

Two-D Array
 1 11
 2 22
 3 33

Three-D Array
 1 11 111 1111   2 22 222 2222
 3 33 333 3333   4 44 444 4444
 5 55 555 5555   6 66 666 6666

Two-D Array via Pointer
 1 11
 2 22
 3 33

===== End of Output ===== */
