About Lesson
Program 1:
C Program to Find Sum of Two matrices.
#include <stdio.h>int main() {float a[2][2], b[2][2], c[2][2];int i, j;printf(“Enter the elements of the 1st matrix:n”);for (i = 0; i < 2; i++)for (j = 0; j < 2; j++)scanf(“%f”, &a[i][j]);printf(“Enter the elements of the 2nd matrix:n”);for (i = 0; i < 2; i++)for (j = 0; j < 2; j++)scanf(“%f”, &b[i][j]);// Matrix additionfor (i = 0; i < 2; i++)for (j = 0; j < 2; j++)c[i][j] = a[i][j] + b[i][j];// Display the resultprintf(“nSum of Matrices:n”);for (i = 0; i < 2; i++) {for (j = 0; j < 2; j++)printf(“%.2ft”, c[i][j]); // Print with 2 decimal placesprintf(“n”);}// Comment out or remove the following line if not using a compiler that supports conio.h// getch();return 0;}
Output:
Enter the elements of the 1st matrix:
4
3
3
4
Enter the elements of the 2nd matrix:
3
5
4
3Sum of Matrices:
7.00 8.00
7.00 7.00
Program 2:
C Program to Find Multiplication of Two matrices.
#include <stdio.h>#include <stdlib.h>int main(){int i, j, k;int row1, col1, row2, col2, row3, col3;int mat1[5][5], mat2[5][5], mat3[5][5];// Input for the first matrixprintf(“n Enter the no. of rows in the 1st matrix:n”);scanf(“%d”, &row1);printf(“n Enter the no. of columns in the 1st matrix:n”);scanf(“%d”, &col1);// Input for the second matrixprintf(“n Enter the no. of rows in the 2nd matrix:n”);scanf(“%d”, &row2);printf(“n Enter the no. of columns in the 2nd matrix:n”);scanf(“%d”, &col2);// Check if matrices can be multipliedif (col1 != row2){printf(“n The number of columns in the first matrix must be equal to the number of rows in the 2nd matrix”);exit(0);}// Set dimensions for the result matrixrow3 = row1;col3 = col2;// Input elements of the 1st matrixprintf(“n Enter the elements of the 1st matrix:n”);for (i = 0; i < row1; i++){for (j = 0; j < col1; j++)scanf(“%d”, &mat1[i][j]);}// Input elements of the 2nd matrixprintf(“n Enter the elements of 2nd matrix:n”);for (i = 0; i < row2; i++){for (j = 0; j < col2; j++){scanf(“%d”, &mat2[i][j]);}}// Matrix multiplicationfor (i = 0; i < row3; i++){for (j = 0; j < col3; j++){mat3[i][j] = 0;for (k = 0; k < col1; k++)mat3[i][j] += mat1[i][k] * mat2[k][j];}}// Display the resultprintf(“n Elements of the product matrix are:n”);for (i = 0; i < row3; i++){for (j = 0; j < col3; j++)printf(“%dt”, mat3[i][j]);printf(“n”);}return 0;}
Output:
Enter the no. of rows in the 1st matrix:
3Enter the no. of columns in the 1st matrix:
3Enter the no. of rows in the 2nd matrix:
3Enter the no. of columns in the 2nd matrix:
3Enter the elements of the 1st matrix:
3
3
4
5
5
5
5
5
5Enter the elements of 2nd matrix:
55
3
4
7
6
7
7
7Elements of the product matrix are:
55 64 55
80 95 80
80 95 80
Program 3:
C Program to Find Transpose of a matrices.
#include <stdio.h>#include <stdlib.h>int main(){int a[10][10], trans[10][10], r, c, i, j;printf(“Enter rows and columns of matrix: “);scanf(“%d %d”, &r, &c);printf(“nEnter elements of 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”, &a[i][j]);}/* Displaying the matrix a[][] */printf(“nEntered matrix:n”);for (i = 0; i < r; i++){for (j = 0; j < c; j++){printf(“%d “, a[i][j]);}printf(“n”);}/* Finding transpose of matrix a[][] and storing it in array trans[][] */for (i = 0; i < r; i++)for (j = 0; j < c; j++){trans[j][i] = a[i][j];}/* Displaying the array trans[][] */printf(“nTranspose of matrix:n”);for (i = 0; i < c; i++){for (j = 0; j < r; j++){printf(“%d “, trans[i][j]);}printf(“n”);}return 0;}
Output:
Enter rows and columns of matrix: 3
3Enter elements of matrix:
Enter element a11: 3
Enter element a12: 2
Enter element a13: 1
Enter element a21: 3
Enter element a22: 2
Enter element a23: 1
Enter element a31: 2
Enter element a32: 2
Enter element a33: 1Entered matrix:
3 2 1
3 2 1
2 2 1Transpose of matrix:
3 3 2
2 2 2
1 1 1