1. Write a program in C for multiplication of two square Matrices.
SOURCE CODE:
#include<stdio.h>
int main(){
int mtrx1[20][20],mtrx2[20][20],mulmtrx[40][40],n1,n2,i,j,k;
printf("Enter matrix sizes:");
scanf("%d%d",&n1,&n2);
if(n1==n2){
printf("Enter first matrix elements\n");
for(i=0;i<n1;i++)
for(j=0;j<n1;j++)
scanf("%d",&mtrx1[i][j]);
printf("Enter second matrix elements\n");
for(i=0;i<n2;i++)
for(j=0;j<n2;j++)
scanf("%d",&mtrx2[i][j]);
for(i=0;i<n1;i++){
for(j=0;j<n2;j++){
mulmtrx[i][j]=0;
for(k=0;k<n1;k++)
mulmtrx[i][j]=mulmtrx[i][j]+mtrx1[i][k]*mtrx2[k][j];
}
}
printf("\nResult matrix is\n");
for(i=0;i<n1;i++){
for(j=0;j<n1;j++)
printf("%d\t",mulmtrx[i][j]);
printf("\n");
}
}
else
printf("Matrix multiplication is not possible");
return 0;
}
OUTPUT-1:
Enter matrix sizes:3 3
Enter first matrix elements
1 2 3
4 5 6
7 8 9
Enter second matrix elements
9 8 7
6 5 4
3 2 1
Result matrix is
30 24 18
84 69 54
138 114 90
OUTPUT-2:
Enter matrix sizes:3 2
Matrix multiplication is not possible
2. Write a program in C to find transpose of a given matrix.
SOURCE CODE:
#include<stdio.h>
int main(){
int mtrx[10][10],r,c,i,j,temp;
printf("Enter matrix size:\n");
scanf("%d %d",&r,&c);
printf("Enter matrix elements:\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&mtrx[i][j]);
if(r>=c){
for(i=0;i<c;i++){
for(j=0;j<r;j++){
if(j>i){
temp = mtrx[i][j];
mtrx[i][j] = mtrx[j][i];
mtrx[j][i] = temp;
}
}
}
}
else{
for(i=0;i<r;i++){
for(j=0;j<c;j++){
if(j>i){
temp = mtrx[i][j];
mtrx[i][j] = mtrx[j][i];
mtrx[j][i] = temp;
}
}
}
}
printf("Transpose of the given matrix is\n");
for(i=0;i<c;i++){
for(j=0;j<r;j++)
printf("%d ",mtrx[i][j]);
printf("\n");
}
return 0;
}
OUTPUT-1:
Enter matrix size:
2 3
Enter matrix elements:
1 2 3
4 5 6
Transpose of the given matrix is
1 4
2 5
3 6
OUTPUT-2:
Enter matrix size:
3 2
Enter matrix elements:
1 2
3 4
5 6
Transpose of the given matrix is
1 3 5
2 4 6
SOURCE CODE:
#include<stdio.h>
int main(){
int mtrx1[20][20],mtrx2[20][20],mulmtrx[40][40],n1,n2,i,j,k;
printf("Enter matrix sizes:");
scanf("%d%d",&n1,&n2);
if(n1==n2){
printf("Enter first matrix elements\n");
for(i=0;i<n1;i++)
for(j=0;j<n1;j++)
scanf("%d",&mtrx1[i][j]);
printf("Enter second matrix elements\n");
for(i=0;i<n2;i++)
for(j=0;j<n2;j++)
scanf("%d",&mtrx2[i][j]);
for(i=0;i<n1;i++){
for(j=0;j<n2;j++){
mulmtrx[i][j]=0;
for(k=0;k<n1;k++)
mulmtrx[i][j]=mulmtrx[i][j]+mtrx1[i][k]*mtrx2[k][j];
}
}
printf("\nResult matrix is\n");
for(i=0;i<n1;i++){
for(j=0;j<n1;j++)
printf("%d\t",mulmtrx[i][j]);
printf("\n");
}
}
else
printf("Matrix multiplication is not possible");
return 0;
}
OUTPUT-1:
Enter matrix sizes:3 3
Enter first matrix elements
1 2 3
4 5 6
7 8 9
Enter second matrix elements
9 8 7
6 5 4
3 2 1
Result matrix is
30 24 18
84 69 54
138 114 90
OUTPUT-2:
Enter matrix sizes:3 2
Matrix multiplication is not possible
2. Write a program in C to find transpose of a given matrix.
SOURCE CODE:
#include<stdio.h>
int main(){
int mtrx[10][10],r,c,i,j,temp;
printf("Enter matrix size:\n");
scanf("%d %d",&r,&c);
printf("Enter matrix elements:\n");
for(i=0;i<r;i++)
for(j=0;j<c;j++)
scanf("%d",&mtrx[i][j]);
if(r>=c){
for(i=0;i<c;i++){
for(j=0;j<r;j++){
if(j>i){
temp = mtrx[i][j];
mtrx[i][j] = mtrx[j][i];
mtrx[j][i] = temp;
}
}
}
}
else{
for(i=0;i<r;i++){
for(j=0;j<c;j++){
if(j>i){
temp = mtrx[i][j];
mtrx[i][j] = mtrx[j][i];
mtrx[j][i] = temp;
}
}
}
}
printf("Transpose of the given matrix is\n");
for(i=0;i<c;i++){
for(j=0;j<r;j++)
printf("%d ",mtrx[i][j]);
printf("\n");
}
return 0;
}
OUTPUT-1:
Enter matrix size:
2 3
Enter matrix elements:
1 2 3
4 5 6
Transpose of the given matrix is
1 4
2 5
3 6
OUTPUT-2:
Enter matrix size:
3 2
Enter matrix elements:
1 2
3 4
5 6
Transpose of the given matrix is
1 3 5
2 4 6
No comments:
Post a Comment