C Exercises: Multiplication of two Matrices
C Recursion : Exercise-15 with Solution
Write a program in C to multiply two matrix using recursion.
Pictorial Presentation:
Sample Solution:
C Code:
#include<stdio.h>
#define MAX 10
void multiplyMatrix(int [MAX][MAX],int [MAX][MAX]);
int rone,cone,rtwo,ctwo;
int crm[MAX][MAX];
int sum,i=0,j=0,k=0;
int main()
{
int arm[MAX][MAX],brm[MAX][MAX],i,j,k;
printf("\n\n Multiplication of two Matrices :\n");
printf("----------------------------------\n");
printf(" Input number of rows for the first matrix : ");
scanf("%d",&rone);
printf(" Input number of columns for the first matrix : ");
scanf("%d",&cone);
printf(" Input number of rows for the second matrix : ");
scanf("%d",&rtwo);
printf(" Input number of columns for the second matrix : ");
scanf("%d",&ctwo);
if(cone!=rtwo)
{
printf("\n Check col. of first and row of second matrix.");
printf("\n They are different. Try again.\n");
}
else
{
printf("\n Input elements in the first matrix :\n");
for(i=0;i<rone;i++){
for(j=0;j<cone;j++){
printf(" element - [%d],[%d] : ",i,j);
scanf("%d",&arm[i][j]);}}
printf(" Input elements in the second matrix :\n");
for(i=0;i<rtwo;i++){
for(j=0;j<ctwo;j++){
printf(" element - [%d],[%d] : ",i,j);
scanf("%d",&brm[i][j]);}}
printf("\n Here is the elements of First matrix : \n");
for(i=0;i<rone;i++)
{
printf("\n");
for(j=0;j<cone;j++)
{
printf(" %d\t",arm[i][j]);
}
}
printf("\n Here is the elements of Second matrix : \n");
for(i=0;i<rtwo;i++)
{
printf("\n");
for(j=0;j<ctwo;j++)
{
printf(" %d\t",brm[i][j]);
}
}
multiplyMatrix(arm,brm);
}
printf("\n The multiplication of two matrix is : \n");
for(i=0;i<rone;i++)
{
printf("\n");
for(j=0;j<ctwo;j++)
{
printf(" %d\t",crm[i][j]);
}
}
printf("\n\n");
return 0;
}
void multiplyMatrix(int arm[MAX][MAX],int brm[MAX][MAX])
{
if(i<rone)
{ //row of first matrix
if(j<ctwo)
{ //column of second matrix
if(k<cone)
{
sum=sum+arm[i][k]*brm[k][j];
k++;
multiplyMatrix(arm,brm);
}
crm[i][j]=sum;
sum=0;
k=0;
j++;
multiplyMatrix(arm,brm);
}
j=0;
i++;
multiplyMatrix(arm,brm);
}
}
Sample Output:
Multiplication of two Matrices : ---------------------------------- Input number of rows for the first matrix : 2 Input number of columns for the first matrix : 1 Input number of rows for the second matrix : 1 Input number of columns for the second matrix : 2 Input elements in the first matrix : element - [0],[0] : 1 element - [1],[0] : 2 Input elements in the second matrix : element - [0],[0] : 3 element - [0],[1] : 4 Here is the elements of First matrix : 1 2 Here is the elements of Second matrix : 3 4 The multiplication of two matrix is : 3 4 6 8
Multiplication of two Matrices : ---------------------------------- Input number of rows for the first matrix : 2 Input number of columns for the first matrix : 2 Input number of rows for the second matrix : 2 Input number of columns for the second matrix : 2 Input elements in the first matrix : element - [0],[0] : 1 element - [0],[1] : 2 element - [1],[0] : 3 element - [1],[1] : 4 Input elements in the second matrix : element - [0],[0] : 5 element - [0],[1] : 6 element - [1],[0] : 7 element - [1],[1] : 8 Here is the elements of First matrix : 1 2 3 4 Here is the elements of Second matrix : 5 6 7 8 The multiplication of two matrix is : 19 22 43 50
Flowchart:
C Programming Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a program in C to print even or odd numbers in given range using recursion.
Next: Write a program in C to Check whether a given string is Palindrome or not.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
C Programming: Tips of the Day
Static variable inside of a function in C
The scope of variable is where the variable name can be seen. Here, x is visible only inside function foo().
The lifetime of a variable is the period over which it exists. If x were defined without the keyword static, the lifetime would be from the entry into foo() to the return from foo(); so it would be re-initialized to 5 on every call.
The keyword static acts to extend the lifetime of a variable to the lifetime of the programme; e.g. initialization occurs once and once only and then the variable retains its value - whatever it has come to be - over all future calls to foo().
Ref : https://bit.ly/3fOq7XP
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework