Matrix Multiplication

Hello, I am having trouble with my matrix multiplication program. What the program does is takes in the dimensions of the two matrices via command line as well as the numbers in matrices via command line. I believe I have issues regarding my arrays and pointers and how the multiplication function is getting the information.

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
  #include <iostream>
#include <cstdlib>
#include <stdio.h>
using namespace std;

// Preconditions: m is a two-dimensional array, initialized
//				  the first dimension of m is [0, 1, ..., rows-1]
//				  the second dimension of m is [0, 1, ..., cols-1]
// Postconditions: m is output to the screen
void output(int **m, int rows, int cols);

// Postconditions: outputs the usage statement to the screen
void usage();

// Preconditions: m1, m2, and r are two-dimensional arrays
//				  m1 and m2 are initialized
//				  the first dimension of m1 is [0, 1, ... rrows-1]
//				  the second dimension of m1 is [0, 1, ... inner-1]
//				  the first dimension of m2 is [0, 1, ... inner-1]
//				  the second dimension of m2 is [0, 1, ... rcols-1]
//				  the first dimension of r is [0, 1, ... rrows-1]
//				  the second dimension of r is [0, 1, ... rcols-1]
// Postconditions: r = m1 x m2
void m_mult(int **m1, int **m2, int **r, int rrows, int rcols, int inner);

int main(int argc, char *argv[])
{
    if (argc < 2)
    {
        cout << "usage: matrix_mult <m1 rows> <m1 columns> <m2 rows> <m2 columns> 1 " << endl;
        return 0;
    }
    
    int m1r = atoi( argv[1] );
    int m1c = atoi( argv[2] );
    int m2r = atoi( argv[3] );
    int m2c = atoi( argv[4] );

    
    if( ( m1r < 1 ) || (m1c < 1))
    {
        cout << "usage: matrix_mult <m1 rows> <m1 columns> <m2 rows> <m2 columns> 2" << endl;
        return -1;
    }
    else if(( m2r < 1 ) || (m2c < 1))
    {
        cout << "usage: matrix_mult <m1 rows> <m1 columns> <m2 rows> <m2 columns> 3" << endl;
        return -2;
    }
    else if( m1c != m2r)
    {
        cout << "Invalid Dimensions" << endl;
        return -3;
    }
    
    int **m1;
    
        m1 = new int*[ m1r ];
        
        for ( int i=0; i<m1r; i++ )
        {
            m1[i] = new int[ m1c ];
            
                for ( int j=0; j<m1c; j++ )
                    cin >> m1[i][j];
        }
    
    int **m2;
    
        m2 = new int*[ m2r ];
        
        for ( int i=0; i<m2r; i++ )
        {
            m2[i] = new int[ m2c ];
            
                for ( int j=0; j<m2c; j++ )
                    cin >> m2[i][j];
        }
        
    int **r;
    
        r = new int*[ m1r ];
    
    for ( int i=0; i<m2c; i++)
    {
        r[i] = new int[ m2c ];
        
    }
    
    m_mult( m1, m2, r, m1r, m2c, m1c);
    
    output( r, m1r, m2c);
    
    delete [] m1;
    delete [] m2;
    delete [] r;
    return 0;
}

void usage()
{
    cout << "usage: matrix_mult <m1 rows> <m1 columns> <m2 rows> <m2 columns>" << endl;
}

void output(int **m, int rows, int cols)
{
    cout << "Equals:" << endl;
    
    for ( int i=0; i<rows; i++ )
    {
        for ( int j=0; j<cols; j++ )
            cout << m[i][j] << " ";
        
        cout << endl;
    }
    
}

void m_mult(int **m1, int **m2, int **r, int rrows, int rcols, int inner)
{
    for(int i = 0; i < rrows; i++)
        for(int j = 0; j < inner; j++)
        {
            r[i][j]=0;
            for(int k = 0; k < rcols; k++)
                r[i][j] += m1[i][j] * m2[i][j];
        }
    
}
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <cstdlib>
#include <cstdio>  // use the proper header
using namespace std;

// Preconditions: m is a two-dimensional array, initialized
//				  the first dimension of m is [0, 1, ..., rows-1]
//				  the second dimension of m is [0, 1, ..., cols-1]
// Postconditions: m is output to the screen
void output(int **m, int rows, int cols);

// Postconditions: outputs the usage statement to the screen
void usage();

// Preconditions: m1, m2, and r are two-dimensional arrays
//				  m1 and m2 are initialized
//				  the first dimension of m1 is [0, 1, ... rrows-1]
//				  the second dimension of m1 is [0, 1, ... inner-1]
//				  the first dimension of m2 is [0, 1, ... inner-1]
//				  the second dimension of m2 is [0, 1, ... rcols-1]
//				  the first dimension of r is [0, 1, ... rrows-1]
//				  the second dimension of r is [0, 1, ... rcols-1]
// Postconditions: r = m1 x m2
void m_mult(int **m1, int **m2, int **r, int rrows, int rcols, int inner);

int main(int argc, char *argv[])
{
    if (argc != 5)  // check for the proper number of arguments!
    {
        usage();    // use your usage function!
        return 0;
    }
    
    int m1r = atoi( argv[1] );
    int m1c = atoi( argv[2] );
    int m2r = atoi( argv[3] );
    int m2c = atoi( argv[4] );

    
    if( ( m1r < 1 ) || (m1c < 1))
    {
        usage();
        return 1;   // don't return negative numbers!
    }
    else if(( m2r < 1 ) || (m2c < 1))
    {
        usage();
        return 2;
    }
    else if( m1c != m2r)
    {
        cout << "Invalid Dimensions" << endl;
        return 3;
    }
    
    int **m1;
    
        // notice that you are doing the same thing here:
        m1 = new int*[ m1r ];
        
        for ( int i=0; i<m1r; i++ )
        {
            m1[i] = new int[ m1c ];
            
                for ( int j=0; j<m1c; j++ )
                    cin >> m1[i][j];
        }
    
    int **m2;
    
        // ...and here:
        m2 = new int*[ m2r ];
        
        for ( int i=0; i<m2r; i++ )
        {
            m2[i] = new int[ m2c ];
            
                for ( int j=0; j<m2c; j++ )
                    cin >> m2[i][j];
        }
        // when you are doing the same thing, think about using a function. (hint)
        
    int **r;
    
        r = new int*[ m1r ];
    
        for ( int i=0; i<m2c; i++)
        {
            r[i] = new int[ m2c ];
        
        }
    
    m_mult( m1, m2, r, m1r, m2c, m1c);
    
    output( r, m1r, m2c);
    
    delete [] m1;  // you forgot to delete m1[0],m1[1],...,m1[m1c-1].
    delete [] m2;  // likewise for m2
    delete [] r;   // and m3 (hmm, doing the same thing over and over... hint)
    return 0;
}

void usage()
{
    cout << "usage: matrix_mult <m1 rows> <m1 columns> <m2 rows> <m2 columns>" << endl;
}

void output(int **m, int rows, int cols)
{
    cout << "Equals:" << endl;
    
    for ( int i=0; i<rows; i++ )
    {
        for ( int j=0; j<cols; j++ )
            cout << m[i][j] << " ";
        
        cout << endl;
    }
    
}

void m_mult(int **m1, int **m2, int **r, int rrows, int rcols, int inner)
{
    // You need to rethink your multiplication here, the following is not correct:
    //for(int i = 0; i < rrows; i++)
    //    for(int j = 0; j < inner; j++)
    //    {
    //        r[i][j]=0;
    //        for(int k = 0; k < rcols; k++)
    //            r[i][j] += m1[i][j] * m2[i][j];
    //    }
    // Consider, the result should be iterated over by its row and column:
    for (int rr = 0; rr < rrows; rr++)
    for (int rc = 0; rc < rcols; rc++)
    {
        r[rr][rc]=0;
        // Now, the result row and result column is the intersection of the m1[rr] and m2[rc], right?
        // So you'll need to iterate over the k=0..inner COLUMN of m1, and the k=0..inner ROW of m2.
        for (int k=0; k < inner; k++)
          r[rr][rc] += m1[...][...] * m2[...][...];
    }
}

Very nicely done otherwise.
Hope this helps.
Last edited on
Topic archived. No new replies allowed.