The greatest mistake you can make in life is to be continually fearing you will make one

Wednesday, 10 March 2010

C Multidimensional Arrays

1.Two-Dimensional Array
  • Two dimensional array is also called a matrix.
  • It is taken as an array of an array.
  • Logically the 2 dimensional array 3 X 2 is taken as
    1 2
    3 4
    5 6
    Here there are 3 arrays. one array in each row
  • The values are stored in a row major form. ie each row is stored first. The above array is stored as 1 2 3 4 5 6.
  • Each row array is represented as a[0],which consist of elements 1 and 2.a[1] consists of 3 and 4. and a[2] consists of 5 and 6
  • Each element of a[0] is accessed as a[0][0] and a[0][1].Thus the value of a[0][0]  is 1 and a[0][1] is 2.
  • In memory whether it is a one dimensional or a two dimensional array the array elements are stored in one continuous chain.
  • The size of the array is number of rows * number of columns
Initializing a 2-Dimensional Array:
  • For initializing a 2-dimensional array we have to specify 2 dimensions. Number of rows and the Number of columns 
  • Ex1:
    int emp[4][3]={
       {1,100,'a'},
       {2,100,'a'},
       {3,98,'b'},
       {4,90,'c'}
    };
  • Ex2: int emp[4][3]={1,100,'a',2,100,'a',3,98,'b',4,90,'c'};
  • While initializing a 2-D array it is necessary to mention the second dimension ie the number of columns.The first dimension ie number of rows is optional.
  • Ex: int a[2][3]={1,2,3,4,5,6}; is same as int a[][3]={1,2,3,4,5,6}; are same. But int a[2][]={1,2,3,4,5,6}; and int a[][]={1,2,3,4,5,6} are wrong way of initializing.
  • In one dimensional arrays name[i] is same as *(name+i). Similarly in 2-dimensional array *(a[2]+1) is same as *(*(a+2)+1).This refer to the 2nd row 1st element.
  • Thus a[2][1] is same as *(a[2]+1) and *(*(a+2)+1)
Example: passing 2 dimensional array to a function
#include<stdio.h>
main()
{
    int i,j,a[3][2];
    for(i=0;i<3;i++)
    {
        for(j=0;j<2;j++)
        {
            a[i][j]=i;
        }
    }
    printArr(a,3,2);
    printArray(a,3,2);
    return 0;
}
void printArr(int a[][2],int m,int n)
{
    int i,j;
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            printf("a[%d][%d]=%d is in address=%8u\n",i,j,a[i][j],&a[i][j]);
        }
    }
}
void printArray(int (*a)[2],int m,int n)
{
    int i,j;
    int *p;
    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {
            p=a+i;
            printf("a[%d][%d]=%d is in address=%p\n",i,j,*(p+j),(p+j));
        }
    }
}
Output
a[0][0]=0 is in address=2609148672
a[0][1]=0 is in address=2609148676
a[1][0]=1 is in address=2609148680
a[1][1]=1 is in address=2609148684
a[2][0]=2 is in address=2609148688
a[2][1]=2 is in address=2609148692
a[0][0]=0 is in address=0x7fff9b847300
a[0][1]=0 is in address=0x7fff9b847304
a[1][0]=1 is in address=0x7fff9b847308
a[1][1]=1 is in address=0x7fff9b84730c
a[2][0]=2 is in address=0x7fff9b847310
a[2][1]=2 is in address=0x7fff9b847314


2.Three-Dimensional Array:
  • Each 3 dimensional array is taken as an array of two-dimensional array
  • Eg: int a[2][3][4]; is a 3 dimensional array. This array consist of 2 arrays of the size 3 X 4. Each array is referred as a[0] & a[1]. Thus a[0] consist of 12 elements and a[1] also consists of 12 elements.
  • The 3 X 4 two dimensional array is then taken as 3 arrays of the size 4.
  • When passing a array to the function,we have to specify the inner dimensions.so while passing this 3 dimension array we have to pass 3 and 4 as inner dimension.
  • The following 2 expressions refer to the same element in the 3 dimensional array
  1. a[1][2][3]
  2. *(*(*(arr+1)+2)+3)
Example Program:
#include<stdio.h>
main()
{
    int i,j,k,x=0,a[2][3][4];
    for(i=0;i<2;i++)
    {
        for(j=0;j<3;j++)
        {
            for(k=0;k<4;k++)
            {
                a[i][j][k]=x;
                x++;
            }
        }
    }
    printArr(a,2,3,4);
    return 0;
}
void printArr(int a[][3][4],int m,int n,int p)
{
    int i,j,k;

    for(i=0;i<m;i++)
    {
        for(j=0;j<n;j++)
        {

            for(k=0;k<p;k++)
            {
                printf("a[%d][%d][%d}=%d is in address=%8u\n",i,j,k,a[i][j][k],&a[i][j][k]);
            }
        }
    }
}
Output

a[0][0][0}=0 is in address=544671328
a[0][0][1}=1 is in address=544671332
a[0][0][2}=2 is in address=544671336
a[0][0][3}=3 is in address=544671340
a[0][1][0}=4 is in address=544671344
a[0][1][1}=5 is in address=544671348
a[0][1][2}=6 is in address=544671352
a[0][1][3}=7 is in address=544671356
a[0][2][0}=8 is in address=544671360
a[0][2][1}=9 is in address=544671364
a[0][2][2}=10 is in address=544671368
a[0][2][3}=11 is in address=544671372
a[1][0][0}=12 is in address=544671376
a[1][0][1}=13 is in address=544671380
a[1][0][2}=14 is in address=544671384
a[1][0][3}=15 is in address=544671388
a[1][1][0}=16 is in address=544671392
a[1][1][1}=17 is in address=544671396
a[1][1][2}=18 is in address=544671400
a[1][1][3}=19 is in address=544671404
a[1][2][0}=20 is in address=544671408
a[1][2][1}=21 is in address=544671412
a[1][2][2}=22 is in address=544671416
a[1][2][3}=23 is in address=544671420


1 comment:

  1. This comment has been removed by a blog administrator.

    ReplyDelete