1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include<iostream>
#include<iomanip>
using namespace std;
void GenMatrix(int matrix [10][10], int rows, int cols)
{
for(int i=0;i<=rows-1;i++)
for(int j=0;j<=cols-1;j++)
matrix[i][j]=rand()%4;
}
void OutputMatrix(int matrix[10][10], int rows, int cols)
{
for(int i=0;i<=rows-1;i++)
{
for(int j=0;j<=cols-1;j++)
cout<<setw(5)<<matrix[i][j];
cout<<endl;
}
}
void MultiplicationMatrixes(int matrix1[10][10], int matrix2[10][10], int matrixResult[10][10], int rows1, int cols2, int cols1rows2)
{
for(int i=0;i<=rows1-1;i++)
for(int j=0;j<=cols2-1;j++)
{
matrixResult[i][j]=0;
for(int k=0;k<=cols1rows2-1;k++)
matrixResult[i][j]=matrixResult[i][j]+(matrix1[i][k]*matrix2[k][j]);
}
}
int main()
{
int a[10][10];
int b[10][10];
int matrixResult[10][10];
int n,m,l;
cout<<"Input the rows of the first matrix ";
cin>>n;
cout<<endl;
cout<<"Input the cols of the second matrix ";
cin>>m;
cout<<endl;
srand(time(0));
GenMatrix(a,n,m);
cout<<"The first matrix is : ";
OutputMatrix(a,n,m);
srand(time(0));
GenMatrix(b,n,m);
cout<<"The second matrix is : ";
OutputMatrix(b,n,m);
cout<<"Input the cols and the rows for the matrix : ";
cin>>l;
cout<<endl;
cout<<"The result from the multiplication of the two matrixes is : ";
MultiplicationMatrixes(a,b,matrixResult,n,m,l);
OutputMatrix(matrixResult,n,m); //I tried like that and it seems to be working;
cout<<endl;
system("pause");
return 0;
}
|