One of the very popular programs in C programming is Matrix Multiplication. The manual method of multiplication procedure involves a large number of calculations especially when it comes to higher order of matrices, whereas a program in C can carry out the operations with short, simple and understandable codes. Matrix Multiplication in C can be done in two ways: without using functions and by passing matrices into functions. In this post, I’ve included source code for both these methods with sample outputs for each.
The source codes of these two programs for Matrix Multiplication in C programming are to be compiled in Code::Blocks. Running them on Turbo C and other platforms might require a few modifications to the code. To help you understand the programs better, there are multiple comment lines in the source code.
A Brief Introduction to Matrix Multiplication:
You probably know how to multiply two matrices. Anyway, I’ve presented these three pictures below which clearly show how matrix multiplication takes place. The same idea as shown in these pictures has been followed in the same order in the program source codes for Matrix Multiplication in C. (For matrix multiplication, the column of the first matrix should be equal to the row of the second.)
Consider two matrices A and B of order 3×3 as shown below. Let’s denote the elements of matrix A by aijand those of matrix B by bij as shown below. These aij and bij are asked as inputs in the form of arrays inC program for Matrix Multiplication.
The matrix multiplication takes place as shown below, and this same procedure is is used formultiplication of matrices using C.
Solving the procedure manually would require nine separate calculations to obtain each element of the final matrix X. These nine separate calculations have been done using very few lines of code involving loops and function in C program for Matrix Multiplication.
Source Code for Matrix Multiplication in C without using function:
The above Matrix Multiplication in C program first asks for the order of the two matrices. If in the entered orders, the column of first matrix is equal to the row of second matrix, the multiplication is possible; otherwise, new values should be entered in the program.
The program then asks for the respective elements of the two matrices and multiplies them using loops as shown in the program. Finally, the resultant matrix obtained upon multiplication is printed. The final output screen is:
Source Code for Matrix Multiplication in C by passing arrays to functions:
The operations involving loops and calculations in this program are similar to those in the previous one. The only difference is that this Matrix Multiplication program in C uses functions to pass arrays of matrices. There are three separate user-defined functions in this program to read data, preform matrix multiplication operation and display the resultant matrix.
Both these source codes are bug-free and have been tested on Code::Blocks with the inputs as shown in the output screens.
No comments:
Post a Comment