2 D Arrays
·       
Array
is set of similar data types, which can be referred by common name. 
·       
Array
is called subscripted variable because array uses the subscript i.e. [] 
·       
The
array is called as indexed variable because the array elements can be accessed
by using index number.
·       
Array
are divided into following types
1)     One dimensional array (or single –
subscripted array)
2)     Two-dimensional array (or double –
subscripted array)
3)     Multi-dimensional array (or multi –
subscripted array)
Two dimension
(2-D) array:
Array declaration
Syntax: 
            Datatype   variable_name  [Row size] [Column size];
                        Or
            Datatype array_name [Row size]
[Column size];
Here, 
Data type – type of element that is to store in array
[]-
known as subscript  
Row Size- is maximum possible rows that can be store in array.
column Size- is maximum possible columns that can be store in array.
Example:
            int a[5][3];      - here, int is datatype ,a is array name/ variable name
and  Row size is 5 Column size is 3.
Array
initialization:
            Datatype
array_name [Row size] [Column size] = {value separated by comma};
            The
array can be initialized as follows,
            int
a[4][2] ={ {11,22},
          {33,44},
          {55,66},
          {77,88} };
Or
            int
a[4] [2] = {11,22,33,44,55,66,77,88};
In all above examples, the 11 is stored in a[0][0], 22 is stored in a[0][1], 33 is stored in a[1][0] and so on…
·       
The
array variable can be initialized only at the time of declaration.
Memory
representation of array:
            Array is stored in memory in row
order and in continuous memory location.
Example,
int a[4][2] ={ {11,22},
          {33,44},
          {55,66},
          {77,88} };
Or
            int
a[4] [2] = {11,22,33,44,55,66,77,88};
| Row / Column | Column no.0 | Column no.1 | 
| Row no.0 | 11 | 22 | 
| Row no.1 | 33 | 44 | 
| Row no.2 | 55 | 66 | 
| Row no.3 | 77 | 88 | 
| a[0][0] | a[0][1] | a[1][0] | a[1][1] | 
| 11 | 22 | 33 | 44 | 
| 1000 | 1002 | 1004 | 1006 | 
Program: Write a program to find sum of two
Matrix. 
/* Program to find sum of two Matrix
*/
#include <stdio.h>
#include <conio.h>
void main()
{
  
int i,j,mat1[3][3],mat2[3][3],sum[3][3];
  
clrscr();
  
printf("\n\nPrint 9 element for first matrix mat1 = ");
  
for(i=0;i<3;i++)
  
{
            for(j=0;j<3;j++)
            {
                        scanf("%d",&mat1[i][j]);
            }
  
}
   
printf("Print 9 element for first matrix mat2 = ");
  
for(i=0;i<3;i++)
  
{
            for(j=0;j<3;j++)
            {
                        scanf("%d",&mat2[i][j]);
            }
  
}
  
for(i=0;i<3;i++)
  
{
            for(j=0;j<3;j++)
            {
                        sum[i][j]=mat1[i][j]+mat2[i][j];
            }
  
}
 printf("\nSum of 3 x 3 Matrix =\n");
  
for(i=0;i<3;i++)
  
{    printf("\n");
            for(j=0;j<3;j++)
            {
                        printf("%d\t",sum[i][j]);
            }
  
}
  
getch();
}
Output:
Program: Write a program perform transpose (transpose
means interchange row in to column and column in to row) matrix.
/* Program to perform transpose (transpose means interchange row in to column and column in to row) matrix */
#include <stdio.h>
#include <conio.h>
void main()
{
  
int i,j,mat[3][3];
  
clrscr();
  
printf("\n\nPrint 9 element for matrix  = ");
  
for(i=0;i<3;i++)
  
{
            for(j=0;j<3;j++)
            {
                        scanf("%d",&mat[i][j]);
            }
  
}
   
printf("\nPrint Matrix\n ");
  
for(i=0;i<3;i++)
  
{
            printf("\n");
            for(j=0;j<3;j++)
            {
                        printf("%d\t",mat[i][j]);
            }
   }
     printf("\n\n
Transpose Matrix(interchange row in to column and column in to row)\n ");
  
for(i=0;i<3;i++)
  
{    printf("\n");
            for(j=0;j<3;j++)
            {
                        printf("%d\t",mat[j][i]);
            }
  
}
  
getch();
} 
Output:


 

0 Comments