Practical-5 and Practical-6 - C Language Code
Practical-5 and Practical-6 - C Language Code
Aim: Addition, Subtraction, Transpose, Trace and Multiplication of a square matrix N*N.
Objective: Write a program to enter two nxn (square) matrices from user and calculate its sum,
difference, transpose, trace and product.
Two dimensional array take value from user using nested loop, where first loop represent rows
and second loop is for columns of matrix.
Sum: Sum of two matrix is calculated by adding each element of first matrix with
corresponding element of second matrix and sorting value in a new matrix.
Difference: Subtract each element of first matrix with corresponding element of first matrix
and sorting value in new matrix using nested loop.
Transpose: We store value of row elements of a matrix in a column of other new matrix
Product: If we consider
AXB =
A=
Trace = a + e + j
Code:
#include<stdio.h>
#include<conio.h>
main()
{
int a[3][3],b[3][3],c[3][3],d[3][3],transpose[3][3],mult[3][3]={0,0},trace;
int i,j,k,l,m,n,v,f,g,h,u;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter%d%d element of a:",i,j);
scanf("%d",&a[i][j]);
printf("Enter%d%d element of b:",i,j);
scanf("%d",&b[i][j]);
}
printf("\n");
}
printf("Addition\n");
for(k=0;k<3;k++)
{
for(m=0;m<3;m++)
{
c[k][m]=a[k][m]+b[k][m];
printf("%d\t",c[k][m]);
}
printf("\n");
}
printf("Subtraction\n");
for(l=0;l<3;l++)
{
for(n=0;n<3;n++)
{
d[l][n]=a[l][n]-b[l][n];
printf("%d\t",d[l][n]);
}
printf("\n");
}
printf("Transpose\n");
for(v=0;v<3;v++)
{
for(f=0;f<3;f++)
{
transpose[v][f]=a[f][v];
printf("%d\t",transpose[v][f]);
}
printf("\n");
}
trace=a[0][0]+a[1][1]+a[2][2];
printf("Trace=%d\n",trace);
printf("Multiplication:\n");
for(u=0;u<3;u++)
{
for(g=0;g<3;g++)
{
for(h=0;h<3;h++)
{
mult[g][u]=mult[g][u]+(a[g][h]*b[h][g]);
}
printf("%d\t",mult[g][u]);
}
printf("\n");
}
}
Output: paste output here
Practical-6
int a1[10][10],a2[10][10],c[10][10],i,j,k,a,b,x,p,q;
printf("Enter the size of 1st matrix\n");
scanf("%d %d",&a,&b);
printf("Enter the size of 2st matrix\n");
scanf("%d %d",&p,&q);
for(j=0;j<b;j++)
{
scanf("%d",&a1[i][j]);
}
}
printf("Enter the element of 2nd matrix\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&a2[i][j]);
}
}
for(i=0;i<a;i++)
{
printf("\n");
for(j=0;j<q;j++)
{
printf("%d ",c[i][j]);
}
}
}