Description:
program takes the two matrixes of
same size and performs the addition an also takes the two matrixes of different
sizes and checks for possibility of multiplication and perform multiplication
if possible.
algorithm:
Step 1: start
Step 2: read the size of matrices A,B
– m,n
Step 3: read the elements of matrix
A
Step 4: read the elements of matrix
B
Step 5: select the choice for you
want. If you select case 1 then goto matric
addition. Else goto Step 7.
Step 6: print Sum of matrix A and B
Step 7: if you select case 2 then goto matrix
multiplication
Step 8: check if n=p, if not print
matrices can not be multiplied
Step 9: Otherwise perform the
multiplication of matrices
Step 10: Print the resultant matrix
Step 11: Stop
Program:
#include<stdio.h>
void main()
{
int ch,i,j,m,n,p,q,k,r1,c1,a[10][10],b[10][10],c[10][10];
clrscr();
printf("************************************");
printf("\n\t\tMENU");
printf("\n**********************************");
printf("\n[1]ADDITION OF TWO
MATRICES");
printf("\n[2]MULTIPLICATION OF
TWO MATRICES");
printf("\n[0]EXIT");
printf("\n**********************************");
printf("\n\tEnter your
choice:\n");
scanf("%d",&ch);
if(ch<=2 & ch>0)
{
printf("Valid Choice\n");
}
switch(ch)
{
case 1:
printf("Input rows
and columns of A & B Matrix:");
scanf("%d%d",&r1,&c1);
printf("Enter elements of matrix
A:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&a[i][j]);
}
printf("Enter elements of matrix
B:\n");
for(i=0;i<r1;i++)
{
for(j=0;j<c1;j++)
scanf("%d",&b[i][j]);
}
printf("\n =====Matrix
Addition=====\n");
for(i=0;i<r1;i++)
{
For(j=0;j<c1;j++)
printf("%5d",a[i][j]+b[i][j]);
printf("\n");
}
break;
case 2:
printf("Input rows and columns of
A matrix:");
scanf("%d%d",&m,&n);
printf("Input rows and columns of
B matrix:");
scanf("%d%d",&p,&q);
if(n==p)
{
printf("matrices can be
multiplied\n");
printf("resultant matrix is
%d*%d\n",m,q);
printf("Input A matrix\n");
read_matrix(a,m,n);
printf("Input B
matrix\n");
/*Function call to read the matrix*/
read_matrix(b,p,q);
/*Function for Multiplication of two
matrices*/
printf("\n =====Matrix
Multiplication=====\n");
for(i=0;i<m;++i)
for(j=0;j<q;++j)
{
c[i][j]=0;
for(k=0;k<n;++k)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("Resultant of
two matrices:\n");
write_matrix(c,m,q);
}
/*end if*/
else
{
printf("Matrices cannot be
multiplied.");
}
/*end else*/
break;
case 0:
printf("\n Choice Terminated");
exit();
break;
default:
printf("\n Invalid Choice");
}
getch();
}
/*Function read matrix*/
int read_matrix(int a[10][10],int
m,int n)
{
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
return 0;
}
/*Function to write the matrix*/
int write_matrix(int a[10][10],int
m,int n)
{
int i,j;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%5d",a[i][j]);
printf("\n");
}
return 0;
}
Output:
1.
************************************
MENU
**********************************
[1]ADDITION OF TWO MATRICES
[2]MULTIPLICATION OF TWO MATRICES
[0]EXIT
**********************************
Enter your
choice:
1
Valid Choice
Input rows and columns of A & B Matrix:2
2
Enter elements of matrix A:
2
2
2
2
Enter elements of matrix B:
2
2
2
2
=====Matrix
Addition=====
4 4
4 4
************************************
MENU
**********************************
[1]ADDITION OF TWO MATRICES
[2]MULTIPLICATION OF TWO MATRICES
[0]EXIT
**********************************
Enter your
choice:
2
Valid Choice
Input rows and columns of A matrix:2
3
Input rows and columns of B matrix:2
2
Matrices cannot be multiplied.
************************************
MENU
**********************************
[1]ADDITION OF TWO MATRICES
[2]MULTIPLICATION OF TWO MATRICES
[0]EXIT
**********************************
Enter your
choice:
2
Valid Choice
Input rows and columns of A matrix:2
2
Input rows and columns of B matrix:2
2
matrices can be multiplied
resultant matrix is 2*2
Input A matrix
2
2
2
2
Input B matrix
2
2
2
2
=====Matrix Multiplication=====
Resultant of two matrices:
8 8
8 8
Conclusion :
The program is error free
VIVA QUESATIONS:
1) What
is condition for performing an matric addition ?
Ans: program takes the two
matrixes of same size and performs the addition
2) What
is condition for performing an matric addition ?
Ans: The two matrixes of different
sizes and checks for possibility of multiplication and perform multiplication
if possible
No comments:
Post a Comment