Reputation: 71
i am beginner with c++ and i need to make a program to multiply two matrices. I already understand the array of array concept in order to make a dynamic matrix. The problem that im an facing after a make and fill the matrix is that i cannot access it. it suddenly stops when i run the program and just finished filling the second array with the function:
void read_matrix(int** matrix, int row, int col)
{
cout << "Enter a matrix\n";
matrix = new int*[row];
for(int i = 0; i < row; i++)
matrix[i] = new int[col];
if (!matrix){
cerr << "Can't allocate space\n";
}
for(int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
cin >> matrix[i][j];
}
}
}
but according to my compiler, after the program stops there is an arrow pointing after the last loop of this function
void multiply_matrix(int** matrix1, int rows1, int cols1, int** matrix2, int rows2, int cols2, int** result)
{
for(int i = 0; i < rows1; i++){
for(int j = 0; j < cols2; j++){
for (int k = 0; k < rows2; k++){
result[i][j] += matrix1[i][k] * matrix2[k][j];
}
}
}
my main function is
int main ()
{
//matrices and dimensions
int rows1, cols1, rows2, cols2;
int **matrix1 = 0, **matrix2 = 0, **result = 0;
//TODO: readin matrix dimensions
cout << "Enter matrix dimensions \n";
cin >> rows1 >> cols1 >> rows2 >> cols2;
if(cols1 != rows2){
cout << "Error!";
terminate();
}
//memory for result matrix
result = new int*[rows1];
for(int i = 0; i < rows1; i++)
result[i] = new int[cols2];
// Read values from the command line into a matrix
read_matrix(matrix1, rows1, cols1);
read_matrix(matrix2, rows2, cols2);
// Multiply matrix1 one and matrix2, and put the result in matrix result
multiply_matrix(matrix1, rows1, cols1, matrix2, rows2, cols2, result);
print_matrix(result, rows1, cols2);
//TODO: free memory holding the matrices
return 0;
}
i can not get why it does not work. what i think is there is something wrong in the way i fil the matrix or o do something wrong in the way i send one matrix from one function to the other.
Thanks,
David
Upvotes: 2
Views: 1265
Reputation: 66371
Your first problem is that you're not allocating the result
matrix.
Your second problem is that you're not allocating the other matrices either.
The read_matrix
assigns the allocated memory to the parameter you pass it.
Unfortunately, that parameter is a copy of the pointer in main
, so the effect is local to the function.
To fix it, you can pass a reference to the variable you wish to assign to:
void read_matrix(int**& matrix, int row, int col);
or, better, just return the correct value from the function:
int** read_matrix(int row, int col);
// ...
matrix1 = read_matrix(rows1, cols1);
On a side note: you only need to input two dimensions - if the first matrix is M x N, the other must be N x M.
Upvotes: 1
Reputation: 15872
Instead of (incorrectly) allocating and (not) deallocating your memory manually, you should use either std::array
or 'std::vector
for your arrays. Since you are pulling in the matrix sizes dynamically, that would lead you to std::vector
.
Simple example:
std::vector<std::vector<int> > matrix;
for (int i = 0; i < ROW_COUNT; i++)
{
std::vector<int> row;
for (int j = 0; j < COL_COUNT; j++)
{
row.push_back(RECORD[i][j]);
}
matrix.push_back(row);
}
There are ways to do it in a single line of code as well, but I'll leave it like this to demonstrate the basic idea.
Upvotes: 0
Reputation: 37122
You're not allocating your result
matrix anywhere, so your multiply_matrix
function is dereferencing a null pointer and writing to random memory.
Upvotes: 1