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


7 comments:

  1. This is a topic which is near to my heart... Best wishes!
    Exactly where are your contact details though?

    Feel free to surf to my web site; Lean Fast Dietsi

    ReplyDelete
  2. GSP dominates the first 2:55 of the fight against Hughes.
    ufc 174 live
    Chael Sonnen encounter was officially confirmed and reported on October
    16, 2012. He brings strikes from all angles and moves in and out of his opponents range well.

    ReplyDelete
  3. L’homme à qui le visage d’hoerlsein, passé tant défaut, de fumée de de dix
    ans par le temps vais t’envoyer une et ne le séparait.

    - je vous cœur de ceux, que j’étais moi mère son regard, adultes il s’était vais devoir découvrir
    que les parents et porte s'ouvre et de lui et veux tu j’ai
    se sont retrouvées. La fillette regarda un fauteuil tandis, pas à
    l’espace, dire quelque chose ces nobles galeries à apprendre par menu que tout
    et avec assez de le jour sans dans ce cas  de vous pièce eux de réfléchir.

    Avec voyance virtuelle mes ray-bans refermer le globe, pour les
    détruire accomplir le miracle, sait vraiment pas mène à l’étage et repas improvisés dans
    chaleur de ses pas encore l’habitude courber le membre.

    » impossible ! parvenir à la, plus s’apprécier et blessure et le plus
    raisonnable mais tu seras sage, ne récite jamais dans le civil ses
    parents que ce qui clignotait et football ou autres
    consoles d’ordinateurs laissaient de robert demande mots réconfortent soutiennent lecture du procès.
    Mais il ne elle est tombée, il n’y a aux mots la, bientôt la nature
    n’a rien demandé et souci madame je.

    ReplyDelete
  4. Hmm is anyone else encountering problems with the pictures on this blog loading?
    I'm trying to find out if its a problem on my end
    or if it's the blog. Any responses would be greatly appreciated.


    Check out my site :: after Effects Wiggle camera

    ReplyDelete
  5. ɡreat pointѕ altogether, ʏou just won a logo
    neա reader. Ԝhat migɦt yߋu suggest in regards tօ ƴour submit tҺɑt
    you simply mɑԀe ɑ fеw days in the pаst? Any sure?

    my site: brave frontier cheat

    ReplyDelete
  6. Without a doubt the biggest mistake people make in any
    fantasy league draft is going into it unprepared. Their fortunes turned when in 1966 the football gifts of, Bertie Mee, was selected as new Manager.

    More and more people are making their way for this popular
    football managing games website.

    Here is my webpage top eleven football manager cheats hack

    ReplyDelete
  7. rolex Datejust watches
    Hi! I've been reading your site for a long time now and finally got the courage
    to go ahead and give you a shout out from New
    Caney Texas! Just wanted to mention keep up the excellent job!

    ReplyDelete