/*------------------------------------------------------------------------------------------
Program : Write a program to perform operations on
matrices like
addition, multiplication,saddle
point, magic square,
inverse & transpose etc using
functions &pointers.
[Minimum 4 operations]
Created
By : JABIR DAUD PATHAN
Branch :
COMPUTER ENGINEERING
------------------------------------------------------------------------------------------*/
/*--------------------<< Header File
>>-----------------------*/
#include<stdio.h>
#include<conio.h>
/*---------<<
Global Declaration of Functions
>>---------------*/
void
accept1(void);
void
accept2(void);
void
addmat(void);
void
mulmat(void);
void
saddlepoint(void);
void
magicsquare(void);
void
transpose(void);
void
inverse(void);
int
a[3][3],b[3][3],c[3][3],i,j,dtr;//Global Declaration of variables
/*---<<
Function Definition to accept the 1st matrix element >>---*/
void
accept1()
{
printf("\nEnter the Elements of 1st
Matrix==>\n");
for(i=0;i<=2;i++) //count the no of rows
{
for(j=0;j<=2;j++) //count the no of columns
{
scanf("%d",&a[i][j]); //accept elements for 1st matrix
}
}
printf("\naccepted elements of 1st
Matrix are==>\n\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\ta[%d][%d]=%d",i,j,a[i][j]);
}
printf("\n\n");
}
}
/*----<<Function
Definition to accept the 2nd matrix element>>----*/
void
accept2()
{
printf("\nEnter the Elements of 2nd
Matrix==>\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
scanf("%d",&b[i][j]);
//accept elements for 2nd matrix
}
}
printf("\naccepted elements of 2nd
Matrix are==>\n\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\tb[%d][%d]=%d",i,j,b[i][j]);
}
printf("\n\n");
}
}
/*------<<
Function Definition for Addition of Matrix >>-------*/
void
addmat()
{
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
c[i][j]=a[i][j]+b[i][j]; //formula for matrix addition
}
}
printf("\nAddition of Two
Matrix==>\n\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\tc[%d][%d]=%d",i,j,c[i][j]);
//display addition
}
printf("\n\n");
}
}
/*-----<<
Function Definition for Multiplication of Matrix >>-----*/
void
mulmat()
{
int k,sum=0;
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
sum=0;
for(k=0;k<=2;k++)
{
sum=sum+a[i][k]*b[k][j]; //Matrix Multiplication Formula
}
c[i][j]=sum;
}
}
printf("\nMultiplication of Two
Matrix==>\n\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\tc[%d][%d]=%d",i,j,c[i][j]);//Display
Multiplication
}
printf("\n\n");
}
}
/*-----<<
Function Definition for Saddle point of Matrix >>------*/
void
saddlepoint()
{
int k,min,max,col;
for(i=0;i<3;i++)
{
min=a[i][0];
for(j=0;j<3;j++)
{
if(a[i][j]<=min)
{
min=a[i][j];
col=j;
}
}
max=a[0][col];
for(k=0;k<3;k++)
{
if(a[k][col]>=max)
{
max=a[k][col];
}
}
if(max==min)
printf("saddle pt.at
a[%d][%d]=%d",i,col,max);
//display
the saddle point
}
}
/*------<<
Function Definition for Magic square of Matrix >>-----*/
void
magicsquare()
{
int x=0,y=0,z=0,p=0,q=0,r=0,s=0,t=0;
x=a[0][0]+a[0][1]+a[0][2];
y=a[1][0]+a[1][1]+a[1][2];
z=a[2][0]+a[2][1]+a[2][2];
p=a[0][0]+a[1][0]+a[2][0];
q=a[0][1]+a[1][1]+a[2][1];
r=a[0][2]+a[1][2]+a[2][2];
s=a[0][0]+a[1][1]+a[2][2];
t=a[0][2]+a[1][1]+a[2][0];
if(x==y && y==z && z==p
&& p==q && q==r && r==s && s==t)
{
printf("\nEnter matrix is magic
square");
}
else
{
printf("\nEnter matrix is not magic
square");
}
}
/*-----<<
Function Definition for Transpose of Matrix >>-------*/
void
transpose()
{
printf("\nTranspose of the
matrix==>\n\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\ta[%d][%d]=%d",i,j,a[j][i]); //display the transpose
}
printf("\n\n");
}
}
/*-------<<
Function Definition for Inverse of Matrix >>--------*/
void
inverse()
{
dtr=
(a[0][0]*((a[1][1]*a[2][2])-(a[1][2]*a[2][1])))
-(a[0][1]*((a[1][0]*a[2][2])-(a[1][2]*a[2][0])))
+(a[0][2]*((a[1][0]*a[2][1])-(a[1][1]*a[2][0])));
//formula for calculate determinant
printf("\nthe determinant of the matrix
is==>%d",dtr);
//dispaly
the determinant
b[0][0]=(a[1][1]*a[2][2])-(a[2][1]*a[1][2]); //
b[0][1]=-((a[1][0]*a[2][2])-(a[2][0]*a[1][2])); // [ Formula
b[0][2]=(a[1][0]*a[2][1])-(a[2][0]*a[1][1]); //
for
b[1][0]=-((a[0][1]*a[2][2])-(a[2][1]*a[0][2])); //
calculate
b[1][1]=(a[0][0]*a[2][2])-(a[2][0]*a[0][2]); //
adjoint
b[1][2]=-((a[0][0]*a[2][1])-(a[2][0]*a[0][1])); // of
b[2][0]=(a[0][1]*a[1][2])-(a[1][1]*a[0][2]); //
matrix ]
b[2][1]=-((a[0][0]*a[1][2])-(a[1][0]*a[0][2])); //
b[2][2]=(a[0][0]*a[1][1])-(a[1][0]*a[0][1]); //
printf("\n\nadjoint of the matrix
is==>\n\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\tb[%d][%d]=%d",i,j,b[j][i]);
//display
adjoint of matrix
}
printf("\n\n");
}
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
c[i][j]=b[j][i]/dtr; //formula to find
inverse of matrix
}
}
printf("inverse of matrix
is==>\n\n");
for(i=0;i<=2;i++)
{
for(j=0;j<=2;j++)
{
printf("\tc[%d][%d]=%d",i,j,c[i][j]); //display the inverse
}
printf("\n\n");
}
}
/*---------------<< Main Function Starts >>--------------------*/
void
main()
{
int ch;
char ans;
clrscr(); //Clear the text mode of window
do
{
printf("\n\t\t--------<< MENU
>>--------");
printf("\n\n\t\t1.Addition of
Matrix");
printf("\n\t\t2.Multiplication of
Matrix");
printf("\n\t\t3.Saddle point of
Matrix");
printf("\n\t\t4.Magic square of
Matrix");
printf("\n\t\t5.Transpose of
Matrix");
printf("\n\t\t6.Inverse of
Matrix");
printf("\n\t\t7.Exit");
printf("\n\n\t\t----------------------------");
printf("\n\n\t\tEnter Your
Choice==>");
scanf("%d",&ch); //accept choice from user
switch(ch)
{
case 1:
accept1(); //function call
accept2(); //function call
addmat();
//function call
break;
case 2:
accept1();
accept2();
mulmat();
break;
case 3:
accept1();
saddlepoint();
break;
case 4:
accept1();
magicsquare();
break;
case 5:
accept1();
transpose();
break;
case 6:
accept1();
inverse();
break;
case 7:
exit(0);
break;
default:
printf("\nInvalid Choice");
break;
}
printf("\n\n\t\t Do you Want to
Continue(Y/N)?==>");
ans=getche();
clrscr();
}while(ans=='y'||ans=='Y');
getch();
}
/*----------------<< End Of Main Function >>--------------------*/
/*-----------------<< OUTPUT
SCREEN >>-------------------*/
--------<< MENU
>>--------
1.Addition of Matrix
2.Multiplication of Matrix
3.Saddle point of Matrix
4.Magic square of Matrix
5.Transpose of Matrix
6.Inverse of Matrix
7.Exit
----------------------------
Enter Your Choice==>1
Enter
the Elements of 1st Matrix==>
1 2
3 4 5 6 7 8 9
accepted
elements of 1st Matrix are==>
a[0][0]=1 a[0][1]=2 a[0][2]=3
a[1][0]=4 a[1][1]=5 a[1][2]=6
a[2][0]=7 a[2][1]=8 a[2][2]=9
Enter
the Elements of 2nd Matrix==>
1 2
3 4 5 6 7 8 9
accepted
elements of 2nd Matrix are==>
b[0][0]=1 b[0][1]=2 b[0][2]=3
b[1][0]=4 b[1][1]=5 b[1][2]=6
b[2][0]=7 b[2][1]=8 b[2][2]=9
Addition
of Two Matrix==>
c[0][0]=2 c[0][1]=4 c[0][2]=6
c[1][0]=8 c[1][1]=10 c[1][2]=12
c[2][0]=14 c[2][1]=16 c[2][2]=18
Do you Want to
Continue(Y/N)?==>Y
--------<< MENU
>>--------
1.Addition of Matrix
2.Multiplication of Matrix
3.Saddle point of Matrix
4.Magic square of Matrix
5.Transpose of Matrix
6.Inverse of Matrix
7.Exit
----------------------------
Enter Your Choice==>2
Enter
the Elements of 1st Matrix==>
1 2
3 4 5 6 7 8 9
accepted
elements of 1st Matrix are==>
a[0][0]=1 a[0][1]=2 a[0][2]=3
a[1][0]=4 a[1][1]=5 a[1][2]=6
a[2][0]=7 a[2][1]=8 a[2][2]=9
Enter
the Elements of 2nd Matrix==>
1 2
3 4 5 6 7 8 9
accepted
elements of 2nd Matrix are==>
b[0][0]=1 b[0][1]=2 b[0][2]=3
b[1][0]=4 b[1][1]=5 b[1][2]=6
b[2][0]=7 b[2][1]=8 b[2][2]=9
Multiplication
of Two Matrix==>
c[0][0]=30 c[0][1]=36 c[0][2]=42
c[1][0]=66 c[1][1]=81 c[1][2]=96
c[2][0]=102 c[2][1]=126 c[2][2]=150
Do you Want to
Continue(Y/N)?==>Y
--------<< MENU
>>--------
1.Addition of Matrix
2.Multiplication of Matrix
3.Saddle point of Matrix
4.Magic square of Matrix
5.Transpose of Matrix
6.Inverse of Matrix
7.Exit
----------------------------
Enter Your Choice==>3
Enter
the Elements of 1st Matrix==>
1 2
3 4 5 6 7 8 9
accepted
elements of 1st Matrix are==>
a[0][0]=1 a[0][1]=2 a[0][2]=3
a[1][0]=4 a[1][1]=5 a[1][2]=6
a[2][0]=7 a[2][1]=8 a[2][2]=9
saddle
pt.at a[2][0]=7
Do you Want to
Continue(Y/N)?==>Y
--------<< MENU
>>--------
1.Addition of Matrix
2.Multiplication of Matrix
3.Saddle point of Matrix
4.Magic square of Matrix
5.Transpose of Matrix
6.Inverse of Matrix
7.Exit
----------------------------
Enter Your Choice==>4
Enter
the Elements of 1st Matrix==>
6 1
8 7 5 3 2 9 4
accepted
elements of 1st Matrix are==>
a[0][0]=6 a[0][1]=1 a[0][2]=8
a[1][0]=7 a[1][1]=5 a[1][2]=3
a[2][0]=2 a[2][1]=9 a[2][2]=4
Enter
matrix is magic square
Do you Want to
Continue(Y/N)?==>Y
--------<< MENU
>>--------
1.Addition of Matrix
2.Multiplication of Matrix
3.Saddle point of Matrix
4.Magic square of Matrix
5.Transpose of Matrix
6.Inverse of Matrix
7.Exit
----------------------------
Enter Your Choice==>5
Enter
the Elements of 1st Matrix==>
1 2
3 4 5 6 7 8 9
accepted
elements of 1st Matrix are==>
a[0][0]=1 a[0][1]=2 a[0][2]=3
a[1][0]=4 a[1][1]=5 a[1][2]=6
a[2][0]=7 a[2][1]=8 a[2][2]=9
Transpose
of the matrix==>
a[0][0]=1 a[0][1]=4 a[0][2]=7
a[1][0]=2 a[1][1]=5 a[1][2]=8
a[2][0]=3 a[2][1]=6 a[2][2]=9
Do you Want to
Continue(Y/N)?==>Y
--------<< MENU
>>--------
1.Addition of Matrix
2.Multiplication of Matrix
3.Saddle point of Matrix
4.Magic square of Matrix
5.Transpose of Matrix
6.Inverse of Matrix
7.Exit
----------------------------
Enter Your Choice==>6
Enter
the Elements of 1st Matrix==>
1 3
3 1 4 3 1 3 4
accepted
elements of 1st Matrix are==>
a[0][0]=1 a[0][1]=3 a[0][2]=3
a[1][0]=1 a[1][1]=4 a[1][2]=3
a[2][0]=1 a[2][1]=3 a[2][2]=4
the
determinant of the matrix is==>1
adjoint
of the matrix is==>
b[0][0]=7 b[0][1]=-3 b[0][2]=-3
b[1][0]=-1 b[1][1]=1 b[1][2]=0
b[2][0]=-1 b[2][1]=0 b[2][2]=1
inverse
of matrix is==>
c[0][0]=7 c[0][1]=-3 c[0][2]=-3
c[1][0]=-1 c[1][1]=1 c[1][2]=0
c[2][0]=-1 c[2][1]=0 c[2][2]=1
Do you Want to
Continue(Y/N)?==>Y
--------<< MENU
>>--------
1.Addition of Matrix
2.Multiplication of Matrix
3.Saddle point of Matrix
4.Magic square of Matrix
5.Transpose of Matrix
6.Inverse of Matrix
7.Exit
----------------------------
Enter Your Choice==>7
No comments:
Post a Comment