如何使用双点处理矩阵?

问题描述 投票:0回答:1

我想在c ++中实现矩阵乘法。这是我的代码:


#include"math.h";
#include <iostream>

using namespace std;
int** create_matrix(int rows, int cols);
int** calculate_matrix(int** matrix1, int** matrix2, int row_1, int col_1, int row_2, int col_2);
void printMatrix(int** matrix, int rows, int cols);

void main(int argc, char** argv) {
int matrix_A[3][2] = { {1,2},{3,4},{5,6} };
    int matrix_B[2][3] = { {1,2,5},{3,4,6} };

    int* matrix_A_point = &matrix_A[0][0];
    int* matrix_B_point = &matrix_B[0][0];

    int** matrix_A_point_final = &matrix_A_point;
    int** matrix_B_point_final = &matrix_B_point;

    int** new_matrix = calculate_matrix(matrix_A_point_final, matrix_B_point_final, 3, 2, 2, 3);

    printMatrix(new_matrix, 3, 3);
}



int** create_matrix(int rows, int cols) {
    //create the new matrix
    int** matrix = new int* [rows];
    for (int i = 0; i < rows; i++) {
        matrix[i] = new int[cols];
    }

    return matrix;


}


int** calculate_matrix(int** matrix1, int** matrix2,int row_1,int col_1,int row_2,int col_2) {
    //multiplication the matrixs
    int** new_matrix = create_matrix(row_1, col_2);
    if (col_1 == row_2) {
        cout << "The two matrixs can multiplicate!" << endl;

        for (int i = 0; i < row_1; i++) {
            for (int j = 0; j < col_2; j++) {

                new_matrix[i][j] = 0;
                for (int m1_col = 0; m1_col < col_1; m1_col++) {
                    new_matrix[i][j] = matrix1[i][m1_col] * matrix2[m1_col][j];
                }
            }
        }


    }
    else {
        cout << "The two matrixs can't multiplicate!" << endl;

    }
    return new_matrix;



}

void printMatrix(int** matrix, int rows, int cols) {

    for (int i = 0; i < rows; ++i) {
        cout << "the line of: " << i;
        for (int j = 0; j < cols; ++j) {
            cout << matrix[i][j] << " ";
        }
        cout << endl;
    }
}

但是,当我运行该行时,它将报告访问冲突读取位置:new_matrix [i] [j] = matrix1 [i] [m1_col] * matrix2 [m1_col] [j];

Exception thrown at 0x00007FF6F6DA4997 in Assessment_2.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

Unhandled exception at 0x00007FF6F6DA4997 in Assessment_2.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

Exception thrown at 0x00007FF6F6DA4997 in Assessment_2.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

Unhandled exception at 0x00007FF6F6DA4997 in Assessment_2.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

Exception thrown at 0x00007FF6F6DA4997 in Assessment_2.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

Unhandled exception at 0x00007FF6F6DA4997 in Assessment_2.exe: 0xC0000005: Access violation reading location 0xFFFFFFFFFFFFFFFF.

似乎我的第二个指针索引了错误的位置。该如何处理?请帮我。谢谢。

c++ matrix
1个回答
2
投票

在此行:

int** matrix_A_point_final = &matrix_A_point;

这不符合您的想法。您的堆栈矩阵(matrix_Amatrix_B)是代表所有行和列的连续数字。

您的动态矩阵是pointers到列的行。这些是不同的事情,您将这些结果混淆在一起。

请注意,通常不赞成使用原始分配,而使用智能指针,向量或包装器类将是一个更好的主意,并且有助于避免此问题。

但是,快速解决方案是使用matrix_A创建matrix_Bcreate_matrix并手动填写。

也是此行:

new_matrix[i][j] = matrix1[i][m1_col] * matrix2[m1_col][j];

大概应该是:

new_matrix[i][j] += matrix1[i][m1_col] * matrix2[m1_col][j];
© www.soinside.com 2019 - 2024. All rights reserved.