Professional Documents
Culture Documents
CS110 Lab 6me54
CS110 Lab 6me54
Question 1:
Algorithm:
1.start
2.read the no.of rows(m),no.of columns(n)
3.input the elements of matrix
4.display the input matrix
5. b[j][i]=a[i][j] is the expression for evaluation of transpose where b[i][j] is
transpose matrix
6.display b[i][j],the required output
7.stop
Source code:
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],i,j,m,n;
printf("enter the order of matrix\n");
scanf("%d%d" ,&m ,&n);
printf("enter the elements of matrix\n");
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
scanf("%d",&a[i][j]);
}
}
printf("the matrix is\n");
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
b[j][i]=a[i][j];
}
}
printf("transpose of matrix is\n");
for(i=0;i<n;++i)
{
for(j=0;j<m;++j)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
return 0;
}
Question2:
Algorithm:
1.start
2.read the order of matrix(rxr)
3.input the elements of matrix
4.if i,j are positions of elements and j>=i then display corresponding elements
5.else display nothing
6.stop
Source code:
#include<stdio.h>
int main()
{
int a[10][10],i,j,r;
printf("enter the order of matrix\n");
scanf("%d",&r);
printf("enter the elements\n");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
if(j>=i)
{
printf("%d\t",a[i][j]);
}
else
{
printf("\t");
}
}
printf("\n");
}
return 0;
}
Question 3:
Algorithm:
1.start
2.read the order of matrix(rxr)
3.input the elements of matrix
4.if i,j are positions of elements and j<=i then display corresponding elements
5.else display 0
6.stop
Source code:
#include<stdio.h>
int main()
{
int a[10][10],i,j,r;
printf("enter the order of matrix\n");
scanf("%d",&r);
printf("enter the elements\n");
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
scanf("%d",&a[i][j]);
}
}
for(i=0;i<r;i++)
{
for(j=0;j<r;j++)
{
if(j<=i)
{
printf("%d\t",a[i][j]);
}
else
{
printf("0\t");
}
}
printf("\n");
}
return 0;
}
Question4:
Algorithm:
1.start
2.read the order mxn
3.input the elements of matrices a,b
{
for(j=0;j<n;++j)
{
sum[i][j] = a[i][j] + b[i][j];
printf("%d\t",sum[i][j]);
}
printf("\n");
}
return 0;
}
Question5:
Algorithm:
1.start
2.read the order for matrices a(r1xc1),b(r2xc2)
3.re-read the order if c1 is not equal to r2
4.input the elements of respective matrices
5.display the matrices
}
printf("the matrix a is\n");
for(i=0;i<r1;++i)
{
for(j=0;j<c1;++j)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("enter the elements of matrix b\n");
for(i=0;i<r2;++i)
{
for(j=0;j<c2;++j)
{
scanf("%d",&b[i][j]);
}
}
printf("the matrix b is\n");
for(i=0;i<r2;++i)
{
for(j=0;j<c2;++j)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
for(i=0; i<r1; ++i)
{
for(j=0; j<c2; ++j)
{
m[i][j]=0;
}
}
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
for(k=0; k<c1; ++k)
{
m[i][j]+=a[i][k]*b[k][j];
}
printf("multiplication of a and b is m\n");
for(i=0; i<r1; ++i)
for(j=0; j<c2; ++j)
{
printf("%d\t",m[i][j]);
if(j==c2-1)
printf("\n\n");
}
return 0;
}
Question6:
Algorithm:
1.start
2.read the elements of matrix
3.display the matrix
}
printf("\n");
}
for(i=0;i<3;i++)
{
determinant = determinant + (a[0][i]*(a[1][(i+1)%3]*
a[2][(i+2)%3] - a[1][(i+2)%3]*a[2][(i+1)%3]));
}
if(determinant==0)
{
printf("Inverse does not exist\n");
}
else
{
printf("\nInverse of matrix is \n");
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%f\t",((a[(i+1)%3][(j+1)%3] *
a[(i+2)%3][(j+2)%3]) - (a[(i+1)%3][(j+2)%3]*
a[(i+2)%3][(j+1)%3]))/ determinant);
}
printf("\n");
}
getch();
}