2

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

4

3 回答 3

1

您的第一个问题是您没有分配result矩阵。

您的第二个问题是您也没有分配其他矩阵。

read_matrix分配的内存分配给您传递给它的参数。
不幸的是,该参数是in 指针的副本main,因此效果是函数本地的。

要修复它,您可以传递对要分配给的变量的引用:

void read_matrix(int**& matrix, int row, int col);

或者,更好的是,只从函数中返回正确的值:

int** read_matrix(int row, int col);
// ...
matrix1 = read_matrix(rows1, cols1);

附带说明:您只需要输入两个维度 - 如果第一个矩阵是 M x N,另一个必须是 N x M。

于 2013-09-10T20:38:20.607 回答
1

您没有在result任何地方分配矩阵,因此您的multiply_matrix函数正在取消引用空指针并写入随机内存。

于 2013-09-10T20:21:06.130 回答
0

而不是(不正确地)手动分配和(不)释放内存,您应该使用std::arrayor 或 'std::vector为您的数组。由于您正在动态拉入矩阵大小,这将导致您std::vector.

简单的例子:

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);
}

也有一些方法可以在一行代码中完成,但我将保持这样的方式来演示基本思想。

于 2013-09-10T20:38:02.617 回答