Two-Dimensional Arrays


    Two-Dimensional Arrays or 2-D

            It is also possible for arrays to have two or more dimensions. The two dimensional array is also called a matrix and used to store table of values.

Declaration

Syntax:
datatype variable-name[row size][column size];

Eg:-                 int a[4][3];
                                              

              
     The table contains a total of 12 values. We can think of this table as a matrix consisting of 4 rows and 3 columns.
     In mathematics, we represent a particular value in a matrix by using two subscripts. Here first subscript will starts from 0 to row size-1 and second subscript will start from 0 to column size-1.

Initializing Two Dimensional arrays:

            Like the one-dimensional arrays, two-dimensional arrays may be initialized by following their declaration with a list of initial values enclosed in braces.

int num [2][3] = {1, 1, 1, 2, 2, 2};

1
1
1
2
2
2

 

This declaration initiates the elements of first row to zero and second row to 2. The initialization is done row by row.
The above statement is equivalently written as

                        int num [2][3] = {{1,1,1}, {2,2,2}};

By surrounding the elements of each row by braces we can also initialize a two-dimensional array in the form of a matrix.
                        int num [2][3] = {
                                                    {1,1,1},
                                                    {2,2,2}
                                                  };
If the values are missing in an initializer, they are automatically set to zero. For instance the statement
                        int num [2][3] = {
                                                      {1,1},
                                                      {2}
                                                   };
will initialize the first two elements of the first row to one, the first element of the second row to two. And all other elements to zero.

1
1
0
2
0
0



If we want to initialize all array values to zero. For instance the statement

                        int num [2][3] = {0);
will initialize the all array elements to zero.

0
0
0
0
0
0



If we have the size of the array is less than the list of values. For instance the statement

int a[2][3]={-10,4,0,7,3,1,8,9};

will return an error saying “Too many initializers”.

Sample Program for Addition of two matrices

#include<stdio.h>
#include<conio.h>
void main()
{
 int a[4][4],b[4][4],c[4][4],i,j,m,n,p,q;
 clrscr();
 printf("\n Enter Order for 1st Matrix: ");
 scanf("%d%d",&m,&n);
 printf("\n Enter Order for 2nd Matrix: ");
 scanf("%d%d",&p,&q);
 if((m==p)&&(n==q))
 {
  printf("\n Enter 1st Matrix elements: ");
  for(i=0;i<m;i++)
  {
   for(j=0;j<n;j++)
   {
    scanf("%d",&a[i][j]);
   }
  }
  printf("\n Enter 2nd Matrix elements: ");
  for(i=0;i<p;i++)
  {
   for(j=0;j<q;j++)
   {
    scanf("%d",&b[i][j]);
   }
  }
  printf("\n Addition of two matrices: \n");
  for(i=0;i<m;i++)
  {
   for(j=0;j<n;j++)
   {
    c[i][j]=a[i][j]+b[i][j];
    printf("%d \t",c[i][j]);
   }
   printf("\n");
  }
 }
 else
 {
  printf("\n Addition not possible...");
 }
 getch();
}

Output

 Enter Order for 1st Matrix: 3 2

 Enter Order for 2nd Matrix: 3 2

 Enter 1st Matrix elements: 1 2 3 4 5 6

Enter 2nd Matrix elements: 6 5 4 3 2 1

Addition of two matrices:
7       7
7       7
7       7

No comments:

Post a Comment