Here's a C program to multiply the given two matrices with proper output and explanation. This program uses IF-ELSE Condition, Multidimensional Arrays, Nested Loops and For Loops.
About matrix multiplication: Here are some points to remember regarding matrix multiplication:
- The number of columns of first matrix and number of rows of second matrix must be equal.
- When multiplying matrices, the elements of the rows in the first matrix are multiplied with corresponding columns in the second matrix.
Suppose A and B are two matrices and their product is a matrix AxB or AB. The images below shows how matrices are multiplied.
A and B are two matrices |
AB is the product of matrices A and B |
# include <stdio.h> # include <conio.h> void main() { int mata[10][10], matb[10][10], matc[10][10] ; int i, j, k, row1, col1, row2, col2 ; clrscr() ; printf("Enter the order of first matrix : ") ; scanf("%d %d", &row1, &col1) ; printf("\nEnter the order of second matrix : ") ; scanf("%d %d", &row2, &col2) ; if(col1 == row2) { printf("\nEnter the elements of first matrix : \n\n") ; for(i = 0 ; i < row1 ; i++) for(j = 0 ; j < col1 ; j++) scanf("%d", &mata[i][j]) ; printf("\nEnter the elements of second matrix : \n\n") ; for(i = 0 ; i < row2 ; i++) for(j = 0 ; j < col2 ; j++) scanf("%d", &matb[i][j]) ; for(i = 0 ; i < row1 ; i++) { for(j = 0 ; j < col2 ; j++) { matc[i][j] = 0 ; for(k = 0 ; k < col1 ; k++) matc[i][j] = matc[i][j] + mata[i][k] * matb[k][j] ; } } printf("\nThe resultant matrix is : \n\n") ; for(i = 0 ; i < row1 ; i++) { for(j = 0 ; j < col2 ; j++) { printf("%d \t", matc[i][j]) ; } printf("\n") ; } } else printf("\nMatrix Multiplication is not possible ...") ; getch() ; }
Output of above program:
Enter the order of first matrix : 2 2
Enter the order of second matrix : 2 2
Enter the elements of first matrix :
1 1
2 2
Enter the elements of second matrix :
1 1
1 1
The resultant matrix is :
2 2
4 4
Explanation of above program:
Let us first take a look at all the variables used in this program:
Then using an IF-Else condition the program checks whether the number of columns in first matrix is equal to number of rows in second i.e. col1 is equal to row2. If they are not equal then the program prints "Matrix multiplication is not possible" otherwise this program performs multiplication of two matrices.
Process of matrix multiplication: If col1 = row2 then process of matrix multiplication moves further. Inside IF part, the program first asks the user to enter two matrices and using two FOR loops the values are stored in mata and matb respectively.
Third FOR loop which is a nested FOR loop is responsible for calculating the matrix multiplication. This third for loop is a nested for loop having a combination of three for loops.
Lets take a look at how this nested loop calculates multiplication of two matrices with the help of an example.
Suppose, mata and matb are two 2x2 matrices (refer to the above output). Now refer to the nested loop having three FOR loops. The outermost FOR loop will run for loop variable i = 0 to row1 i.e. 2. Similarly, the first inner loop will run for loop variable j = 0 to col2 i.e. 2 and the innermost loop will run for loop variable k = 0 to col1 i.e. 2.
Now, notice the line matc[i][j] = matc[i][j] + mata[i][k] * matb[k][j] (we'll call it exp1 from now on). This line is the key to calculate the matrix multiplication. Lets take an example to understand how this whole matrix multiplication works. (Refer to the matrices used in the example section)
Since, the matrices are of 2x2 dimension then the resultant matrix will also be of 2x2 dimension. Here, row1 = row2 = col1 = col2 = 2 so the outermost FOR loop will run for i = 0 and 1. Similarly, both inner loops will run for j = 0 and 1 and k = 0 and 1.
Calculating value of matc[0][0]:
When i = 0, j = 0 and k = 0:
Exp1 will evaluate to matc[0][0] = matc[0][0] + mata[0][0] * matb[0][0] = 0 + 1 * 1 = 1. This will give an intermediate value at matc[0][0].
When i = 0, j = 0 and k = 1:
Exp1 will evaluate to matc[0][0] = matc[0][0] + mata[0][1] * matb[1][0] = 1 + 1 * 1 = 2. So, the final value at matc[0][0] will be 2.
Enter the order of second matrix : 2 2
Enter the elements of first matrix :
1 1
2 2
Enter the elements of second matrix :
1 1
1 1
The resultant matrix is :
2 2
4 4
Explanation of above program:
Let us first take a look at all the variables used in this program:
- mata, matb and matc: These are three matrices each with a maximum size of 10x10. mata and matb contain the elements of first and second matrix respectively whereas matc contains the product of mata and matb.
- row1 and row2: There variables hold the number of rows in mata and matb respectively.
- col1 and col2: There variables hold the number of columns in mata and matb respectively.
- i, j and k: These are the loop variables.
Then using an IF-Else condition the program checks whether the number of columns in first matrix is equal to number of rows in second i.e. col1 is equal to row2. If they are not equal then the program prints "Matrix multiplication is not possible" otherwise this program performs multiplication of two matrices.
Process of matrix multiplication: If col1 = row2 then process of matrix multiplication moves further. Inside IF part, the program first asks the user to enter two matrices and using two FOR loops the values are stored in mata and matb respectively.
Third FOR loop which is a nested FOR loop is responsible for calculating the matrix multiplication. This third for loop is a nested for loop having a combination of three for loops.
Lets take a look at how this nested loop calculates multiplication of two matrices with the help of an example.
Suppose, mata and matb are two 2x2 matrices (refer to the above output). Now refer to the nested loop having three FOR loops. The outermost FOR loop will run for loop variable i = 0 to row1 i.e. 2. Similarly, the first inner loop will run for loop variable j = 0 to col2 i.e. 2 and the innermost loop will run for loop variable k = 0 to col1 i.e. 2.
Now, notice the line matc[i][j] = matc[i][j] + mata[i][k] * matb[k][j] (we'll call it exp1 from now on). This line is the key to calculate the matrix multiplication. Lets take an example to understand how this whole matrix multiplication works. (Refer to the matrices used in the example section)
Since, the matrices are of 2x2 dimension then the resultant matrix will also be of 2x2 dimension. Here, row1 = row2 = col1 = col2 = 2 so the outermost FOR loop will run for i = 0 and 1. Similarly, both inner loops will run for j = 0 and 1 and k = 0 and 1.
Calculating value of matc[0][0]:
When i = 0, j = 0 and k = 0:
Exp1 will evaluate to matc[0][0] = matc[0][0] + mata[0][0] * matb[0][0] = 0 + 1 * 1 = 1. This will give an intermediate value at matc[0][0].
When i = 0, j = 0 and k = 1:
Exp1 will evaluate to matc[0][0] = matc[0][0] + mata[0][1] * matb[1][0] = 1 + 1 * 1 = 2. So, the final value at matc[0][0] will be 2.
Calculating value of matc[0][1]:
When i = 0, j = 1 and k = 0:
Exp1 will evaluate to matc[0][1] = matc[0][1] + mata[0][0] * matb[0][1] = 0 + 1 * 1 = 1. This will give an intermediate value at matc[0][1].
When i = 0, j = 1 and k = 1:
Exp1 will evaluate to matc[0][1] = matc[0][1] + mata[0][1] * matb[1][1] = 1 + 1 * 1 = 2. So, the final value at matc[0][1] will be 2.
Calculating value of matc[1][0]:
When i = 1, j = 0 and k = 0:
Exp1 will evaluate to matc[1][0] = matc[1][0] + mata[1][0] * matb[0][0] = 0 + 2 * 1 = 2. This will give an intermediate value at matc[1][0].
When i = 1, j = 0 and k = 1:
Exp1 will evaluate to matc[1][0] = matc[1][0] + mata[1][1] * matb[1][0] = 2 + 2 * 1 = 4. So, the final value at matc[1][0] will be 4.
Calculating value of matc[1][1]:
When i = 1, j = 1 and k = 0:
Exp1 will evaluate to matc[1][1] = matc[1][1] + mata[1][0] * matb[0][1] = 0 + 2 * 1 = 2. This will give an intermediate value at matc[1][1].
When i = 1, j = 1 and k = 1:
Exp1 will evaluate to matc[1][1] = matc[1][1] + mata[1][1] * matb[1][1] = 2 + 2 * 1 = 4. So, the final value at matc[1][1] will be 4.
When i = 1, j = 1 and k = 0:
Exp1 will evaluate to matc[1][1] = matc[1][1] + mata[1][0] * matb[0][1] = 0 + 2 * 1 = 2. This will give an intermediate value at matc[1][1].
When i = 1, j = 1 and k = 1:
Exp1 will evaluate to matc[1][1] = matc[1][1] + mata[1][1] * matb[1][1] = 2 + 2 * 1 = 4. So, the final value at matc[1][1] will be 4.
This is how matrix multiplication of two matrices is calculated.
Tip: Try to analyze this program using matrices of dimension other than 2x2. For example, you can take two 3x3 matrices and try to find the output using the method explained above.
#include
#include
#include //used for exit function
int i,j,k,ch; //Global variable
char ch1; //Global variable
void main()
{ void sub(int a[3][3],int b[3][3],int c[3][3]);
void add(int a[3][3],int b[3][3],int c[3][3]);
void multi(int a[3][3],int b[3][3],int c[3][3]);
void read(int a[3][3],int b[3][3]);
void trans(int a[3][3],int b[3][3]);
void display(int c[3][3]);
clrscr(); //For clearing screen
int a[3][3],b[3][3],c[3][3];
do{
printf("---MATRIX OPERATIONS---\n");
printf("1.Addition\n");
printf("2.Subtraction\n");
printf("3.Multiplication\n");
printf("4.Transpose\n");
printf("5.Exit\n");
printf("Enter your choice-->");
scanf("%d",&ch);
switch(ch)
{
case 1: read(a,b);
add(a,b,c);
printf("\nResultant matrix is\n");
display(c);
break;
case 2: read(a,b);
sub(a,b,c);
printf("\nResultant matrix is\n");
display(c);
break;
case 3: read(a,b);
multi(a,b,c);
printf("\nResultant matrix is \n");
display(c);
break;
case 4: printf("\nEnter the elements in matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}
trans(a,b);
printf("\nTranspose of matrix is given below\n");
display(b);
break;
case 5: exit(0);
default:printf("\nYou Have entered wrong choice\n");
break;
}//End of switch
printf("\nDo you want to continue\a(Y/N)-->");
scanf("%c",&ch1);
if(ch1=='y'||ch=='Y')
printf("Enter your choice again(1-5)-->");
}while(ch1=='y'||ch1=='Y');
getch();
}
void read(int a[3][3],int b[3][3])
{
printf("\nEnter the matrix first matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&a[i][j]);
}
printf("\nEnter the second matrix\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
scanf("%d",&b[i][j]);
}
}
void add(int a[3][3],int b[3][3],int c[3][3]) //Fn to add 2 matrices
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
c[i][j]=a[i][j]+b[i][j];
}
}
void sub(int a[3][3],int b[3][3],int c[3][3]) //Fn to subtract 2 matrices
{ for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
c[i][j]=a[i][j]-b[i][j];
}
}
void multi(int a[3][3],int b[3][3],int c[3][3])//Fn to multiply 2 matrices
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]=0;
for(k=0;k<3;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
}
void trans(int a[3][3],int b[3][3]) //Fn to transpose matrix
{ for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
b[i][j]=a[j][i];
}
}
}
void display(int c[3][3])
{
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d",c[i][j]);
}
printf("\n");
}
}
//Please help do while statement not working
Hi U, what are you getting as output? As far as I can see, please correct IF statement in line no 58.
how would you implement the program so that you input the matrix values via text file
how would you implement the program so that you input the matrix values via text file
Hi There,
I learnt so much in such little time about C Programming Tutorial. Even a toddler could become smart reading of your amazing articles.
I am using 3 vectors each of which is of type struct "ABC". I am trying to find duplicates in each vector using equal_range() method. In order for equal_range() to work, the vectors must be sorted. The criteria for sorting each vector is different, therefore, I can't write a single sort method inside the struct "ABC". Is there any way to write different methods for sorting, one for each vector?
Appreciate your effort for making such useful blogs and helping the community.
Best Regards,
Vindya
didn't give outpuut it say
[Error] ld returned 1 exit status