# How to use double point to handle matrix?

0

I want to implement the matrix multiplication in c++.And here is my code:

``````
#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;
}
}
``````

However, it report the Access violation reading location when I run the line: 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.
``````

It seems that my second pointer indexs wrong place. How to handle the problem? Please help me. Thanks.

c++
matrix

2

In this line:

``````int** matrix_A_point_final = &matrix_A_point;
``````

This doesn't do what you think. Your stack matricies (`matrix_A` and `matrix_B`) are contiguous numbers representing all the rows and columns.

Your dynamic matricies are rows of pointers to columns. These are different things, and you are conflating those with bad results.

Note that using raw allocation is generally frowned upon, and using smart pointers, vectors or wrapper classes would be a much better idea and help avoid this issue.

However, a quick fix would be creating `matrix_A` and `matrix_B` using `create_matrix` and filling those in manually.

Also this line:

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

Presumably should be:

``````new_matrix[i][j] += matrix1[i][m1_col] * matrix2[m1_col][j];
``````
answered on Stack Overflow May 4, 2020 by Mike Vine
0
``````#include<iostream>
``````

# include

using namespace std;

int main() {

``````int** A;
int* B;
int Row, Col;

cout<<"Enter Row:";
cin>>Row;
cout<<"Enter column:";
cin>>Col;

A = new int* [Row];
B = new int[Row];

for(int i = 0; i < Row; i++)
{

A[i] = new int [Col];

}

cout<<"OK!\nEnter:";

for(int j = 0; j < Row; j++)
for(int k = 0; k < Col; k++)
{

cin>>A[j][k];
cout<<"\n";

}

cout<<"\n";

for(int j = 0; j < Row; j++)
{
for(int k = 0; k < Col; k++)
{

cout<<A[j][k]<<"  ";

}

cout<<endl;

}

getch();
return 0;
``````

}

answered on Stack Overflow May 4, 2020 by behniya • edited May 4, 2020 by behniya

User contributions licensed under CC BY-SA 3.0