Users Online
· Guests Online: 36
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
· Members Online: 0
· Total Members: 188
· Newest Member: meenachowdary055
Forum Threads
Newest Threads
No Threads created
Hottest Threads
No Threads created
Latest Articles
Articles Hierarchy
C Program to Perform Matrix Multiplication using Recursion
The following C program, using recursion, performs Matrix multiplication of two matrices and displays the result. We use 2 D array to represent a matrix and resulting matrix is stored in a different matrix.
Here is the source code of the C program to display a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Perform Matrix Multiplication using Recursion
*/
#include <stdio.h>
void multiply(int, int, int [][10], int, int, int [][10], int [][10]);
void display(int, int, int[][10]);
int main()
{
int a[10][10], b[10][10], c[10][10] = {0};
int m1, n1, m2, n2, i, j, k;
printf("Enter rows and columns for Matrix A respectively: ");
scanf("%d%d", &m1, &n1);
printf("Enter rows and columns for Matrix B respectively: ");
scanf("%d%d", &m2, &n2);
if (n1 != m2)
{
printf("Matrix multiplication not possible.\n");
}
else
{
printf("Enter elements in Matrix A:\n");
for (i = 0; i < m1; i++)
for (j = 0; j < n1; j++)
{
scanf("%d", &a[i][j]);
}
printf("\nEnter elements in Matrix B:\n");
for (i = 0; i < m2; i++)
for (j = 0; j < n2; j++)
{
scanf("%d", &b[i][j]);
}
multiply(m1, n1, a, m2, n2, b, c);
}
printf("On matrix multiplication of A and B the result is:\n");
display(m1, n2, c);
}
void multiply (int m1, int n1, int a[10][10], int m2, int n2, int b[10][10], int c[10][10])
{
static int i = 0, j = 0, k = 0;
if (i >= m1)
{
return;
}
else if (i < m1)
{
if (j < n2)
{
if (k < n1)
{
c[i][j] += a[i][k] * b[k][j];
k++;
multiply(m1, n1, a, m2, n2, b, c);
}
k = 0;
j++;
multiply(m1, n1, a, m2, n2, b, c);
}
j = 0;
i++;
multiply(m1, n1, a, m2, n2, b, c);
}
}
void display(int m1, int n2, int c[10][10])
{
int i, j;
for (i = 0; i < m1; i++)
{
for (j = 0; j < n2; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
}
$ cc pgm23.c
$ a.out
Enter rows and columns for Matrix A respectively: 2
2
Enter rows and columns for Matrix B respectively: 2
2
Enter elements in Matrix A:
12 56
45 78
Enter elements in Matrix B:
2 6
5 8
On matrix multiplication of A and B the result is:
304 520
480 894
Here is the source code of the C program to display a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Perform Matrix Multiplication using Recursion
*/
#include <stdio.h>
void multiply(int, int, int [][10], int, int, int [][10], int [][10]);
void display(int, int, int[][10]);
int main()
{
int a[10][10], b[10][10], c[10][10] = {0};
int m1, n1, m2, n2, i, j, k;
printf("Enter rows and columns for Matrix A respectively: ");
scanf("%d%d", &m1, &n1);
printf("Enter rows and columns for Matrix B respectively: ");
scanf("%d%d", &m2, &n2);
if (n1 != m2)
{
printf("Matrix multiplication not possible.\n");
}
else
{
printf("Enter elements in Matrix A:\n");
for (i = 0; i < m1; i++)
for (j = 0; j < n1; j++)
{
scanf("%d", &a[i][j]);
}
printf("\nEnter elements in Matrix B:\n");
for (i = 0; i < m2; i++)
for (j = 0; j < n2; j++)
{
scanf("%d", &b[i][j]);
}
multiply(m1, n1, a, m2, n2, b, c);
}
printf("On matrix multiplication of A and B the result is:\n");
display(m1, n2, c);
}
void multiply (int m1, int n1, int a[10][10], int m2, int n2, int b[10][10], int c[10][10])
{
static int i = 0, j = 0, k = 0;
if (i >= m1)
{
return;
}
else if (i < m1)
{
if (j < n2)
{
if (k < n1)
{
c[i][j] += a[i][k] * b[k][j];
k++;
multiply(m1, n1, a, m2, n2, b, c);
}
k = 0;
j++;
multiply(m1, n1, a, m2, n2, b, c);
}
j = 0;
i++;
multiply(m1, n1, a, m2, n2, b, c);
}
}
void display(int m1, int n2, int c[10][10])
{
int i, j;
for (i = 0; i < m1; i++)
{
for (j = 0; j < n2; j++)
{
printf("%d ", c[i][j]);
}
printf("\n");
}
}
$ cc pgm23.c
$ a.out
Enter rows and columns for Matrix A respectively: 2
2
Enter rows and columns for Matrix B respectively: 2
2
Enter elements in Matrix A:
12 56
45 78
Enter elements in Matrix B:
2 6
5 8
On matrix multiplication of A and B the result is:
304 520
480 894
Comments
No Comments have been Posted.
Post Comment
Please Login to Post a Comment.