Multidimensional arrays in C-language - C-Tutorial

Latest

Sunday, 9 October 2016

Multidimensional arrays in C-language

Multidimensional arrays

            If an array consists of two or more subscripts then it is called "Multidimensional array".  In generally a multidimensional array consists of three subscripts.

Declarationof Multi dimensional arrays:

The general format of a multidimensional array is:
            Syntax:           datatype ArrayName[size1][size2] - - - - -  - [sizen];
            Example:        int k[2][3][4];

            The above example is a three dimensional array.  Here, compiler allocates memory as in terms of tables.
                                   
INITIALIZATION OF MULTIDIMENSIONAL ARRAYS

            Three dimensional arrays can be initialized into two different ways.
            1. Direct initialization
            2. Through program execution

            Direct Initialization:
     The general form of initialization of a multidimensional array is:
Syntax:           datatype ArrayName[size1][size2]- - - -[sizen] = {List of Values};

            Here, List of Values is separated by comma operator.
                        Example:        int k[2][3][2] = {10,20,30,40,50,60,70,80,90,10,11,12};      

Through program execution:

/* PROGRAM TO READ A MULTDIMENSIONAL ARRAY AND PRINT IT */

main()
{
            int m,n,p,i,j,k,x[10][10][10];
            clrscr();
            printf("\nEnter how many Tables:");
            scanf("%d",&m);
            printf("\nEnter how many rows:");
            scanf("%d",&n);
            printf("\nEnter how many columns:");
            scanf("%d",&p);
            printf("\nEnter Array Elements:");
            for(i=0;i<m;i++)
            {
                        for(j=0;j<n;j++)
                        {
                                    for(k=0;k<p;k++)
                                    scanf("%d",&x[i][j][k]);
                        }
            }
            printf("\nArray Elements Are:");
            for(i=0;i<m;i++)
            {
                        printf("\n");
                        for(j=0;j<n;j++)
                        {
                                    printf("\n");
                                    for(k=0;k<p;k++)
                                    printf("%5d",x[i][j][k]);
                        }
            }

}

No comments:

Post a Comment