Actually very confused trying to do this, and it throwing this error doesn't help much.
the Matrices I try to enter to recreate issue are:
Mat1 = {3} (1 row x 1 column) Mat2 = {1 2 3} (1 row x 3 columns)
The code functioned fine when i tried 2 (2x2) arrays, I think the issue lies within the for lope with interval "k" when trying to set values of mat3.
using namespace std;
#include <iostream>
#include <cstdlib>
int c1, c2, r1, r2;
void multiply(int c1, int r2, int c2, int r1, int** mat1, int** mat2){
if (r1 != c2) {
return ;
}
else {
int c3 = r2;
int r3 = c1;
int** mat3 = new int* [c3];
for (int i = 0; i < c3; i++) {
mat3[i] = new int[r3];
}
for (int i = 0; i < c3; i++) {
for (int j = 0; j < r3; j++) {
for (int k = 0; k < c2; k++)
{
mat3[i][j] += mat1[i][k] * mat2[k][j];
}
}
}
cout << endl << "Matrix 3:" << endl;
for (int i = 0; i < c3; i++) {
for (int j = 0; j < r3; j++) {
cout << mat3[i][j] << " ";
}
cout << endl;
}
}
}
int main() {
cout << "Enter size of matrix 1 \nColumns then rows" << endl;
cin >> c1 >> r1;
int** mat1 = new int* [r1];
for (int i = 0; i < r1; i++) {
mat1[i] = new int[c1];
}
cout << "Enter values of matrix 1; left to right then top down" << endl;
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
cin >> mat1[i][j];
}
}
cout << "Enter size of matrix 2 \nColumns then rows" << endl;
cin >> c2 >> r2;
int** mat2 = new int* [r2];
for (int i = 0; i < r2; i++) {
mat2[i] = new int[c2];
}
cout << "Enter values of matrix 2; left to right then top down" << endl;
for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++) {
cin >> mat2[i][j];
}
}
cout << endl << "Matrix 1:" << endl;
for (int i = 0; i < r1; i++) {
for (int j = 0; j < c1; j++) {
cout << mat1[i][j] << " ";
}
cout << endl;
}
cout << endl << "Matrix 2:" << endl;
for (int i = 0; i < r2; i++) {
for (int j = 0; j < c2; j++) {
cout << mat2[i][j] << " ";
}
cout << endl;
}
multiply(r1, c2, r2, c1, mat1, mat2);
system("pause");
return 0;
}