Welcome back people,
I am here with a new lesson for everyone. We are currently performing matrix functions/operations using C programing language.
We will see matrix addition and substraction both in one program today. So let us see the program itself.
Program Continue below :
You can copy the code from below :
#include <stdio.h>
void addMatrix(int A[][10], int B[][10], int C[][10], int m, int n) {
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
C[i][j] = A[i][j] + B[i][j];
}
}
}
void subtractMatrix(int A[][10], int B[][10], int C[][10], int m, int n) {
int i, j;
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
C[i][j] = A[i][j] - B[i][j];
}
}
}
int main() {
int A[10][10], B[10][10], C[10][10];
int m, n, i, j;
printf("Enter the number of rows and columns of the matrix: ");
scanf("%d%d", &m, &n);
printf("Enter elements of matrix A:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &A[i][j]);
}
}
printf("Enter elements of matrix B:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &B[i][j]);
}
}
addMatrix(A, B, C, m, n);
printf("Matrix addition:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
subtractMatrix(A, B, C, m, n);
printf("Matrix subtraction:\n");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
printf("%d ", C[i][j]);
}
printf("\n");
}
return 0;
}
In this program, the addMatrix() function takes two matrices A and B as input, and their sum is stored in matrix C. The subtractMatrix() function also takes two matrices A and B as input, and their difference is stored in matrix C.
In the main function, the number of rows and columns of the matrices are taken as input, and the elements of the matrices are also taken as input. The addMatrix() and subtractMatrix() functions are called, and the resulting matrices are displayed on the screen.
Let us look at the output for this program.
So we successfully obtained the desired output for our matrix program. If you want me to write a explanation ( in detail ). Then do tell me in comments and i will make a new or edit this post.
If you also want to learn coding then find your teacher according to your language. For me i am comfortable with hindi language and follow hindi teachers.
You can refer to my teacher below in case you are also comfortable with hindi.
I will soon call myself a trader and a small programmer together.
Happy trading and keep learning what you love.