Write a program to perform
various operation on matrices.
#include <stdio.h>
#include<conio.h>
#define MAX 3
int create ( int [3][3] ) ;
int display ( int [3][3] ) ;
int matadd ( int [3][3], int
[3][3], int [3][3] ) ;
int matsub ( int [3][3], int
[3][3], int [3][3] ) ;
int matmul ( int [3][3], int
[3][3], int [3][3] ) ;
int transpose ( int [3][3], int
[3][3] ) ;
int main( )
{
int mat1[3][3], mat2[3][3],
mat3[3][3], mat4[3][3], mat5[3][3] ;
clrscr();
printf ( "\nEnter elements
for first array: \n\n" ) ;
create ( mat1 ) ;
printf ( "\nEnter elements
for second array: \n\n" ) ;
create ( mat2 ) ;
printf ( "\nFirst Array:
\n" ) ;
display ( mat1 ) ;
printf ( "\nSecond
Array:\n" ) ;
display ( mat2 ) ;
matadd ( mat1, mat2, mat3 ) ;
printf ( "\nAfter Addition:
\n" ) ;
display ( mat3 ) ;
matsub ( mat1, mat2, mat3 ) ;
printf ( "\nAfter
Subtraction: \n" ) ;
display ( mat3 ) ;
matmul ( mat1, mat2, mat4 ) ;
printf ( "\nAfter
Multiplication: \n" ) ;
display ( mat4 ) ;
transpose ( mat1, mat5 ) ;
printf ( "\nTranspose of
first matrix: \n" ) ;
display ( mat5 ) ;
}
int create ( int mat[3][3] )
{
int i, j ;
for ( i = 0 ; i < MAX ; i++ )
{
for ( j = 0 ; j < MAX ; j++ )
{
printf ( "Enter the element:
" ) ;
scanf ( "%d",
&mat[i][j] ) ;
}
}
}
int display ( int mat[3][3] )
{
int i, j ;
for ( i = 0 ; i < MAX ; i++ )
{
for ( j = 0 ; j < MAX ; j++ )
printf ( "%d\t",
mat[i][j] ) ;
printf ( "\n" ) ;
}
}
int matadd ( int m1[3][3], int
m2[3][3], int m3[3][3] )
{
int i, j ;
for ( i = 0 ; i < MAX ; i++ )
{
for ( j = 0 ; j < MAX ; j++ )
m3[i][j] = m1[i][j] + m2[i][j] ;
}
}
int matsub ( int m1[3][3], int
m2[3][3], int m3[3][3] )
{
int i, j ;
for ( i = 0 ; i < MAX ; i++ )
{
for ( j = 0 ; j < MAX ; j++ )
m3[i][j] = m1[i][j] - m2[i][j] ;
}
}
int matmul ( int m1[3][3], int
m2[3][3], int m3[3][3] )
{
int i, j, k ;
for ( k = 0 ; k < MAX ; k++ )
{
for ( i = 0 ; i < MAX ; i++ )
{
m3[k][i] = 0 ;
for ( j = 0 ; j < MAX ; j++ )
m3[k][i] += m1[k][j] * m2[j][i] ;
}
}
}
int transpose ( int m1[3][3], int
m2[3][3] )
{
int i, j ;
for ( i = 0 ; i < MAX ; i++ )
{
for ( j = 0 ; j < MAX ; j++ )
m2[i][j] = m1[j][i] ;
}
}
Output