Sunteți pe pagina 1din 12

CS110 Lab 6:

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

4. sum[i][j] = a[i][j] + b[i][j] is required expression for sum


5.display sum[i][j]
6.stop
Source code:
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],sum[10][10],m,n,i,j;
printf("enter the number of rows and columns of matrix\n");
scanf("%d%d",&m,&n);
printf("enter the elements of first matrix\n");
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
scanf("%d",&a[i][j]);
}
}
printf("enter the elements of second matrix\n");
for(i=0;i<m;++i)
{
for(j=0;j<n;++j)
{
scanf("%d",&b[i][j]);
}
}
printf("the sum is\n");
for(i=0;i<m;++i)

{
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

6.apply the multiplication expression m[i][j]+=a[i][k]*b[k][j]


7.display the m[i][j]
8.stop
Source code:
#include<stdio.h>
int main()
{
int a[10][10],b[10][10],m[10][10],r1,c1,r2,c2,i,j,k;
printf("order of a is r1xc1,b is r2xc2");
printf("Enter rows and column for matrix a ");
scanf("%d%d",&r1,&c1);
printf("Enter rows and column for matrix b");
scanf("%d%d",&r2,&c2);
while (c1!=r2)
{
printf("Error because column of matrix a not equal to row of b\n\n");
printf("Enter rows and column for matrix a");
scanf("%d%d",&r1,&c1);
printf("Enter rows and column for matrix b ");
scanf("%d%d",&r2,&c2);
}
printf("enter the elements of matrix a\n");
for(i=0;i<r1;++i)
{
for(j=0;j<c1;++j)
{
scanf("%d",&a[i][j]);
}

}
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

4.define determinant expression as 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]))
5.if determinant=0 then display inverse doesnt exist
6.else display inverse of matrix as ((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)
7.stop
Source code:
#include<stdio.h>
#include<conio.h>
int main()
{
int a[3][3],i,j;
float determinant=0;
printf("Enter elements of 3x3 matrix:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\nThe entered matrix is:\n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",a[i][j]);

}
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();
}

S-ar putea să vă placă și