Home > Back-end >  Rookie help you how to use the int * * MatrixMult (int * * A, int m, int n, int * * B, int p, int q)
Rookie help you how to use the int * * MatrixMult (int * * A, int m, int n, int * * B, int p, int q)

Time:09-18

The teacher the assignment, for totally have no idea how to write the self-defined function, hope great god taught, the grateful

Declare a 3 x3 2 d integer array, int iarray [3], [3], and initialize the values for all elements.
Iarray [3] [3]=
1 2 3
0 1 4
5 6 0

Write a function to perform integer matrix multiplication and the prototype of the function should be
As follows,
Int * * MatrixMult (int * * A, int m, int n, int * * B, int p, int q);
You should check The if n equals to The p in order to perform The matrix multiplication, The function should return
A 2 d integer array with MXQ.
Declare another 3 x3 integer arrays, int B [3], [3], and initialize them as follows,
B [3] [3]=
? 24 18 5
20? 15? 4
? 5 4 1
Call MatrixMult function to perform matrix multiplication of these two 3 x3 integer arrays declared
Above.

CodePudding user response:

With matrix formula can, two loops can solve

CodePudding user response:

Matrix multiplication? For a long time didn't also specially to how multiplication is searched by:
Deal with a problem, I are generally analyzed in own logic only then to code implementation
1. First we understand how matrix multiplication, the following excerpt from baidu encyclopedia:
Matrix multiplication is the most important method is the product of general matrix, it is only in the first column of the matrix number (column) and the second number of rows of the matrix (row) makes sense at the same time, the general single refers to the product of matrix, the mean is the product of general matrix, a matrix is m m * n * n number arranged in m rows n columns a number of array
2. Learn from the above, the first step we need to judge the first number of columns of the matrix (column) and the second number of rows of the matrix (row) are equal, equal to the next step, before You should tip error, has also mentioned this point looks like You should check the if n equals to the p in order to perform the matrix multiplication, is to determine whether n and q equal to
3.
The topic given in the function prototype:
Int * * MatrixMult (int * * A, int m, int n, int * * B, int p, int q);
First of all, we declare a two-dimensional array, is line with m, q for column
Function prototypes did not give the return value of the incoming, to avoid the stack are popping up in the use of the return value have unpredictable consequences, can use the new or malloc dynamic application of the two-dimensional array memory
4. Product line the first m C n columns of the elements is equal to the matrix A of the first m lines of elements and matrix B n column corresponding to the sum of product of elements:
for(int i=0; i{
For (int j=0; J{
For (int k=0; K{
[I] [j] + C=A [I] [j] * B [j] [I];//A in the ith row j column with B j line in I column elements multiplication
}
}
}


CodePudding user response:

I think with two dimensional pointer to operate two dimensional array is not possible, and should use the array pointer (int (* A) [3]) or one dimensional pointer (int * A) to operate the two-dimensional array,
Because of two-dimensional Pointers don't know how many columns you a line, so I can't to manipulate, the two dimensional array only array pointer, or one dimensional pointer can
The array pointer operation just like using A two-dimensional array is A [line number] [column number], in the form of the one-dimensional array is * (A + line number * number of columns, column number)
In this way can correct operation two-dimensional arrays,
Don't know how your teacher successfully using two-dimensional pointer to a two dimensional array of operation, so I'm interested in the question the answer is, if you have the answer, please also let me refer to, add some knowledge,
  • Related