A little help with matrix multiplication

I've been assigned to build a program which completes mathematical operations using matrices. I have to use dynamically allocated 2d arrays, and can only use * to dereference and not []. I have this code so far, but the multiply_matrix function does not work with most values. I've looked at other similar posts, which have a similar algorithm to mine, but mine does not work for some reason.

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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*




*(*matrix(matrix+i)+j)

*/

#include <iostream>
//for sleep() which allows user to see messages before screen is cleared
#include <unistd.h>
using namespace std;

//xcode does not allow for using system("cls")
void cls();
void promptnew(int&, int&, int&, int&);
int** creatematrix(int, int);
void fillmatrix(int**, int, int, short);
void displaymatrix(int**, int, int);
void clearmatrix(int**, int , int);
void add_matrix(int**, int**, int, int, int, int);
void subtract_matrix(int**, int**, int, int, int, int);
void multiply_matrix(int**, int**, int, int, int, int);

int main()
{
    int _1matrix_row;
    int _1matrix_column;
    int _2matrix_row;
    int _2matrix_column;
    int** usermatrix1;
    int** usermatrix2;
    short menu_choice;

thebigbang:

    promptnew(_1matrix_row, _1matrix_column, _2matrix_row, _2matrix_column);
    usermatrix1 = creatematrix(_1matrix_column, _1matrix_row);
    usermatrix2 = creatematrix(_2matrix_column, _2matrix_row);
    fillmatrix(usermatrix1, _1matrix_row, _1matrix_column, 1);
    fillmatrix(usermatrix2, _2matrix_row, _2matrix_column, 2);
    
    //runs menu until exit option is choice
    do
    {
        
        //menu after correct pin is entered
        do
        {
            cout << "*** Welcome to the Matrix Math Calculator ****\n"
            << "1. Add two matrices\n"
            << "2. Subtract two matrices\n"
            << "3. Multiply two matrices\n"
            << "4. Clear and enter new matrices\n"
            << "5. Exit\n";
            cout << "Choose an option: ";
            cin >> menu_choice;
            
            //input validation
            if (menu_choice < 1 || menu_choice > 5)
            {
                cout << "Invalid selection, please select an option from the menu\n";
                //allows user to see error before proceeding
                sleep(3);
                cls();
            }
        }
        while(menu_choice < 1 || menu_choice > 5);
        
        if (menu_choice == 1)
        {
            cls();
            add_matrix(usermatrix1, usermatrix2, _1matrix_row, _1matrix_column, _2matrix_row, _2matrix_column);
            sleep(5);
            cls();
        }
        
        else if (menu_choice == 2)
        {
            cls();
            subtract_matrix(usermatrix1, usermatrix2, _1matrix_row, _1matrix_column, _2matrix_row, _2matrix_column);
            sleep(5);
            cls();
        }
        
        else if (menu_choice == 3)
        {
            cls();
            multiply_matrix(usermatrix1, usermatrix2, _1matrix_row, _1matrix_column, _2matrix_row, _2matrix_column);
            sleep(5);
            cls();
        }
        
        else if (menu_choice == 4)
        {
            cls();
            clearmatrix(usermatrix1, _1matrix_row, _1matrix_column);
            clearmatrix(usermatrix2, _2matrix_row, _2matrix_column);
            goto thebigbang;
        }
        else if (menu_choice == 5)
        {
            clearmatrix(usermatrix1, _1matrix_row, _1matrix_column);
            clearmatrix(usermatrix2, _2matrix_row, _2matrix_column);
            return 0;
        }
    }
    while (menu_choice != 5);
}

void promptnew(int& _1matrix_row, int& _1matrix_column, int& _2matrix_row, int& _2matrix_column)
{
    do
    {
        cout << "Enter the number of rows and columns in the first matrix, one by one, with enters in between: ";
        cin >> _1matrix_row >> _1matrix_column;
        if (_1matrix_column <= 0 || _1matrix_row <= 0)
        {
            cout << "Enter a valid number of rows and columns" << endl;
            sleep(3);
            cls();
        }
        
    }
    while (_1matrix_column < 1 || _1matrix_row < 1);
    
    do
    {
        cout << "Enter the number of rows and columns in the second matrix, one by one, with enters in between: ";
        cin >> _2matrix_row >> _2matrix_column;
        if (_2matrix_column <= 0 || _2matrix_row <= 0)
        {
            cout << "Enter a valid number of rows and columns" << endl;
            sleep(3);
            cls();
        }
    }
    while (_2matrix_column < 1 || _2matrix_row < 1);
}

int** creatematrix(int column, int row)
{
    int** matrix = new int* [row];
    for (int count = 0; count < row; count++)
    {
        matrix[count] = new int [column];
    }
    return matrix;
}

void fillmatrix(int** matrix, int row, int column, short a)
{
    for (int rowcount = 0; rowcount < row; rowcount++)
    {
        for (int colcount = 0; colcount < column; colcount++)
        {
            cout << "Filling the " << a << " matrix\n";
            cout << "Enter the value of the " << (rowcount + 1) << " row and the " << (colcount+1)
                 << " column: \n";
            cin >> *(*(matrix+rowcount)+colcount);
        }
    }
}

void displaymatrix(int** matrix, int row, int column)
{
    for (int rowcount = 0; rowcount < row; rowcount++)
    {
        for (int colcount = 0; colcount < column; colcount++)
        {

            cout << *(*(matrix+rowcount)+colcount) << "\t";
        }
        cout << "\n";
    }
}

void clearmatrix(int** removal_que, int row, int column)
{
    for (int process = (row-1); process >= 0; process--)
    {
        delete [] removal_que[process];
    }
    removal_que = NULL;
}

void add_matrix(int** mat1, int** mat2, int row1, int col1, int row2, int col2)
{
    if (row1 != row2 || col1 != col2)
    {
        cout << "MAJOR ERR: You must enter valid matrice dimensions, both rows and columns must be equal for addition\n";
        sleep(5);
        clearmatrix(mat1, row1, col1);
        clearmatrix(mat2, row2, col2);
        exit(1);
    }
    int** answer = creatematrix(col1, row1);
    for (int rowcount = 0; rowcount < row1; rowcount++)
    {
        for (int colcount = 0; colcount < col1; colcount++)
        {
            
            *(*(answer+rowcount)+colcount) = *(*(mat1+rowcount)+colcount) + *(*(mat2+rowcount)+colcount);
        }
    }
    cout << "The answer is:  \n\t--Solution Matrix--\n";
    displaymatrix(answer, row1, col2);
    
}

void subtract_matrix(int** mat1, int** mat2, int row1, int col1, int row2, int col2)
{
    if (row1 != row2 || col1 != col2)
    {
        cout << "MAJOR ERR: You must enter valid matrice dimensions, both rows and columns must be equal for addition\n";
        sleep(5);
        clearmatrix(mat1, row1, col1);
        clearmatrix(mat2, row2, col2);
        exit(1);
    }
    int** answer = creatematrix(col1, row1);
    for (int rowcount = 0; rowcount < row1; rowcount++)
    {
        for (int colcount = 0; colcount < col1; colcount++)
        {
            
            *(*(answer+rowcount)+colcount) = *(*(mat1+rowcount)+colcount) - *(*(mat2+rowcount)+colcount);
        }
    }
    cout << "The answer is:  \n\t--Solution Matrix--\n";
    displaymatrix(answer, row1, col2);
    
}

void multiply_matrix(int** mat1, int** mat2, int row1, int col1, int row2, int col2)
{
    if (col2 != row1)
    {
        cout << "MAJOR ERR: You must enter valid matrice dimensions, both rows and columns must be equal for addition\n";
        sleep(5);
        clearmatrix(mat1, row1, col1);
        clearmatrix(mat2, row2, col2);
        exit(1);
    }
    int** answer = creatematrix(col2, row1);
    for (int rowcount = 0; rowcount < row1; rowcount++)
    {
        for (int colcount = 0; colcount < col2; colcount++)
        {
            for (int cound = 0; cound < col2; cound++)
            {
                *(*(answer+rowcount)+colcount) += ( *(*(mat1+rowcount)+cound) * *(*(mat2+cound)+colcount) );
            }
        }
    }
    cout << "The answer is:  \n\t--Solution Matrix--\n";
    displaymatrix(answer, row1, col2);
}

void cls()
{
    for (short count =1; count <= 100; count++)
    {
        cout << "\n";
    }
}
The elements of the matrix has not been initialized.
Topic archived. No new replies allowed.