C++ - Matrices multiplyer
1
A quick matrices multiplyer I wrote in C++; I was a bit lazy as i had to get it done quickly and so it lacks functions and a decent structure but gets the job done.
#include <iostream>
using namespace std;
int main ()
{
//program here:
//TODO - Remove variables from array declarations to comply with C++ standards
cout<<"Please enter # of rows : Matrix 1 \n";
int numy;
cin >> numy;
cout<<"Please enter # of columns : Matrix 1 \n";
int numx;
cin >> numx;
//cout << numx << " " << numy << "\n";
cout<<"Please enter # of rows: Matrix 2 \n";
int numy2;
cin >> numy2;
cout<<"Please enter # of columns: Matrix 2 \n";
int numx2;
cin >> numx2;
//cout << numx << " " << numy << "\n";
//Check coherency
if (numx!=numy2) {
cout << "Error: Matrices are incoherent - impossible product";
return 0;
}
int matrix1 [numx][numy];
//finished setting up array - time to fill
int curx=0;
int cury=0;
int curnum=0;
int iteration=1;
while (iteration<=numx*numy){
//curx is x-coord (col number) cury is y-coord (row number)
cout<<"Enter number - Matrix 1. Position: " << curx << ", " << cury << " \n";
cin >> curnum;
matrix1[curx][cury]=curnum;
curx++;
iteration++;
if (curx==numx){ //arrays start from 0 so this is right
curx=0;
cury++;
}
}
cout << "Numbers taken - Matrix 1 filled \n \n";
// Matrix 2
int matrix2 [numx2][numy2];
//finished setting up array - time to fill
curx=0;
cury=0;
curnum=0;
iteration=1;
while (iteration<=numx2*numy2){
cout<<"Enter number - Matrix 2. Position: " << curx << ", " << cury << " \n";
cin >> curnum;
matrix2[curx][cury]=curnum;
curx++; //remember x and y refer to co-ords
iteration++;
if (curx==numx2){ //arrays start at 0
curx=0;
cury++;
}
}
//Print matrices check they're correct
cout << "Numbers taken - Matrix 2 filled \n \n";
cout <<"\n Matrix 1: \n";
/* For Debugging and Viewing arrays - Currently set up for matrix1 */
iteration = 1;
curx=0;
cury=0;
while (iteration<=numx*numy){
cout << matrix1[curx][cury] << " ";
iteration++;
curx++;
if (curx==numx){ //arrays start from 0
curx=0;
cury++;
cout << " \n";
}
}
cout <<"\n Matrix 2: \n";
/* For Debugging and Viewing arrays - Currently set up for matrix2 */
iteration = 1;
curx=0;
cury=0;
while (iteration<=numx2*numy2){
cout << matrix2[curx][cury] << " ";
iteration++;
curx++;
if (curx==numx2){ //arrays start from 0
curx=0;
cury++;
cout << " \n";
}
}
string check;
cout <<"\n Are these correct? (y/n) \n";
cin >> check;
if (check=="n") {
cout << "Error: Matrices incorrect - Please start again";
return 0;
}
//All martrices filled and coherency checked - multiplication may now take place
//Result matrix has same number of rows as the first and same number of coulmns as the second
int rmatx=numx2; //x=xcord or col. #
int rmaty=numy; //y=ycord or row #
int rmatrix [rmatx][rmaty];
curx=0;
cury=0;
curnum=0; //is used to represent the current multiplication - summed to make entnum
iteration=1;
int rcurx=0; //is used for the current x co-ordinate in the rmatrix
int rcury=0; //is used for the current y co-ordinate in the rmatrix
int entnum=0; //entnum is used to sum the curnums so it may be added to rmatrix
//Think of while loop which will work
while (iteration<=rmatx*rmaty){ //rmatx and rmaty are literal numbers of columns/rows as if entered in by user
while(curx<numx && cury<numy2){ //Maybe rmatx instead of numx
curnum=matrix1[curx][rcury]*matrix2[rcurx][cury];
entnum+=curnum;
curx++;
cury++;
}
rmatrix[rcurx][rcury]=entnum;
curx=0;
cury=0;
rcurx++;
iteration++;
curnum=0;
entnum=0;
if(rcurx==rmatx){ //arrays start from 0
rcury++;
rcurx=0;
}
}
/* For Debugging and Viewing arrays - Currently set up for rmatrix */
iteration = 1;
curx=0;
cury=0;
cout <<"\n Resultant matrix: \n";
while (iteration<=rmatx*rmaty){
cout << rmatrix[curx][cury] << " ";
iteration++;
curx++;
if (curx==rmatx){ //arrays start from 0
curx=0;
cury++;
cout << " \n";
}
}
return 0;
}
//TODO - Remove variables from array declarations to comply with C++ standards






There are currently no comments for this snippet.