Multiplying Random Matrices

i have this program here that multiplies two matrices together. you enter in the dimensions of the matrices and then the elements of the matrices. but instead of manually entering the elements i want to have the program enter in random elements.

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
#include <iostream>
#include <string>
using namespace std;

class matrix{
public:
    int i, j, row, col, mat[10][10];
    void input();
    matrix operator*(matrix mm2);
    void output();
};
void matrix::input(){
    cout<<"\nEnter number of rows and columns : ";
    cin>>row>>col;
    cout<<"\nEnter the elements : ";
    for(i=0; i<row; i++){
        for(j=0;j<col;j++){
            cin>>mat[i][j];
        }
    }
}
matrix matrix::operator*(matrix mm2){
    matrix temp;
    temp.row=row;
    temp.col=col;
    for(i=0; i<temp.row; i++){
        temp.mat[i][j]=0;
        for(j=0; j<temp.col; j++){
            temp.mat[i][j]+=mat[i][j]*mm2.mat[i][j];
        }
    }
    return temp;
}
void matrix::output(){
    cout<<"Matrix is : \n";
    for(i=0;i<row;i++){
        for(j=0;j<col;j++){
            cout<<mat[i][j]<<"\t";
        }
        cout<<"\n";
    }
}
int main(){
    matrix m1, m2, m3;
    m1.input();
    m2.input();
    m3=m1*m2;
    cout<<"\nProduct ";
    m3.output();
    return 0;
}
Last edited on
http://www.cplusplus.com/reference/random/uniform_int_distribution/


Note, I presume that you still want the user to determine the dimensions. As a user, I do write 42 7. Do we feel lucky?

Are you sure about the math of multiplication? How does a 3x5 multiply a 2x2?

The operator* should be a non-member function.
Topic archived. No new replies allowed.