Two dimensional array in c

there are some products or programmers, where we have to store data in the form of a table containing both rows and columns, in this context we should use one more subscript to control the columns. Two-dimensional arrays are generally referred as a matrix with both row and column. Here the data type and name of an array will be same and we should choose the size of row and column of an array or leave empty, it detects automatically.

syntax:

Datatype arrayname [rows][columns];

example:

int marks[5][3];
2D arrays in C in c

so, in this example, the array is about marks of a student in three tests. the row is the roll no of student and column is the test NO .so let’s look this array in tabular form with some examples

we can represent the above matrix array as single individual elements .and we can initialize value for each part using index numbers of rows and columns .every element in the matrix is represented by i,j .i as rows and j as columns .

The above example is represented in the form of single individual elements down below.

marks[0][0]=69 marks[0][1]=88 marks[0][2]=76 marks[1][0]=81 marks[1][1]=67 marks[1][2]=53 marks[2][0]=96 marks[2][1]=61 marks[2][2]=93 marks[3][0]=79 marks[3][1]=85 marks[3][2]=47 marks[4][0]=98 marks[4][1]=78 marks[4][2]=61

so ,the sequence of the assignment of arrays will be in row manner. We can initialize the separate elements of an array individually by using index numbers of row and column as shown in the above example.

Initialization:

we can use the above method and this method as similar to representing single dimensional arrays.

syntax:

Datatype arrayname[size1][size2]={x1,x2,x3,..xn};

the above example can be represented in this way too

Int marks[5][3]={69,88,76,81,67,53,96,61,93,79,85,47,98,78,61};
marks[0][0]=69 marks[0][1]=88 marks[0][2]=76
marks[1][0]=81 marks[1][1]=67 marks[1][2]=53
marks[2][0]=96 marks[2][1]=61 marks[2][2]=93
marks[3][0]=79 marks[3][1]=85 marks[3][2]=47
marks[4][0]=98 marks[4][1]=78 marks[4][2]=61

if no value is assigned to respective array element,it is considered as value zero . lets see one example of adding two matrices

example:

#include <stdio.h>
int main(){
int r,c,a[100][100],b[100][100],sum[100][100],i,j;
printf("Enter number of rows (between 1 and 100): ");
scanf("%d",&r);
printf("Enter number of columns (between 1 and 100): ");
scanf("%d",&c);
printf("\nEnter elements of 1st matrix:\n");
/* Storing elements of first matrix entered by user. */
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("Enter element a%d%d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
/* Storing elements of second matrix entered by user. */
printf("Enter elements of 2nd matrix:\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("Enter element a%d%d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
/*Adding Two matrices */
for(i=0;i<r;++i)
for(j=0;j<c;++j)
sum[i][j]=a[i][j]+b[i][j];
/* Displaying the resultant sum matrix. */
printf("\nSum of two matrix is: \n\n");
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
printf("%d ",sum[i][j]);
if(j==c-1)
printf("\n\n");
}
return 0;
}