Matrix-Matrix Multiplication and Matrix-Vector Multiplication using C++
The Code which is presented below can multiply both Matrix-Matrix as well as Matrix-Vector:
Firstly, the code checks for the size of the matrix and verifies if the Matrix-Matrix or Matrix-Vector pair is compatible for multiplication:
- If the Matrix-Matrix or Matrix-Vector pair are compatible, the code goes to next step where it accepts the data from the user, arranges it in the matrix/vector form and continues with the multiplication.
- If the Matrix-Matrix or Matrix-Vector pair are not compatible, the code informs the user that the given Matrix-Matrix or Matrix-Vector pair are not compatible and it exits the code.
The Code for Both Matrix-Matrix and Matrix-Vector Multiplication is as given below:
//Program that performs Matix Multiplication Operation//
#include<iostream>
int main(){
int r1, c1, r2, c2, r3, c3;
//Verifying the correct size of the Matrices//
std::cout<<"Enter the size of the Matrix A i.e rows * columns"<<std::endl;
std::cin>>r1>>c1;
std::cout<<"Enter the size of the Matrix B i.e rows * columns"<<std::endl;
std::cin>>r2>>c2;
if(c1==r2){
std::cout<<"Column of Matrix A == Row of Matrix B, hence, multiplication is possible"<<std::endl;
}
else{
std::cout<<"Column of Matrix A is not equal to Row of Matrix B"<<std::endl;
std::cout<<"Program Terminated"<<std::endl;
std::cout<<"Please choose a compatible Matix-Matrix or Matrix-Vector pair."<<std::endl;
std::exit(0);
}
//Reading and Printing the first Matrix//
double Mat1[r1][c1];
float el1;
for(int i = 0; i < r1; ++i){
for(int j = 0; j < c1; ++j){
std::cout<<"Enter the Elements of Matrix A:"<<std::endl;
std::cin>>el1;
Mat1[i][j] = el1;
}
}
std::cout<<"Matrix A:"<<std::endl;
for(int i = 0; i < r1; ++i){
for(int j = 0; j < c1; ++j){
std::cout<<" "<<Mat1[i][j];
}std::cout<<" "<<std::endl;
}
//Reading and Printing the second Matrix//
double Mat2[r2][c2];
float el2;
for(int i = 0; i < r2; ++i){
for(int j = 0; j < c2; ++j){
std::cout<<"Enter the Elements of Matrix B:"<<std::endl;
std::cin>>el2;
Mat2[i][j] = el2;
}
}
std::cout<<"Matrix B:"<<std::endl;
for(int i = 0; i < r2; ++i){
for(int j = 0; j < c2; ++j){
std::cout<<" "<<Mat2[i][j];
}std::cout<<" "<<std::endl;
}
//Matrix Multiplication Starts//
//Initialization of Matix Multiplication (we first initialize the product to zero)//
//We produce a zeros matrix//
double MatProd[r1][c2];
for(int i = 0; i < r1; ++i){
for(int j = 0; j < c2; ++j){
MatProd[i][j] = 0;
}
}
//We now replace the zeros in the zeros matrix with product values//
for(int i = 0; i < r1; ++i){
for(int j = 0; j < c2; ++j){
for(int k = 0; k < c1; ++k){
MatProd[i][j] += Mat1[i][k] * Mat2[k][j];
}
}
}
std::cout<<"The Product of Matrices:"<<std::endl;
r3 = r1;
c3 = c2;
for(int i = 0; i < r3; ++i){
for(int j = 0; j < c3; ++j){
std::cout<<" "<<MatProd[i][j];
}std::cout<<" "<<std::endl;
}
return 0;
}
Results:
Matrix-Matrix Multiplication:
Matrix A:
Matrix B:
Product of the Matrices:
Matrix-Vector Multiplication:
Matrix A:
Vector B:
Product of Matrices:
Conclusion:
The Code presented above can multiply both Matrix-Matrix Pair and Matrix-Vector Pair.