Multidimensional Arrays

 Hi, you are welcome to this blog...........................

What is multidimensional array? Let's see about this.

We can identify a table as a 2D array. this table has two rows and three columns. Mtr[  ][  ] is a matrix.

table1






Take example as we have a matrix as mtr. Also mtr has 4 rows and 3 columns.(picture 1)

picture 1





Above matrix,

1, 2,3 --> first row     

4,5,8 --> second row

7,9,6 --> third row 

          12,11,17 --> fourth row

1,4,7,12 --> first column

2,5,9,11 --> second column

3,8,6,17 --> third column

 We can initialize this matrix as mtr[4][3].  In first square bracket how many rows and second one how many columns.

This is an initialization of a matrix. matrix is x.

int x[2][3]={{1,2,3},
{3,4,5}};

Also this method is correct.

int x[2][3]={1,2,3,4,5,6};

 In short,

how to access 2D array:

int matrix[3][4]; // declare the matrix
matrix[0][0]=5; // assign value 5 to location of matrix[0][0]
matrix[3][2]=12; //assign value 12 to location of matrix[3][2]

Now  Let's print the matrix mentioned in picture 1.

There are 4 rows and 3 columns :  Therefore we should get mtr[4][3].

data type of  array is int(integer).

int mtr[4][3]= {  {1,2,3},

                           {4,5,8},

                           {7,9,6},

                           {12,11,17}

                         };

See the full code (using C programming language) how to print this matrix.

#include <stdio.h>
int main() {
int i,j;
int mtr[4][3]={{1,2,3},
{4,5,8},
{7,9,6},
{12,11,17}
};
for(i=0;i<4;i++){
for(j=0;j<3;j++){
printf("%d ",mtr[i][j]);
}
printf("\n");
}
return 0;
}

Let's try to go depth, How to get user input? following code is written for get user inputs.

#include <stdio.h>
int main() {
   
int i,j,row,col;
    printf(
"How many rows do you wish to input: \n");
    scanf(
"%d",&row);
    printf(
"How many columns do you wish to input: \n");
    scanf(
"%d",&col);
   
int matrix[row][col];
   
//get user's input
   
for(i=0;i<row;i++){
       
for(j=0;j<col;j++){
            printf(
"Input [%d][%d] : ",i,j);
            scanf(
"%d",&matrix[i][j]);
        }
    }
   
//print the matrix.
   
for(i=0;i<row;i++){
       
for(j=0;j<col;j++){
            printf(
"%d ",matrix[i][j]);
        }
        printf(
"\n");
    }
   
return 0;
}


How to get the summation of two matrixes. In following code there are two matrixes called mtr1 and mtr2. variables are taken as r1,r2,c1,c2. r1for number of rows of matrix 1, r2 for number of rows of matrix 2, c1 for number of columns for matrix 1 and c2 for number of columns for matrix 2.

#include<stdio.h>
int main(){
int i,j,r1,r2,c1,c2;
printf("How many rows do you input: ");
scanf("%d",&r1);
printf("How many columns do you input: ");
scanf("%d",&c1);
int mtr1[r1][c1];
// get user input.
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
printf("Enter [%d][%d] = ",i,j);
scanf("%d",&mtr1[i][j]);
}
}

printf("How many rows do you input: ");
scanf("%d",&r2);
printf("How many columns do you input: ");
scanf("%d",&c2);
int mtr2[r2][c2];
//get user input
for(i=0;i<r2;i++){
for(j=0;j<c2;j++){
printf("Enter [%d][%d] = ",i,j);
scanf("%d",&mtr2[i][j]);
}
}

printf("Matrix 1:\n");
//print the matrix 1.
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
printf("%d ",mtr1[i][j]);
}
printf("\n");
}

printf("Matrix 2:\n");
//print the matrix 2.
for(i=0;i<r2;i++){
for(j=0;j<c2;j++){
printf("%d ",mtr2[i][j]);
}
printf("\n");
}

int ans[r1][c1]={};
printf("Sum of matrix 1 and matrix 2:\n");
// to get addition of two matrices row of matrix 1 = row of matrix 2 and
// columns of matrix 1 = columns o matrix 2 , therefore r1=r2 and c1=c2;
if(r1==r2 && c1==c2){
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
ans[i][j]+=mtr1[i][j]+mtr2[i][j];
printf("%d ",ans[i][j]);
}
printf("\n");
}
}
else{
printf("Can't add because are not in same dimensions.");
}

return 0;
}


How to get the multiplication of two matrixes. Following code is for multiply two matrixes.

#include<stdio.h>
int main(){
int i,j,k,r1,r2,c1,c2;
printf("How many rows do you input: ");
scanf("%d",&r1);
printf("How many columns do you input: ");
scanf("%d",&c1);
int mtr1[r1][c1];
// get user input.
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
printf("Enter [%d][%d] = ",i,j);
scanf("%d",&mtr1[i][j]);
}
}

printf("How many rows do you input: ");
scanf("%d",&r2);
printf("How many columns do you input: ");
scanf("%d",&c2);
int mtr2[r2][c2];
//get user input
for(i=0;i<r2;i++){
for(j=0;j<c2;j++){
printf("Enter [%d][%d] = ",i,j);
scanf("%d",&mtr2[i][j]);
}
}

printf("Matrix 1:\n");
//print the matrix 1.
for(i=0;i<r1;i++){
for(j=0;j<c1;j++){
printf("%d ",mtr1[i][j]);
}
printf("\n");
}

printf("Matrix 2:\n");
//print the matrix 2.
for(i=0;i<r2;i++){
for(j=0;j<c2;j++){
printf("%d ",mtr2[i][j]);
}
printf("\n");
}

if(c1==r2){
int ans[r1][c2];
printf("Multiplication of matrix 1 and matrix 2: \n");
for(i=0;i<r1;i++){
for(j=0;j<c2;j++){
ans[i][j]=0;
for(k=0;k<r2;k++){
ans[i][j]+=mtr1[i][k]*mtr2[k][j];
}
printf("%d ",ans[i][j]);
}
printf("\n");
}
}
else{
printf("Matrix 1 and Matrix 2 can't be multiplied");
}

return 0;
}





Comments