Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Name: MATRIX OPERATIONS PROGRAMME
- Author: A.S.Ram Narayanan
- Date: 10-05-15 13:47
- Description: this programme does
- equality checking
- addition
- multiplication
- subtraction
- transpose
- chexs for symmetry of matrix
- determinant
- adjoint
- inverse
- for 3 BY 3 ORDERED MATRIX
- */
- #include<iostream>
- #include<conio.h>
- using namespace std ;
- void inverse();
- void adjoint();
- void matsub();
- void add();
- void matmult();
- void trans();
- void sym();
- void det();
- int A[3][3],B[3][3],r,c,d,C[3][3],i,j,k,D[3][3],T[3][3],E[3][3],f,f1,adj[3][3],F[3][3],T1[3][3],adjB[3][3];
- float determinant=0,determinantB=0;
- int main()
- {
- cout<<"enter the first matrix row"<<endl;
- /*input of data in matrix */
- for(r=0;r<3;r++)
- {
- for(c=0;c<3;c++)
- {
- cin>>A[r][c];
- }
- }
- cout<<"enter the second matrix to check equality"<<endl;
- for(r=0;r<3;r++)
- {
- for(c=0;c<3;c++)
- {
- cin>>B[r][c];
- }
- }
- for(r=0;r<3;r++)//checking the matrix equaltiy
- {
- for(c=0;c<3;c++)
- {
- if(A[r][c]!=B[r][c])
- {
- d=0;
- }
- }//end of for loop(col)
- }//end of row for loop
- if(d!=0)
- cout<<"matrices are equal"<<endl;
- else
- cout<<"they arent equal"<<endl;
- /* choice for matrix addtition or subtraction or multiplication */
- int ch;
- cout<<"enter your choice"<<endl;
- cout<<"1.subtraction"<<"\n"<<"2.addition"<< "\n" <<"3.multiplication"<<endl<<"4.transpose[FOR A and B]"<<endl<<"5.symmetric checking[FOR A and B]"<<endl<<"6.determinant[FOR A and B]"<<endl;
- cout<<"7.inverse(FOR A & B)"<<endl<<"8.adjoint(FOR A&B)"<<endl;
- cin>>ch;
- if(ch==1)
- {
- matsub();//function call of matsub
- }
- else if(ch==2)
- {
- add();//function addtion call
- }
- else if(ch==3)
- {
- matmult();//function matmult call
- }
- else if(ch==4)
- trans();//call of transpose fumction
- else if(ch==5)
- sym();//call of sym function
- else if(ch==6)
- det();//call of determinant function
- else if(ch==7)
- {
- //cout<<"the inverse of the matrix A is ";
- inverse();//call of inverse of matrix function
- }
- else if(ch==8)
- adjoint();//call of adjoint function
- cout<<endl;
- system("PAUSE");
- return 0;
- }
- void matsub()//subtraction function
- {
- for(i=0;i<3;i++)
- {
- for(j=0;j<3;j++)
- {
- C[i][j]=A[i][j]-B[i][j];
- }
- }//end of subtraction operation loop
- for(i=0;i<3;i++)
- {
- cout<<"\n";
- for(j=0;j<3;j++)
- {
- cout<<" "<<C[i][j];
- }
- }//end pf output loop
- }//end of function
- void add()//addtiotn function
- {
- for(i=0;i<3;i++)//addition operation
- {
- for(j=0;j<3;j++)
- {
- C[i][j]=A[i][j]+B[i][j];
- }
- }
- //end of input loop
- for(i=0;i<3;i++)
- {
- cout<<"\n";
- for(j=0;j<3;j++)
- {
- cout<<" "<<C[i][j];
- }
- }//end of output loop
- }//end of function
- void matmult()//function for matrix multiplication
- {
- for(i=0;i<3;i++)//start of operation of multiplication
- {
- for(j=0;j<3;j++)
- {
- D[i][j]=0;
- for(k=0;k<3;k++)
- {
- D[i][j]=D[i][j]+A[i][k]*B[k][j];
- }
- }
- }//end of loop
- for(i=0;i<3;i++)
- {
- for(j=0;j<3;j++)
- {
- cout<<" "<<D[i][j];
- }
- cout<<endl;
- }//end of output loop
- }//end of function matmult
- void trans()//transpose function
- {
- cout<<"matrix A"<<endl;
- for(i=0;i<3;i++)//start of operation of transpose
- {
- E[3][3]=0;
- for(j=0;j<3;j++)
- {
- T[j][i]=A[i][j];
- }
- }
- for(i=0;i<3;i++)// output of transpose
- {
- for(j=0;j<3;j++)
- {
- cout<<" "<<T[i][j];
- }
- cout<<endl;
- }//end of loop for A
- cout<<"matrix B"<<endl;
- for(i=0;i<3;i++)//start of operation of transpose for B
- {
- E[3][3]=0;
- for(j=0;j<3;j++)
- {
- T[j][i]=B[i][j];
- }
- }
- for(i=0;i<3;i++)//output of transpose for B
- {
- for(j=0;j<3;j++)
- {
- cout<<" "<<T[i][j];
- }
- cout<<endl;
- }//END OF LOOP FOR B
- }//end of function trans
- void sym()//symmetry
- {
- //matrix A
- cout<<" checking for matrix A"<<endl;
- for(i=0;i<3;i++)//start of operation of transpose
- {
- for(j=0;j<3;j++)
- {
- T[j][i]=A[i][j];
- }
- }
- for(i=0;i<3;i++)//start of operation of transpose
- {
- for(j=0;j<3;j++)
- {
- cout<<" "<<T[i][j];
- }
- cout<<endl;
- }
- for(i=0;i<3;i++)//start of operation of transferinf ////elemts to matrix e
- {
- for(j=0;j<3;j++)
- {
- A[i][j]=E[i][j];
- }
- }
- for(i=0;i<3;i++)//start of chexkong of symmetr
- {
- for(j=0;j<3;j++)
- {
- if(T[i][j]!=E[i][j])
- {
- f=0;
- }
- }
- }//end of loop
- //if else for checking symmetricity
- if(f!=0)
- cout<<" its a symmetric matrix";
- else
- cout<<"its not a symmetric";
- cout<<"\n";
- //matrix B
- cout<<"checking for matrix B"<<endl;
- for(i=0;i<3;i++)//start of operation of transpose
- {
- for(j=0;j<3;j++)
- {
- T1[j][i]=B[i][j];
- }
- }
- for(i=0;i<3;i++)//start of operation of transpose
- {
- for(j=0;j<3;j++)
- {
- cout<<" "<<T1[i][j];
- }
- cout<<endl;
- }
- for(i=0;i<3;i++)//start of operation of transferinf ////elemts to matrix e
- {
- for(j=0;j<3;j++)
- {
- B[i][j]=F[i][j];
- }
- }
- for(i=0;i<3;i++)//start of chexkong of symmetr
- {
- for(j=0;j<3;j++)
- {
- if(T1[i][j]!=F[i][j])
- {
- f1=0;
- }
- }
- }//end of loop
- //if else for checking symmetricity
- if(f1!=0)
- cout<<" its a symmetric matrix";
- else
- cout<<"its not a symmetric matrix";
- }//end of function sym
- void det()//det function
- {
- determinant= A[0][0]*((A[1][1]*A[2][2])-(A[2][1]*A[1][2]))-A[0][1]*((A[1][0]*A[2][2])-(A[2][0]*A[1][2]))+A[0][2]*((A[1][0]*A[2][1])-(A[2][0]*A[1][1]));
- determinantB= B[0][0]*((B[1][1]*B[2][2])-(B[2][1]*B[1][2]))-B[0][1]*((B[1][0]*B[2][2])-(B[2][0]*B[1][2]))+B[0][2]*((B[1][0]*B[2][1])-(B[2][0]*B[1][1]));
- cout<<"the determinant of matrix A is "<<determinant<<endl;
- cout<<"the determinant of matrix B is "<<determinantB<<endl;
- }//en d of function det
- void adjoint()//adjoint fumction
- {
- //for matrix A
- cout<<"adjoint of matrix A is "<<endl;
- for(i=0;i<3;i++)
- {
- for(j=0;j<3;j++)
- {
- adj[i][j]=0;
- adj[i][j]=(A[(i+1)%3][(j+1)%3]*A[(i+2)%3][(j+2)%3])-(A[(i+1)%3][(j+2)%3]*A[(i+2)%3][(j+1)%3]);
- }
- }
- for(i=0;i<3;i++)
- {
- cout<<"\n";
- for(j=0;j<3;j++)
- {
- cout<<" "<<adj[i][j];
- }
- }//end of loop for matrix a
- cout<<"\n"<<endl;
- //for matrix B
- cout<<"adjoint of matrix B is "<<endl;
- for(i=0;i<3;i++)
- {
- for(j=0;j<3;j++)
- {
- adjB[i][j]=0;
- adjB[i][j]=(B[(i+1)%3][(j+1)%3]*B[(i+2)%3][(j+2)%3])-(B[(i+1)%3][(j+2)%3]*B[(i+2)%3][(j+1)%3]);
- }
- }
- for(i=0;i<3;i++)
- {
- cout<<"\n";
- for(j=0;j<3;j++)
- {
- cout<<" "<<adjB[i][j];
- }
- }
- }//end of function adjoint
- void inverse()//inverse function
- {
- determinant= A[0][0]*((A[1][1]*A[2][2])-(A[2][1]*A[1][2]))-A[0][1]*((A[1][0]*A[2][2])-(A[2][0]*A[1][2]))+A[0][2]*((A[1][0]*A[2][1])-(A[2][0]*A[1][1]));
- if(determinant!=0)
- {
- cout<<"the inverse of the matrix A is "<<endl;
- for(i=0;i<3;i++)//output loop
- {
- cout<<"\n";
- for(j=0;j<3;j++)
- {
- cout<<" "<<((A[(i+1)%3][(j+1)%3]*A[(i+2)%3][(j+2)%3])-(A[(i+1)%3][(j+2)%3]*A[(i+2)%3][(j+1)%3]))/determinant;
- }
- }
- }
- else
- {
- cout<<"the inverse of this matrix A is not possible since the determinant is 0"<<endl;
- }
- cout<<endl;
- // for matrix B
- determinantB= B[0][0]*((B[1][1]*B[2][2])-(B[2][1]*B[1][2]))-B[0][1]*((B[1][0]*B[2][2])-(B[2][0]*B[1][2]))+B[0][2]*((B[1][0]*B[2][1])-(B[2][0]*B[1][1]));
- if(determinantB!=0)
- {
- cout<<"the inverse of the matrix B is "<<endl;
- for(i=0;i<3;i++)//output loop
- {
- cout<<"\n";
- for(j=0;j<3;j++)
- {
- cout<<" "<<((B[(i+1)%3][(j+1)%3]*B[(i+2)%3][(j+2)%3])-(B[(i+1)%3][(j+2)%3]*B[(i+2)%3][(j+1)%3]))/determinant;
- }
- }
- }
- else
- {
- cout<<"the inverse of this matrix B is not possible since the determinant is 0"<<endl;
- }
- }//end of function
- THE OUTPUT
Advertisement
Add Comment
Please, Sign In to add comment