Matrices (NxM) Multiplication

Hello everyone, I would need to write a c++ program that takes a matrix from the console, trasposes it, and then calculates the product of the two. I managed to write the first part of the code with the trasposed one. I would need an help for the last part insted. When I run the program it gives back random numbers, I don't know where I am mistaking. This is the source code I wrote.
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
using namespace std;

const int N=2, M=3;

void getMatrix(int matrix[N][M]);
void printMatrix(int matrix[N][M]);
void calculateTrasposed(int m[N][M], int t[M][N]);
void calculateProduct(int m1[N][M],int m2[M][N], int product[N][N]);

int main(){
    int matrix[N][M], trasposed[M][N], product[N][N];
    getMatrix(matrix);
    printMatrix(matrix);
    calculateTrasposed(matrix,trasposed);
    calculateProduct(matrix,trasposed,product); 

system("PAUSE");
}

void getMatrix(int matrix[N][M]){
     cout << "Insert the " << N << " x " << M << " matrix: " <<endl;
     for (int i=0; i<N; i++)
         for(int j=0; j<M; j++)
             cin >> matrix[i][j];
     return;
}
void printMatrix(int matrix[N][M]){
     cout << "The inserted matrix is: " <<endl;
     for (int i=0; i<N; i++){
         for(int j=0; j<M; j++){
             cout << matrix[i][j] << "\t";
         }
         cout <<endl;
     }
     return;
}
void calculateTrasposed(int m[N][M], int t[M][N]){
     cout << "The trasposed matrix is: " <<endl;
     int temp;
     for (int i=0; i<N; i++){
         for(int j=0; j<M; j++){
             temp=m[i][j];
             m[i][j]=t[j][i];
             t[j][i]=temp;
         }
     }
     for(int i=0; i<M; i++){
         for(int j=0; j<N; j++){
             cout << t[i][j] << "\t";
         }
         cout <<endl;
     }
     return;
     
}
void calculateProduct(int m1[N][M],int m2[M][N], int product[N][N]){
    int k;
    for(int i=0; i<N; i++){
        for(int j=0; j<M; j++){
            product[i][j] = 0;
            for(k=0; k<N; k++){
                product[i][j] += m1[i][k] * m2[k][j];
            }
        }
    }
    cout << "The product of the matrix is: " <<endl;
    for (int i=0; i<k; i++){
        for(int j=0; j<k; j++){
            cout << product[i][j] << "\t";
        }
        cout <<endl;
    }


    return;
}

Thank you.

P.S. sorry for the english, I'm italian.

Last edited on
Hi!
I'm a beginner C++ programmer too from the neighbourhood (HUngary) :)

First of all: Trasposed - missing n --correct--> Transposed

Transpose of a matrix:
http://en.wikipedia.org/wiki/Transpose

I have corrected your code, you can test production of matrices online e.g. here:

Checking the matrix product:
http://easycalculation.com/matrix/matrix-multiplication.php

As I said I am beginner so I can make mistakes
but it looks like my code works:

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
using namespace std;

const int N=2, M=3;

void getMatrix(int matrix[N][M]);
void printMatrix(int matrix[N][M]);
void calculateTransposed(int m[N][M], int t[M][N]);
void calculateProduct(int m1[N][M],int m2[M][N], int product[N][N]);

int main(){
    int matrix[N][M], Transposed[M][N], product[N][N];
    getMatrix(matrix);
    printMatrix(matrix);
    calculateTransposed(matrix,Transposed);
    calculateProduct(matrix,Transposed,product);

//system("PAUSE");
}

void getMatrix(int matrix[N][M]){
     cout << "Insert the " << N << " x " << M << " matrix: " <<endl;
     for (int i=0; i<N; i++)
         for(int j=0; j<M; j++)
             cin >> matrix[i][j];
     return;
}
void printMatrix(int matrix[N][M]){
     cout << "The inserted matrix is: " <<endl;
     for (int i=0; i<N; i++){
         for(int j=0; j<M; j++){
             cout << matrix[i][j] << "\t";
         }
         cout <<endl;
     }
     return;
}
void calculateTransposed(int m[N][M], int t[M][N]){
     cout << "The Transposed matrix is: " <<endl;

     for (int i=0; i<N; i++){
         for(int j=0; j<M; j++){
            t[j][i]=m[i][j];                // without temp variable
         }
     }
     for(int i=0; i<M; i++){
         for(int j=0; j<N; j++){
             cout << t[i][j] << "\t";
         }
         cout <<endl;
     }
     return;

}
void calculateProduct(int m1[N][M],int m2[M][N], int product[N][N]){
    int k;
    for(int i=0; i<N; i++){
        for(int j=0; j<N; j++){             // not j<M
            product[i][j] = 0;
            for(k=0; k<M; k++){
                product[i][j] += m1[i][k] * m2[k][j];
            }
        }
    }
    cout << "The product of the matrix is: " <<endl;
    for (int i=0; i<N; i++){                // not i<k (matrix product is NxN)
        for(int j=0; j<N; j++){             // not j<k (matrix product is NxN)
            cout << product[i][j] << "\t";
        }
        cout <<endl;
    }

    return;
}
hahahahaah nice one for the TRANSPOSED ;)!
Thank you, now I understood, it's working perfectly.
Topic archived. No new replies allowed.