Program to multiply two matrices in c language.
In order to perform matrix multiplication we took two matrices by name matrix1 and matrix2 which hold the values of two matrices.
And to store the result after multiplication of matrix1 and matrix2 we took another matrix by name res_matrix .
r1,c1 represents the size of first matrix and r2,c2 represents the size of second matrix.
Condition for multiplication of two matrices is specified below.
In order to multiply two matrices first we have to check the condition.
If condition satisfies then only we can multiply two matrices else multiplication not possible.
The condition is number of columns in first matrix should be equal to number of rows in second matrix i.e; c1= = r2.
And after multiplication the size of resultant matrix will be r1,c2.
So first read sizes of two matrix and then check the condition for multiplication.
In this we took two matrices by name matrix1 and matrix2 which hold the values of two matrices.
And we took another matrix by name res_matrix to store the result after multiplication of matrix1 and matrix2.
#include <stdio.h>
void main(){
int matrix1[10][10], matrix2[10][10], res_matrix[10][10], i, j, k;
// reading the size of first matrix
printf(" enter the size of first matrix < 10 ");
scanf("%d%d", &r1, &c1) ;
// reading the size of second matrix
printf(" enter the size of second matrix < 10 ");
scanf("%d%d", &r2, &c2) ;
// sizes of two matrices has to be checked before multiplication of two matrices.
//checking the sizes of two matrices for multiplication.
//the cond. for multiplication of matrix is number of columns in first matrix should be
// equal to number of rows in second matrix i.e; c1==r2.
if( c1==r2 ){
// reading the elements/ values of first matrix
printf( " \n please specify /enter /provide the values of matrix (first)");
for( i=0; i<r1 ;i++){
for(j=0;j<c1;j++){
scanf( "%d", &matrix1[i][j]);
}
}
// reading the elements of second matrix
printf(" please enter the elements of second matrix ");
for( i=0; i<r2; i++){
for(j=0;j<c2;j++){
scanf( "%d", &matrix2[i][j]);
}
}
// multiplication two matrices`and store the result in matrix3
for( i=0; i<r1; i++){
for (j=0; j<c2; j++){
res_matrix[i][j]=0;
for(k=0;k<c1;k++){
res_matrix[i][j]= res_matrix[i][j] + matrix1[i][k] + matrix2[k][j];
}
}
// printing the resultant matrix
printf(" the resultant matrix is ");
for (i=0; i<r1; i++){
for( j=0; j<c2; j++){
printf(" %d ", res_matrix[i][j]);
}
printf(" \n ");
}
}
else printf(" it is not possible to multiply two matrix ");
}
case1: (condition satisfies)
output1
enter the size of first matrix
2
2
enter the size of second matrix
2
2
please provide / enter first matrix elements / values to proceed further
1
2
3
4
enter elements of second matrix
2
4
6
8
the resultant matrix is
14 20
30 44
case2: (condition not satisfied):
output.
enter the size of first matrix
2
5
enter the size of second matrix
2
2
it is not possible to multiply two matrix
In order to perform matrix multiplication we took two matrices by name matrix1 and matrix2 which hold the values of two matrices.
And to store the result after multiplication of matrix1 and matrix2 we took another matrix by name res_matrix .
r1,c1 represents the size of first matrix and r2,c2 represents the size of second matrix.
Condition for multiplication of two matrices is specified below.
In order to multiply two matrices first we have to check the condition.
If condition satisfies then only we can multiply two matrices else multiplication not possible.
The condition is number of columns in first matrix should be equal to number of rows in second matrix i.e; c1= = r2.
And after multiplication the size of resultant matrix will be r1,c2.
So first read sizes of two matrix and then check the condition for multiplication.
In this we took two matrices by name matrix1 and matrix2 which hold the values of two matrices.
And we took another matrix by name res_matrix to store the result after multiplication of matrix1 and matrix2.
#include <stdio.h>
void main(){
int matrix1[10][10], matrix2[10][10], res_matrix[10][10], i, j, k;
// reading the size of first matrix
printf(" enter the size of first matrix < 10 ");
scanf("%d%d", &r1, &c1) ;
// reading the size of second matrix
printf(" enter the size of second matrix < 10 ");
scanf("%d%d", &r2, &c2) ;
// sizes of two matrices has to be checked before multiplication of two matrices.
//checking the sizes of two matrices for multiplication.
//the cond. for multiplication of matrix is number of columns in first matrix should be
// equal to number of rows in second matrix i.e; c1==r2.
if( c1==r2 ){
// reading the elements/ values of first matrix
printf( " \n please specify /enter /provide the values of matrix (first)");
for( i=0; i<r1 ;i++){
for(j=0;j<c1;j++){
scanf( "%d", &matrix1[i][j]);
}
}
// reading the elements of second matrix
printf(" please enter the elements of second matrix ");
for( i=0; i<r2; i++){
for(j=0;j<c2;j++){
scanf( "%d", &matrix2[i][j]);
}
}
// multiplication two matrices`and store the result in matrix3
for( i=0; i<r1; i++){
for (j=0; j<c2; j++){
res_matrix[i][j]=0;
for(k=0;k<c1;k++){
res_matrix[i][j]= res_matrix[i][j] + matrix1[i][k] + matrix2[k][j];
}
}
// printing the resultant matrix
printf(" the resultant matrix is ");
for (i=0; i<r1; i++){
for( j=0; j<c2; j++){
printf(" %d ", res_matrix[i][j]);
}
printf(" \n ");
}
}
else printf(" it is not possible to multiply two matrix ");
}
case1: (condition satisfies)
output1
enter the size of first matrix
2
2
enter the size of second matrix
2
2
please provide / enter first matrix elements / values to proceed further
1
2
3
4
enter elements of second matrix
2
4
6
8
the resultant matrix is
14 20
30 44
case2: (condition not satisfied):
output.
enter the size of first matrix
2
5
enter the size of second matrix
2
2
it is not possible to multiply two matrix
No comments:
Post a Comment