/*write a menu driven program to perform following operations on matrices*/
(1)addition.
(2)multiplication.
(3)transpose.
(4)subtraction.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int z,i,j,a[5][5],b[5][5],c[5][5],k,l,m,n,sum,o;
char ch;
clrscr();
do{
printf("chose any option\n1:addition\n2:multiplication\n3:transpose\n4:subtraction");
scanf("%d",&z);
switch(z)
{
case 1:
printf("enter row and column of 1st matrix\n");
scanf("%d %d",&k,&l);
printf("enter row and column of 2nd matrix\n");
scanf("%d %d",&m,&n);
printf("enter 1st matrix\n");
for(i=0;i<k;i++)
{
for(j=0;j<l;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter 2nd matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("addition is\n");
for(i=0;i<k;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d \t",c[i][j]);
}
printf("\n");
}
break;
case 2:
printf("enter row and column of 1st matrix\n");
scanf("%d %d",&k,&l);
printf("enter row and column of 2nd matrix\n");
scanf("%d %d",&m,&n);
printf("enter 1st matrix\n");
for(i=0;i<k;i++)
{
for(j=0;j<l;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter 2nd matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<k;i++)
{
for(j=0;j<m;j++)
{
c[i][j]=0;
for(o=0;o<n;o++)
{
c[i][j]=c[i][j]+(a[i][o]*b[o][j]);
}
}
}
for(i=0;i<k;i++)
{
for(j=0;j<n;j++)
{
printf("%d \t",c[i][j]);
}
printf("\n");
}
break;
case 3:
printf("enter row and column of matrix\n");
scanf("%d %d",&k,&l);
printf("enter the matrix\n");
for(i=0;i<l;i++)
{
for(j=0;j<k;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\ntranspose is:\n");
for(i=0;i<l;i++)
{
for(j=0;j<k;j++)
{
c[i][j]=a[j][i];
printf("%d \t",c[i][j]);
}
printf("\n");
}
break;
case 4:
printf("enter row and column of 1st matrix\n");
scanf("%d %d",&k,&l);
printf("enter row and column of 2nd matrix\n");
scanf("%d %d",&m,&n);
printf("enter 1st matrix\n");
for(i=0;i<k;i++)
{
for(j=0;j<l;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter 2nd matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("subtraction is\n");
for(i=0;i<k;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]-b[i][j];
printf("%d \t",c[i][j]);
}
printf("\n");
}
break;
default:
printf("wrong choice");
break;
}
printf("\ndo you want to continue y,n?");
scanf("%s",&ch);
ch=tolower(ch);
}
while(ch=='y');
getch();
}
(1)addition.
(2)multiplication.
(3)transpose.
(4)subtraction.*/
#include<stdio.h>
#include<conio.h>
void main()
{
int z,i,j,a[5][5],b[5][5],c[5][5],k,l,m,n,sum,o;
char ch;
clrscr();
do{
printf("chose any option\n1:addition\n2:multiplication\n3:transpose\n4:subtraction");
scanf("%d",&z);
switch(z)
{
case 1:
printf("enter row and column of 1st matrix\n");
scanf("%d %d",&k,&l);
printf("enter row and column of 2nd matrix\n");
scanf("%d %d",&m,&n);
printf("enter 1st matrix\n");
for(i=0;i<k;i++)
{
for(j=0;j<l;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter 2nd matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("addition is\n");
for(i=0;i<k;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]+b[i][j];
printf("%d \t",c[i][j]);
}
printf("\n");
}
break;
case 2:
printf("enter row and column of 1st matrix\n");
scanf("%d %d",&k,&l);
printf("enter row and column of 2nd matrix\n");
scanf("%d %d",&m,&n);
printf("enter 1st matrix\n");
for(i=0;i<k;i++)
{
for(j=0;j<l;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter 2nd matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
for(i=0;i<k;i++)
{
for(j=0;j<m;j++)
{
c[i][j]=0;
for(o=0;o<n;o++)
{
c[i][j]=c[i][j]+(a[i][o]*b[o][j]);
}
}
}
for(i=0;i<k;i++)
{
for(j=0;j<n;j++)
{
printf("%d \t",c[i][j]);
}
printf("\n");
}
break;
case 3:
printf("enter row and column of matrix\n");
scanf("%d %d",&k,&l);
printf("enter the matrix\n");
for(i=0;i<l;i++)
{
for(j=0;j<k;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("\ntranspose is:\n");
for(i=0;i<l;i++)
{
for(j=0;j<k;j++)
{
c[i][j]=a[j][i];
printf("%d \t",c[i][j]);
}
printf("\n");
}
break;
case 4:
printf("enter row and column of 1st matrix\n");
scanf("%d %d",&k,&l);
printf("enter row and column of 2nd matrix\n");
scanf("%d %d",&m,&n);
printf("enter 1st matrix\n");
for(i=0;i<k;i++)
{
for(j=0;j<l;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("enter 2nd matrix\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&b[i][j]);
}
}
printf("subtraction is\n");
for(i=0;i<k;i++)
{
for(j=0;j<n;j++)
{
c[i][j]=a[i][j]-b[i][j];
printf("%d \t",c[i][j]);
}
printf("\n");
}
break;
default:
printf("wrong choice");
break;
}
printf("\ndo you want to continue y,n?");
scanf("%s",&ch);
ch=tolower(ch);
}
while(ch=='y');
getch();
}
No comments:
Post a Comment