C Program to Multiply two Matrices by Passing Matrix to a Function
This program asks the user to enter the size of the matrix (rows and column).
Then, it asks the user to enter the elements of those matrices and finally adds and displays the result.
To perform this task three functions are made:
- To takes matrix elements from user
enterData()
- To multiply two matrix
multiplyMatrices()
- To display the resultant matrix after multiplication
display()
Example: Multiply Matrices by Passing it to a Function
#include <stdio.h>
void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond);
void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int multResult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond);
void display(int mult[][10], int rowFirst, int columnSecond);
int main()
{
int firstMatrix[10][10], secondMatrix[10][10], mult[10][10], rowFirst, columnFirst, rowSecond, columnSecond, i, j, k;
printf("Enter rows and column for first matrix: ");
scanf("%d %d", &rowFirst, &columnFirst);
printf("Enter rows and column for second matrix: ");
scanf("%d %d", &rowSecond, &columnSecond);
// If colum of first matrix in not equal to row of second matrix, asking user to enter the size of matrix again.
while (columnFirst != rowSecond)
{
printf("Error! column of first matrix not equal to row of second.\n");
printf("Enter rows and column for first matrix: ");
scanf("%d%d", &rowFirst, &columnFirst);
printf("Enter rows and column for second matrix: ");
scanf("%d%d", &rowSecond, &columnSecond);
}
// Function to take matrices data
enterData(firstMatrix, secondMatrix, rowFirst, columnFirst, rowSecond, columnSecond);
// Function to multiply two matrices.
multiplyMatrices(firstMatrix, secondMatrix, mult, rowFirst, columnFirst, rowSecond, columnSecond);
// Function to display resultant matrix after multiplication.
display(mult, rowFirst, columnSecond);
return 0;
}
void enterData(int firstMatrix[][10], int secondMatrix[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond)
{
int i, j;
printf("\nEnter elements of matrix 1:\n");
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnFirst; ++j)
{
printf("Enter elements a%d%d: ", i + 1, j + 1);
scanf("%d", &firstMatrix[i][j]);
}
}
printf("\nEnter elements of matrix 2:\n");
for(i = 0; i < rowSecond; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
printf("Enter elements b%d%d: ", i + 1, j + 1);
scanf("%d", &secondMatrix[i][j]);
}
}
}
void multiplyMatrices(int firstMatrix[][10], int secondMatrix[][10], int mult[][10], int rowFirst, int columnFirst, int rowSecond, int columnSecond)
{
int i, j, k;
// Initializing elements of matrix mult to 0.
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
mult[i][j] = 0;
}
}
// Multiplying matrix firstMatrix and secondMatrix and storing in array mult.
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
for(k=0; k<columnFirst; ++k)
{
mult[i][j] += firstMatrix[i][k] * secondMatrix[k][j];
}
}
}
}
void display(int mult[][10], int rowFirst, int columnSecond)
{
int i, j;
printf("\nOutput Matrix:\n");
for(i = 0; i < rowFirst; ++i)
{
for(j = 0; j < columnSecond; ++j)
{
printf("%d ", mult[i][j]);
if(j == columnSecond - 1)
printf("\n\n");
}
}
}
Output
Enter rows and column for first matrix: 3 2 Enter rows and column for second matrix: 3 2 Error! column of first matrix not equal to row of second. Enter rows and column for first matrix: 2 3 Enter rows and column for second matrix: 3 2 Enter elements of matrix 1: Enter elements a11: 3 Enter elements a12: -2 Enter elements a13: 5 Enter elements a21: 3 Enter elements a22: 0 Enter elements a23: 4 Enter elements of matrix 2: Enter elements b11: 2 Enter elements b12: 3 Enter elements b21: -9 Enter elements b22: 0 Enter elements b31: 0 Enter elements b32: 4 Output Matrix: 24 29 6 25
a step-by-step explanation of a C program that multiplies two matrices by passing the matrices to a function:
#include <stdio.h>
#define ROWS 3
#define COLS 3
// function prototype
void matrix_multiply(int a[][COLS], int b[][COLS], int c[][COLS]);
int main() {
int a[ROWS][COLS], b[ROWS][COLS], c[ROWS][COLS];
printf("Enter elements of matrix A:\n");
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
scanf("%d", &a[i][j]);
}
}
printf("Enter elements of matrix B:\n");
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
scanf("%d", &b[i][j]);
}
}
matrix_multiply(a, b, c); // call the matrix_multiply() function and pass matrices a, b, and c
printf("Resultant matrix C:\n");
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
printf("%d ", c[i][j]);
}
printf("\n");
}
return 0;
}
// function definition
void matrix_multiply(int a[][COLS], int b[][COLS], int c[][COLS]) {
for (int i = 0; i < ROWS; i++) {
for (int j = 0; j < COLS; j++) {
c[i][j] = 0;
for (int k = 0; k < COLS; k++) {
c[i][j] += a[i][k] * b[k][j];
}
}
}
}
Let’s break it down step-by-step:
We include the necessary header file
stdio.h
for input/output functions.We define constants
ROWS
andCOLS
to represent the number of rows and columns of the matrices.We declare three integer matrices
a
,b
, andc
of sizeROWS
xCOLS
.We prompt the user to enter the elements of the matrix
a
using nestedfor
loops and read them in usingscanf()
.We prompt the user to enter the elements of the matrix
b
using nestedfor
loops and read them in usingscanf()
.We call the
matrix_multiply()
function and pass the matricesa
,b
, andc
as arguments.The
matrix_multiply()
the function takes in three 2D integer arraysa
,b
, andc
. Inside the function, we use nestedfor
loops to iterate over all the elements of the resultant matrixc
.For each element of
c[i][j]
, we initialize it to 0 and then calculate its value by multiplying the corresponding row of the matrixa
with the corresponding column of the matrixb
and summing up the products.Once the function completes execution, the values of the matrix
c
inmain()
are updated with the product of matricesa
andb
.We print the resultant matrix
c
using nestedfor
loops andprintf()
.Finally, we return 0 to indicate successful completion of the program.