// This program finds the product of 2 matrices entered by the user. #include <iostream> #include <iomanip> using namespace std; #define SIZE 10 //SIZE of ARRAYS //function(s) declaration void input(int [][SIZE], int [][SIZE], const int&, const int&, const int&); void process(int [][SIZE], int [][SIZE], int [][SIZE], const int&, const int&, const int&); void display(int [][SIZE], int [][SIZE], int [][SIZE], const int&, const int&, const int&); int main() { system("color f0");//for changing the color of output screen //declaration of ARRAYS int a[SIZE][SIZE], b[SIZE][SIZE], c[SIZE][SIZE]; //declaration of variables int m,n; // Rows & Columns of Matrix A respectively int x,y; // Rows & Columns of Matrix B respectively bool flag = true; //flag variable cout<<"Rule for Multiplication of matrices : \n\nNumber of COLUMNS of Matrix 'A' must be equal to Number of ROWS of Matrix 'B'."<<endl<<endl; //input do { cout<<"\n\nEnter the Number of ROWS of Matrix 'A' [Max. 10] : "; cin>>m; if((m>=1)&&(m<=10)) break; else cout<<"Invalid Input...."<<endl; } while (1); do { cout<<"Enter the Number of COLUMNS of Matrix 'A' [Max. 10] : "; cin>>n; if((n>=1)&&(n<=10)) break; else cout<<"Invalid Input...."<<endl; } while (1); do { cout<<"\nEnter the Number of ROWS of Matrix 'B' [Max. 10] : "; cin>>x; if((x>=1)&&(x<=10)) break; else cout<<"Invalid Input...."<<endl; } while (1); do { cout<<"Enter the Number of COLUMNS of Matrix 'B' [Max. 10] : "; cin>>y; if((y>=1)&&(y<=10)) break; else cout<<"Invalid Input...."<<endl; } while (1); if(n==x) input(a,b,m,n,y); //function calling else { cout<<"Multiplication is not possible"<<endl; flag=false; }//end of else //processing if(flag==true) process(a,b,c,m,n,y); //function calling //output if(flag==true) display(a,b,c,m,n,y); //function calling cout<<endl<<endl; system("pause"); return 0; }//end of main void input(int aa[][SIZE], int bb[][SIZE], const int& mm, const int& nn, const int& yy) { system("cls"); cout<<"\t \tFor Matrix 'A'\n\n"; for(int i=0; i<mm; i++) { for(int j=0; j<nn; j++) { cout<<"Enter Element at ROW "<<i+1<<" and COLUMN "<<j+1<<" : "; cin>>aa[i][j]; }//end of nested for }//end of for cout<<"\n\n\t\tFor Matrix 'B'\n\n"; for(int k=0; k<nn; k++) //because n==x { for(int l=0; l<yy; l++) { cout<<"Enter Element at ROW "<<k+1<<" and COLUMN "<<l+1<<" : "; cin>>bb[k][l]; }//end of nested for }//end of for }//end of input void process(int aa[][SIZE], int bb[][SIZE], int cc[][SIZE], const int& mm, const int& nn, const int& yy) { for(int i=0; i<mm; i++) { for(int j=0; j<yy; j++) { cc[i][j]=0; for(int k=0; k<nn; k++) cc[i][j]=cc[i][j]+(aa[i][k]*bb[k][j]); }//end of nested for }//end of for }//end of process void display(int aa[][SIZE], int bb[][SIZE], int cc[][SIZE], const int& mm, const int& nn, const int& yy) { system("cls"); cout<<"Matrix 'A' of the order "<<mm<<"x"<<nn<<" =\n\n"; for (int i=0; i<mm; i++) { for(int j=0; j<nn; j++) cout<<setw(8)<<aa[i][j]; cout<<endl; }//end of for cout<<"\n\nMatrix 'B' of the order "<<nn<<"x"<<yy<<" =\n\n"; for(int k=0; k<nn; k++) //because n==x { for(int l=0; l<yy; l++); cout<<setw(8)<<bb[k][l]; cout<<endl; }//end of for cout<<"\n\n\nTheir Product 'AxB' of the order "<<mm<<"x"<<yy<<" =\n\n"; for(int p=0; p<mm; p++) { for(int q=0; q<yy; q++) cout<<setw(8)<<cc[p][q]; cout<<endl; }//end of for }//end of display
This program finds the product of 2 matrices entered by the user
Post A Comment:
0 comments: