Posted by : Waqas Javed Monday, 22 December 2014

#include<iostream>
#include<conio.h>

using namespace std;

//Entr order of matrixes
void order(int &matrix1_rows, int &matrix1_colomn, int &matrix2_rows,int &matrix2_colomn) {
cout << "Enter no of rows of 1st Matrix: ";
cin >> matrix1_rows;

cout << "Enter no of colomn of 1st Matrix: ";
cin >> matrix1_colomn;

cout << endl;

cout << "Enter no of rows of 2nd Matrix: ";
cin >> matrix2_rows;

cout << "Enter no of colomn of 2nd Matrix: ";
cin >> matrix2_colomn;

cout << endl;
}

//chk if multiplication is possible
int chk(int matrix1_rows,int matrix1_colomn, int matrix2_rows, int matrix2_colomn){
if(matrix1_colomn == matrix2_rows){
cout << "Multiplication possible" << endl;
return 1;
} else {
cout << "Multiplication not possible" << endl << "Multiply any other Matrix" << "\n\n";
}
return 0;
}

void main() {
//variable for Matrix1
int matrix1_rows = 0;
int matrix1_colomn = 0;
int matrix1[100];
int matrix1_max = 0;

//variable for Matrix2
int matrix2_rows = 0;
int matrix2_colomn = 0;
int matrix2[100];
int matrix2_max = 0;

//variable used to count the matrix element in any loop
int displayCounter1 = 0;
int displayCounter2 = 0;

//variable for result matrix
int result_matrix[100];
int result_matrix_rows = 0;
int result_matrix_colomn = 0;
int result_max;

//variable used for calculation of result
int resultCounter = 0;
int x = 0;
int y = 0;
int z = 0;

//start
cout << "Program to multiply two matrix" << endl << endl;

//retaking user input if multiplication is not possible
int a = 0;

do{
order(matrix1_rows, matrix1_colomn, matrix2_rows, matrix2_colomn);
a = chk(matrix1_rows, matrix1_colomn, matrix2_rows, matrix2_colomn);
} while(a !=1);


//Intializing data if multiplication is possible
matrix1_max = matrix1_rows*matrix1_colomn;
matrix2_max = matrix2_rows*matrix2_colomn;



//data entry matrix1
for(int i=0; i < matrix1_max; i++){
cout << "Enter" << "Element NO." << i+1 << " of Matrix1: ";
cin >> matrix1[i];
}

cout << endl;

//data entry matrix2
for(int i=0; i < matrix2_max; i++){
cout << "Enter" << "Element NO." << i+1 << " of Matrix2: ";
cin >> matrix2[i];
}

//matrix1 display
cout << "Matrix-1" << endl;
while(displayCounter1< matrix1_max){
cout << "|\t";
for(int i =0; i<matrix1_colomn; i++){
cout << matrix1[displayCounter1] << "\t";
displayCounter1++;
}
cout << '|' << endl;
}

//print multiplication sybmbol between middle
cout << endl << "\tX" << endl;

//matrix2 display
cout << endl << "Matrix-2" << endl;
while(displayCounter2< matrix2_max){
cout << "|\t";

for(int i =0; i<matrix2_colomn; i++){
cout << matrix2[displayCounter2] << "\t";
displayCounter2++;
}
cout << '|' << endl;
}

//intilizing result matrix
result_matrix_rows = matrix1_rows;
result_matrix_colomn = matrix2_colomn;
result_max = result_matrix_rows*result_matrix_colomn;
resultCounter = 0;

//intializing array 0 value
while(resultCounter < result_max){
result_matrix[resultCounter] = 0;
resultCounter++;
}
resultCounter = 0;

//The main multiplication
for(int i = 0; i < matrix1_rows; i++){
for(int j = 0; j <= matrix2_colomn-1; j++){
for(int k = 0; k < matrix1_colomn; k++){
result_matrix[resultCounter] += ((matrix1[k+x]) * (matrix2[z+y]));
y += matrix2_colomn;
}
z++;
y  = 0;
resultCounter++;
}
x += matrix1_colomn;
z = 0;
}

//diplaying result
resultCounter = 0;
cout << endl << "Result";

while(resultCounter < result_max){
cout << "|\t";
for(int i =0; i<result_matrix_colomn; i++){
cout << result_matrix[resultCounter] << "\t";
resultCounter++;
}
cout << '|' << endl;
}

_getch();
}

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Popular Post

Powered by Blogger.

- Copyright © BSCS -Metrominimalist- Powered by Blogger - Designed by Johanes Djogan -