C program for Matrix Multiplication | C code champ

C program for Matrix Multiplication : Let us discuss how to multiply M x N matrix using a C program. First of all we need to understand how an M x N matrix multiplication takes place. Matrix multiplicationis a binary operation that takes a pair of matrices, and produces another matrix as output. Matrix is basically a two dimensional array. Let us see by an example how to multiply a matrix by another matrix :

As above shown, matrix multiplication consists a concept of dot products of rows and columns.

The “Dot Product” is where you multiply matching members, then sum up:

(1, 2, 3) • (7, 9, 11) = 1×7 + 2×9 + 3×11 = 58

We match the 1st members (1 and 7), multiply them, likewise for the 2nd members (2 and 9) and the 3rd members (3 and 11), and finally sum them up.

Lets see for another row ..

(1, 2, 3) • (8, 10, 12) = 1×8 + 2×10 + 3×12 = 64

We can do the same thing for the 2nd row and 1st column:

(4, 5, 6) • (7, 9, 11) = 4×7 + 5×9 + 6×11 = 139

And for the 2nd row and 2nd column:

(4, 5, 6) • (8, 10, 12) = 4×8 + 5×10 + 6×12 = 154

So this is the resultant matrix obtained after multiplication :

Now lets see how we are going to implement above logic of multiplying matrices in C. The below program accepts two things from user:

1. Order of Matrices

2. Content of matrices

Note : If the order of input matrices is incorrect then program will throw an error saying ” Matrices Orders are incorrect, they cannot be multiplied”.

If the order is correct then program will ask you to enter the elements of a matrix, say you defined order as [3,3] then you need to enter 9 elements.

Note : Enter elements as per order say for [3,3] enter elements like below:

Well, I am software programmer and love to code. My hobbies is to do Hacking, Coding, Blogging, Web Designing and playing online games.
Feel free to contact me at shiviskingg@gmail.com or lokesh@hackingloops.com