0% found this document useful (0 votes)
60 views9 pages

2-D Array: Syntax: Data - Type: Type of Data To Be Stored

A 2-D array, or two-dimensional array, represents a matrix with rows and columns. It has two indices to access elements - one for the row and one for the column. A 2-D array is declared with the size of rows and columns, and elements are accessed using array[row][column]. Common operations on 2-D arrays include initializing the array, inputting/outputting elements, adding/multiplying matrices by looping through rows and columns.

Uploaded by

megha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
60 views9 pages

2-D Array: Syntax: Data - Type: Type of Data To Be Stored

A 2-D array, or two-dimensional array, represents a matrix with rows and columns. It has two indices to access elements - one for the row and one for the column. A 2-D array is declared with the size of rows and columns, and elements are accessed using array[row][column]. Common operations on 2-D arrays include initializing the array, inputting/outputting elements, adding/multiplying matrices by looping through rows and columns.

Uploaded by

megha
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 9

2-D Array

The array can be:


 1-D array: only one dimension[size], so one loop for the index
whenever you access a[i]
 2-D array: array in the form of matrix, rows and columns. 2
dimension[row size][column size], so two loops i,j will be used, one
for row and inner loop for column whenever you access a[i][j]

 The basic form of declaring a two-dimensional array of size x, y:

Syntax:
 data_type array_name[x][y];
 data_type: Type of data to be stored
 Elements in two-dimensional arrays are commonly referred by x[i][j] where
i is the row number and ‘j’ is the column number.
 A two – dimensional array can be seen as a table with ‘x’ rows and ‘y’
columns where the row number ranges from 0 to (x-1) and column number
ranges from 0 to (y-1). A two – dimensional array ‘x’ with 3 rows and 3
columns is shown below:

 if there are 3 rows, that means 0th,1st,2nd row


 if there are 3 columns, that means 0th,1st,2nd
Then the loops would be like:
for(i=0;i<=2;i++) //rows
{
for(j=0;j<=2;j++) //columns
{
a[i][j]; //matrix elements at location 00,01,02 ,10, and so on.
}
}
 The matrix a[3][4] will contain 12 elements.

Initializing Two – Dimensional Arrays: There are two ways in which a Two-
Dimensional array can be initialized.
First Method:
int x[3][4] = {0, 1 ,2 ,3 ,4 , 5 , 6 , 7 , 8 , 9 , 10 , 11}
The above array have 3 rows and 4 columns. The elements in the braces from
left to right are stored in the table also from left to right. The elements will be
filled in the array in the order, first 4 elements from the left in first row, next 4
elements in second row and so on.
Q.1) WAP in C to input the elements of 3x3 matrix and print them.
#include <stdio.h>
void main ()

int arr[3][3],i,j; /*matrix of 3 rows and 3 columns arr[3[3], i loop for


rows and j loop for column*/

for (i=0;i<3;i++) //row loop i<3 or i<=2

for (j=0;j<3;j++) //column loop

printf("Enter a[%d][%d]: ",i,j); //a[%d][%d] means matrix


location 00 01 02...

scanf("%d",&arr[i][j]);

printf("\n printing the elements ....\n");

for(i=0;i<3;i++)

for (j=0;j<3;j++)

printf("%d\t",arr[i][j]);

}
}

Output

Enter a[0][0]: 56

Enter a[0][1]: 10

Enter a[0][2]: 30

Enter a[1][0]: 34

Enter a[1][1]: 21

Enter a[1][2]: 34

Enter a[2][0]: 45

Enter a[2][1]: 56

Enter a[2][2]: 78

printing the elements ....

56 10 30

34 21 34

45 56 78

Q.2) WAP in C to input the matrix of 3x3 order and find the transpose of the
matrix.
#include<stdio.h>
void main()
{
int a[3][3],b[3][3],i,j; /*a is the original matrix and b will contain the
transpose*/
printf("\nEnter the elements of matrix:");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nThe Transpose of matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=a[j][i]; /*transpose means interchanging the rows and columns of
original matrix*/
printf("%d\t",b[i][j]);
}
}
return 0;
}

//OUTPUT:
//Enter the elements of matrix:
123456789
//The Transpose of matrix is:
147
258
369

Q.3) WAP in C to perform addition of two matrices.

/* we need 3 matrices a,b,sum i.e sum[i][j]=a[i][j]+b[i][j]. the size of the matrix is not given
so we cannot declare the matrix as a[m][n] because variable name cannot be written. so
assumption as we done earlier.a[10][10] ,b[10][10] sum[10][10]*/

#include <stdio.h>
int main() {
int r, c, a[10][10], b[10][10], sum[10][10], i, j;
printf("Enter the number of rows (between 1 and 10): ");
scanf("%d", &r); //the actual no of row size inputted by user
printf("Enter the number of columns (between 1 and 10): ");
scanf("%d", &c); //the actual no of row size inputted by user

printf("\nEnter elements of 1st matrix a:\n");


for (i = 0; i < r; ++i) //i<r where r is the row size(0 to r-1)
{
for (j = 0; j < c; ++j) //i<c where r is the column size(0 to c-1)
{
printf("Enter element");
scanf("%d", &a[i][j]);
}
}

printf("Enter elements of 2nd matrix b:\n");


for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
printf("Enter element ");
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];
}
}

// printing the result


printf("\nSum of two matrices: \n");
for (i = 0; i < r; ++i)
{
for (j = 0; j < c; ++j)
{
printf("%d ", sum[i][j]);

}
}

return 0;
}
Enter the number of rows (between 1 and 100): 2
Enter the number of columns (between 1 and 100): 3

Enter elements of 1st matrix:


Enter element a11: 2
Enter element a12: 3
Enter element a13: 4
Enter element a21: 5
Enter element a22: 2
Enter element a23: 3
Enter elements of 2nd matrix:
Enter element a11: -4
Enter element a12: 5
Enter element a13: 3
Enter element a21: 5
Enter element a22: 6
Enter element a23: 3

Sum of two matrices:


-2 8 7

10 8 6
Q.4) WAP in C to multiply two matrices A[r][c] and B[r][c] and display the
resultant matix.

A user inputs the orders and elements of the matrices. If the multiplication isn't possible, an
error message is displayed. You may have studied the method to multiply matrices in
Mathematics.

int main(){
int a[10][10],b[10][10],mul[10][10],r,c,i,j,k;
printf("enter the number of row=");
scanf("%d",&r);
printf("enter the number of column=");
scanf("%d",&c);
printf("enter the first matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the second matrix element=\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
scanf("%d",&b[i][j]);
}
}

printf("multiply of the matrix=\n");


for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
mul[i][j]=0;
for(k=0;k<c;k++)
{
mul[i][j]+=a[i][k]*b[k][j];
}
}
}
//for printing result
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",mul[i][j]);
}
printf("\n");
}
return 0;
}

Output:

enter the number of row=3


enter the number of column=3
enter the first matrix element=
111
222
333
enter the second matrix element=
111
222
333
multiply of the matrix=
666
12 12 12
18 18 18

Let's try to understand the matrix multiplication of 3*3 and 3*3 matrices by the
figure given below:

You might also like