Learn to do matrix multiplication with C programming Language

in c-programming •  2 years ago 

Welcome back to my blog,
Today we will learn about matrix multiplication using c programming language.

If anyone was having mathematics as subject in higher education then they must know how time consuming it was to calculate the multiplication of matrix. Here with a C program we will do it in seconds.

Here is the program itself.

image.png

program continues below.

image.png

You can copy the entire program from below.

#include <stdio.h>

int main() {
int a[3][3], b[3][3], c[3][3], i, j, k;

// Inputting values for matrix A
printf("Enter elements of matrix A (3x3):\n");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
scanf("%d", &a[i][j]);
}
}

// Inputting values for matrix B
printf("Enter elements of matrix B (3x3):\n");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
scanf("%d", &b[i][j]);
}
}

// Initializing elements of matrix C to 0
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
c[i][j] = 0;
}
}

// Multiplying matrices A and B and storing the result in matrix C
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
for(k = 0; k < 3; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}

// Printing the resulting matrix C
printf("Resulting matrix (C = A x B):\n");
for(i = 0; i < 3; i++) {
for(j = 0; j < 3; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}

return 0;
}

This program takes two 3x3 matrices as input from the user, multiplies them, and stores the result in a third matrix. The resulting matrix is then printed to the console.

Note that matrix multiplication is only possible if the number of columns in the first matrix is equal to the number of rows in the second matrix, so this program assumes that the matrices being multiplied are both 3x3.

Let us look at the output now.

image.png

So we can see that our program output shows us all the three matrix. Matrix A,B,and,C.

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.

Source

I will soon call myself a trader and a small programmer together.

Happy trading and keep learning what you love.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE BLURT!