I wrote an algorithm for matrix multiplication but I have a compiler error "Unhandled exception at 0x0FE8591C (msvcp140d.dll) in matriscarpim.exe: 0xC0000005: Access violation reading location 0xCDCDCDCD. occurred"
does anyone can help me?
all codes here.
int **matrix1 = newint *[a];
matrix1[a] = newint[b];
The first line creates an array of a int pointers, OK.
The second line creates one array of b integers assigns the array pointer to matrix1[a] which is out of bounds because the last valid index is a-1 (indexes start at zero).
If you want each pointer in the array to point to its own array of b integers you will have to use a loop and allocate a arrays that you assign to the a array elements.
thank you. actually I want to make a program which asks user to enter size of the matrix. it means I want to create dynamic matrices but it does not work that way.
What are aand b? The sizes of your matrix? Because, if so, matrix1[a][b] and matrix2[a][b] are beyond the end of your matrix; you're attempting to access memory that is being used for something else.
thank you. It works but incorrect :(
this is programs output
please enter number of rows and columns
2
2
enter value of matrix one
enter value of matrix[0][0] :4
enter value of matrix[0][1] :5
enter value of matrix[1][0] :8
enter value of matrix[1][1] :7
enter value of matrix two
enter value of matrix[0][0] :6
enter value of matrix[0][1] :4
enter value of matrix[1][0] :1
enter value of matrix[1][1] :5
-168958497
-168958497
-168958497
-168958497
@bsr, please post your latest code BELOW THIS POST - full compileable version. There is no way anyone can offer advice on matrix multiplication without seeing it. Your original code was multiplying things that hadn't even been initialised.
#include <iostream>
usingnamespace std;
void matrixMult(int** matrix1, int** matrix2, int a, int b);
int main() {
int a, b;
cout<<"please enter number of rows and columns"<<endl;
cin >> a;
cin >> b;
int **matrix1 = newint *[a];
for (int i = 0; i < a; ++i)
{
matrix1[i] = newint[b];
}
int **matrix2 = newint *[a];
for (int i = 0; i < a; ++i)
{
matrix2[i] = newint[b];
}
cout << "enter value of matrix one" << endl;
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
cout<<"enter value of matrix["<<i<<"][" <<j<< "] :";
cin >> matrix1[i][j];
}
}
cout << "enter value of matrix two" << endl;
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
cout << "enter value of matrix[" << i << "][" << j << "] :";
cin >> matrix2[i][j];
}
}
matrixMult(matrix1, matrix2, a, b);
int n;
cin >> n;
return 0;
}
void matrixMult(int** matrix1, int** matrix2, int a, int b) {
int **mult = newint *[a];
for (int i = 0; i < a; ++i)
{
mult[i] = newint[b];
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
for (int k = 0; k < a; k++) {
mult[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
cout << mult[i][j]<<endl;
}
}
}
You never initialize the matrix elements. If you want them to be zero-initialized you can add () or {} after the square brackets when creating the integer arrays.
mult[i] = newint[b]{}; // allocates an array of b ints that are initialized to zero.