/*Program to add, subtract and multiply two matrices*/
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100][100],b[100][100],d[100][100],i,j,r,c,n,k;
clrscr();
printf("Enter the no. of rows: ");
scanf("%d",&r);
printf("\nEnter the no. of cols: ");
scanf("%d",&c);
printf("\nEnter A Matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter the element %d %d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\nEnter B Matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter the element %d %d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
printf("\n=====================\n");
printf("\nA Matrix\n");
printf("\n=====================\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\n=====================\n");
printf("\nB Matrix\n");
printf("\n=====================\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\n*******MENU*******\n");
printf("\n1. Add two matrices");
printf("\n2. Subtract two matrices");
printf("\n3. Multiply two matrices");
printf("\nEnter the choice: \n");
scanf("%d",&n);
switch(n)
{
case 1:
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
d[i][j]=a[i][j]+b[i][j];
}
}
printf("\n=====================\n");
printf("\nC Matrix\n");
printf("\n=====================\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",d[i][j]);
}
printf("\n");
}
break;
case 2:
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
d[i][j]=a[i][j]-b[i][j];
}
}
printf("\n=====================\n");
printf("\nC Matrix\n");
printf("\n=====================\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",d[i][j]);
}
printf("\n");
}
break;
case 3:
for(i=0;i<r;i++)
{
for(k=0;k<r;k++)
{
d[i][k]=0;
for(j=0;j<c;j++)
{
d[i][k]+=a[i][j]*b[j][k];
}
}
}
printf("\n=====================\n");
printf("\nC Matrix\n");
printf("\n=====================\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",d[i][j]);
}
printf("\n");
}
break;
default:
printf("\nInvalid Number");
break;
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a[100][100],b[100][100],d[100][100],i,j,r,c,n,k;
clrscr();
printf("Enter the no. of rows: ");
scanf("%d",&r);
printf("\nEnter the no. of cols: ");
scanf("%d",&c);
printf("\nEnter A Matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter the element %d %d: ",i+1,j+1);
scanf("%d",&a[i][j]);
}
}
printf("\nEnter B Matrix\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("Enter the element %d %d: ",i+1,j+1);
scanf("%d",&b[i][j]);
}
}
printf("\n=====================\n");
printf("\nA Matrix\n");
printf("\n=====================\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("\n=====================\n");
printf("\nB Matrix\n");
printf("\n=====================\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\n*******MENU*******\n");
printf("\n1. Add two matrices");
printf("\n2. Subtract two matrices");
printf("\n3. Multiply two matrices");
printf("\nEnter the choice: \n");
scanf("%d",&n);
switch(n)
{
case 1:
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
d[i][j]=a[i][j]+b[i][j];
}
}
printf("\n=====================\n");
printf("\nC Matrix\n");
printf("\n=====================\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",d[i][j]);
}
printf("\n");
}
break;
case 2:
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
d[i][j]=a[i][j]-b[i][j];
}
}
printf("\n=====================\n");
printf("\nC Matrix\n");
printf("\n=====================\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",d[i][j]);
}
printf("\n");
}
break;
case 3:
for(i=0;i<r;i++)
{
for(k=0;k<r;k++)
{
d[i][k]=0;
for(j=0;j<c;j++)
{
d[i][k]+=a[i][j]*b[j][k];
}
}
}
printf("\n=====================\n");
printf("\nC Matrix\n");
printf("\n=====================\n");
for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%d\t",d[i][j]);
}
printf("\n");
}
break;
default:
printf("\nInvalid Number");
break;
}
getch();
}
No comments:
Post a Comment