Two-dimensional arrays can be used to implement the mathematical concept of matrices. In mathematics, a matrix is a grid of numbers, arranged in rows and columns. Thus, using two-dimensional arrays, we can perform the following operations on an m x n matrix
OPERATIONS
ON TWO-DIMENSIONAL ARRAYS
Two-dimensional
arrays can be used to implement the mathematical concept of matrices. In
mathematics, a matrix is a grid of numbers, arranged in rows and columns. Thus,
using two-dimensional arrays, we can perform the following operations on an m x
n matrix.
Transpose
Transpose of a m x n matrix A is given as a n x m matrix B where,
Bi,j = Aj,i
Sum
Two matrices that are compatible with each other can be added together thereby
storing the result in the third matrix. Two matrices are said to be compatible when
they have the same number of rows and columns. Elements of the matrices can be
added by writing:
Ci,j = Ai,j +
Bi,j
Difference
Two matrices that are compatible with each other can be subtracted thereby
storing the result in the third matrix. Two matrices are said to be compatible
when they have the same number of rows and columns. Elements of the matrices
can be subtracted by writing:
Ci,j = Ai,j -
Bi,j
Product
Two matrices can be multiplied with each other if the number of columns in the
first matrix is equal to the number of rows in the second matrix. Therefore, m
x n matrix A can be multiplied with a p x q matrix if n = P. Elements of the
matrices can be multiplied by writing:
Ci,j = Σ Ai,k Bk,j
for k=1 to k < n
27.
Write a program to read and display a 3 x 3 matrix.
#include <stdio.h>
#include <conio.h>
int main()
int i, j, mat [3] [3];
clrscr();
printf("\n Enter the elements
of the matrix ");
printf("\n
*****************************");
for (i=0; i<3; i++)
{
for(j=0;j<3;j++)
scanf("%d", &mat [i]
[j]);
}
printf("\n The elements of the
matrix are ");
printf("\n
**************************");
for (i=0; i<3;i++)
{
printf("\n");
for (j=0;j<3;j++)
printf("\t%d", mat [i]
[j]);
}
return 0;
}
Output
Enter the elements of the matrix
************************
1 2 3 4 5 6 7 8 9
The elements of the matrix are
************************
1 2 3
4 5 6
7 8 9
28.
Write a program to transpose a 3 x 3 matrix.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, mat [3] [3],
transposed_mat [3] [3];
clrscr();
printf("\n Enter the elements
of the matrix");
printf("\n
********************");
for (i=0;i<3; i++)
{
for(j=0;j<3;j++)
{
scanf("%d", &mat
[i][j]);
}
}
printf("\n The elements of the
matrix are ");
printf("\n
********************");
for (i=0;i<3; i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("\t%d", mat [i]
[j]);
}
for (i=0; i<3; i++)
{
for(j=0;j<3;j++)
transposed_mat [i][j] = mat [j]
[i];
}
printf("\n The elements of the
transposed matrix are are ");
printf("\n
***********************");
for (i=0;i<3; i++)
{
printf("\n");
for(j=0;j<3;j++)
printf("\t%d",
transposed_ mat [i] [j]);
}
return 0;
}
Output
Enter the elements of the matrix
***********************
1 2 3 4 5 6 7 8 9
The elements of the matrix are
***********************
1 2 3
4 5 6
7 8 9
The elements of the transposd
matrix are
***********************
1 4 7
2 5 8
7 8 9
29.
Write a program to input two m x n matrices and then calculate the sum of their
corresponding elements and store it in a third m x n matrix.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j;
int rowsl, colsl, rows2, cols2,
rows_sum, cols_sum;
int mat1 [5] [5], mat2 [5] [5], sum
[5] [5];
clrscr();
printf("\n Enter the number of
rows in the first matrix: ");
scanf("%d", &rows1);
printf("\n Enter the number of
columns in the first matrix: ");
scanf("%d", &cols1);
printf("\n Enter the number of
rows in the second matrix: ");
or scanf("%d", &rows2);
printf("\n Enter the number of
columns in br the second matrix: ");
scanf("%d", &cols2);
if (rowsl != rows2 || cols1 !=
cols2)
{
printf("\n The number of rows
and columns of both the matrices must be equal");
getch();
exit();
}
rows_sum = rowsl;
cols_sum = cols1;
printf("\n Enter the elements
of the first matrix");
printf("\n
***********************");
for (i=0;i<rowsl;i++)
{
for(j=0;j<cols1;j++)
scanf("%d", &mat1 [i]
[j]);
printf("\n Enter the elements
of the second matrix");
printf("\n
***********************");
for (i=0; i<rows2; i++)
{
for(j=0;j<cols2;j++)
scanf("%d", &mat2 [i]
[j]);
}
for (i=0;i<rows_sum; i++)
{
for (j=0;j<cols_sum, j++)
sum [i] [j] = mat1 [i] [j] + mat2
[i] [j];
printf("\n The elements of the
resultant matrix are");
printf("\n
***********************");
for (i=0;i<rows_sum; i++)
{
printf("\n");
for(j=0;j<cols_sum; j++)
printf("\t%d", sum
[i][j]);
}
return 0;
}
Output
Enter the number of rows in the
first matrix: 2
Enter the number of columns in the
first matrix: 2
Enter the number of rows in the
second matrix: 2
Enter the number of columns in the
second matrix: 2
Enter the elements of the first
matrix
**************************
1 2 3 4
Enter the elements of the second
matrix
***************************
5 6 7 8
The elements of the resultant
matrix are
******************************
6
8
10 12
30.
Write a program to multiply two m x n matrices.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, j, k;
int rowsl, colsl, rows2, cols2,
res_rows, res_cols;
int mat1 [5] [5], mat2 [5] [5], res
[5] [5];
clrscr();
printf("\n Enter the number of
rows in the first matrix: ");
scanf("%d", &rows1);
printf("\n Enter the number of
columns in the first matrix: ");
scanf("%d", &cols1);
printf("\n Enter the number of
rows in the second matrix: ");
scanf("%d", &rows2);
printf("\n Enter the number of
columns in the second matrix: ");
scanf("%d", &cols2);
if (colsl != rows2)
{
printf("\n The number of
columns in the first matrix must be equal to the number of rows in the second
matrix");
getch();
exit();
}
res_rows = rows1;
res cols = cols2;
printf("\n Enter the elements
of the first matrix");
printf("\n
**************************");
for (i=0;i<rowsl; i++)
{
for(j=0;j<cols1;j++)
scanf("%d", &mat1 [i]
[j]);
}
printf("\n Enter the elements
of the second matrix");
printf("\n
****************************");
for (i = 0; i < rows2; i++)
{
for (j = 0; j < cols2; j++)
scanf("%d",&mat2 [i]
[j]);
}
for (i = 0; i < res_ rows; i++)
{
j=0;
for(j=0; j < res_cols;j++)
{
res [i] [j]=0;
for (k 0; k < res_cols; k++)
res [i] [j] += mat1 [i] [k] * mat2
[k] [j];
}
}
printf("\n The elements of the
product matrix are");
printf("\n
****************************");
for (i=0;i<res_rows; i++)
{
printf("\n");
for (j = 0; j < res_cols; j++)
printf("\t %d", res [i]
[j]);
}
return 0;
}
Output
Enter the number of rows in the
first matrix: 2
Enter the number of columns in the
first matrix: 2
Enter the number of rows in the
second matrix: 2
Enter the number of columns in the
second matrix: 2
Enter the elements of the first
matrix
***********************
1 2 3 4
Enter the elements of the second
matrix
***********************
5 6 7 8
The elements of the product matrix
are
************************
19 22
43 50
Programming in C: Unit II (a): Arrays : Tag: : with Example C Programs - Operations on Two-Dimensional Arrays
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation
Professional English II
HS3251 2nd Semester 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation
Statistics and Numerical Methods
MA3251 2nd Semester 2021 Regulation M2 Engineering Mathematics 2 | 2nd Semester Common to all Dept 2021 Regulation
Engineering Graphics
GE3251 eg 2nd semester | 2021 Regulation | 2nd Semester Common to all Dept 2021 Regulation
Physics for Electrical Engineering
PH3202 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Basic Civil and Mechanical Engineering
BE3255 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Electric Circuit Analysis
EE3251 2nd Semester 2021 Regulation | 2nd Semester EEE Dept 2021 Regulation
Physics for Electronics Engineering
PH3254 - Physics II - 2nd Semester - ECE Department - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Electrical and Instrumentation Engineering
BE3254 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Circuit Analysis
EC3251 - 2nd Semester - ECE Dept - 2021 Regulation | 2nd Semester ECE Dept 2021 Regulation
Materials Science
PH3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation
Basic Electrical and Electronics Engineering
BE3251 2nd semester Mechanical Dept | 2021 Regulation | 2nd Semester Mechanical Dept 2021 Regulation
Physics for Civil Engineering
PH3201 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation
Basic Electrical, Electronics and Instrumentation Engineering
BE3252 2021 Regulation | 2nd Semester Civil Dept 2021 Regulation
Physics for Information Science
PH3256 2nd Semester CSE Dept | 2021 Regulation | 2nd Semester CSE Dept 2021 Regulation
Basic Electrical and Electronics Engineering
BE3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation
Programming in C
CS3251 2nd Semester CSE Dept 2021 | Regulation | 2nd Semester CSE Dept 2021 Regulation