C LANGUAGE BOOK |
lets take an example.
In matrix.multiplication.
we have the matrix
first matrix have m row and n column
second matrix have n row and p column
when we multiply
third matrix should have m row and p column
((first matrix)m*n )*((second matrix)n*p )
n gets cancel out.
transpose
1 2 3
4 2 1
5 2 1
the transpose of this matrix is
1 4 5
2 2 2
3 1 1
/*
WAP TO PERFORM OPERATION LIKE ADDITION,SUBTRACTION,MULTIPLICATION,TRANSPOSE. ON MATRIX
*/
#include<stdio.h>
int main()
{
int i,j,q;
int a[2][2], b[2][2], c[2][2];
/* Enter the contents of first matrix */
printf("\n\nEnter The Element For First Matrix:\n");
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
scanf("\n%d", &a[i][j]);
}
/* Enter the contents of second matrix */
printf("\n\n\nEnter The Element For Second Matrix:\n");
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
scanf("\n%d", &b[i][j]);
}
/* Display the contents of first matrix */
printf("\n\nThe First Matrix Are\n");
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{ disp(a[i][j]);
}
printf("\n");
}
/* display the contents of second matrix */
printf("\n\nThe Second Matrix Are\n");
for(i = 0;i < 2;i++)
{
for(j = 0; j < 2; j++)
{
disp(b[i][j]);
}
printf("\n");
}
/* Addition of two matrix */
printf("\nThe Addition Of Two Matrix Are\n");
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
add(a[i][j], b[i][j], c[i][j]);
}
printf("\n");
}
printf("\nThe Subtraction Of Two Matrix Are\n");
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
sub(a[i][j], b[i][j], c[i][j]);
}
printf("\n");
}
printf("\nThe Multiplication Of Two Matrix Are\n");
for(i = 0; i < 2; i++)
{
for(j = 0; j < 2; j++)
{
c[i][j]=0;
for(k = 0; k < 2; k++)
{
mul(a[i][k], b[k][j], c[i][j]);
}
printf("\n");
}
}
printf ( "\n\nThe Transpose Of First Matrix Are :\n" ) ;
for ( q = 0 ; q < 2 ; q++ )
{
for ( i = 0 ; i < 2 ; i++ )
c[q][i] = a[i][q];
}
printf ( "\n" ) ;
for ( q = 0 ; q < 2 ; q++ )
{
for ( i = 0 ; i < 2 ; i++ )
{ disp ( c[q][i] ) ;
}
printf ( "\n" ) ;
}
printf ( "\n\nThe Transpose Of Second Matrix Are :\n" ) ;
for ( q = 0 ; q < 2 ; q++ )
{
for ( i = 0 ; i < 2 ; i++ )
c[q][i] = b[i][q];
}
printf ( "\n" ) ;
for ( q = 0 ; q < 2 ; q++ )
{
for ( i = 0 ; i < 2 ; i++ )
{ disp ( c[q][i] ) ;
}
printf ( "\n" ) ;
}
getch();
}
disp(int h)
{
printf("%d%s", h, " ");
}
add(int r, int g, int f)
{
f = r + g;
printf("%d%s", f, " ");
}
sub(int r, int g, int f)
{
f = r - g;
printf("%d%s", f, " ");
}
mul(int r, int g, int f)
{
f = f+ (r * g);
printf("%d%s", f, " ");
}
C LANGUAGE BOOK |
No comments:
Post a Comment