1

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;
}
zomy2000
  • 73
  • 4
  • 1
    `int** mat3 = new int* [c3];` - have you ever considered using some containers and/or smart pointers? What you just wrote there *might* be ok as C++98, but in 2019 is is really *not* how you'd do it.. – Jesper Juhl Dec 25 '19 at 17:15
  • `system("pause");` <-- not portable. Don't do that if you want your code to be usable across different operating systems. Even when `system` is available, don't use it - it's a security nightmare. – Jesper Juhl Dec 25 '19 at 17:19
  • 3
    0xFDFDFDFD is _Used by Microsoft's C/C++ debug malloc() function to mark "no man's land" guard bytes before and after allocated heap memory,[23] and some debug Secure C-Runtime functions implemented by Microsoft (e.g. strncat_s)_ you are going out of bounds. – ChrisMM Dec 25 '19 at 17:21
  • The following link shows the debugging magic codes: [https://stackoverflow.com/a/127404/487892](https://stackoverflow.com/a/127404/487892) – drescherjm Dec 25 '19 at 17:33
  • It seems to be some confusion with notation: here c1 corresponds to the number of rows and r1 to the number of columns, when looking at implementation of the multiplication – Damien Dec 25 '19 at 20:17

0 Answers0