C 使用指针进行矩阵乘法时出错
我正在尝试进行矩阵乘法优化,如下所示: 期望输出矩阵为C 使用指针进行矩阵乘法时出错,c,matrix,C,Matrix,我正在尝试进行矩阵乘法优化,如下所示: 期望输出矩阵为 matrix C 2 2 2 2 但对于如上所示的使用指针的矩阵乘法优化,输出是不同的 matrix C 0 0 7 9 也应该是 A= (int*)malloc(sizeof(int)*(n * n));//not sizeof(int*) B= (int*)malloc(sizeof(int)*(n * n)); C= (int*)calloc(n*n, sizeo
matrix C
2 2
2 2
但对于如上所示的使用指针的矩阵乘法优化,输出是不同的
matrix C
0 0
7 9
也应该是
A= (int*)malloc(sizeof(int)*(n * n));//not sizeof(int*)
B= (int*)malloc(sizeof(int)*(n * n));
C= (int*)calloc(n*n, sizeof(int));
矩阵可以看作是2D数组,这样您就可以使用数组寻址,这有助于读取代码 我会像这样实现动态数组乘法 这可能是一种可能的实现方法,可以将2个矩阵相乘并动态存储结果,我假设您可以通过命令行将矩阵输入到程序中
#include<stdio.h>
//this function returns a pointer to the new array.
int **makeMatrix(int,int);
int **makeMatrix(ROWS,COLS){
int i=0;
//your code u use sizeof(*int) this is wrong suppose you use a 64bit machine
//then sizeof(*int) is going to be 8bytes(64bits)!, whereas an integer can be 32bits,
//the size of the integer is completely compiler dependent, does not matter if you
//have 64bit hardware.
int **array1 = malloc(sizeof(int *)*ROWS);
for(i=0;i<Columns;i++)
array1[i] = malloc(sizeof(int)*COlUMNS);
return array1;
//we are making a 2D array here so we can use array notation to address the memory
//A[row][column] is the same as *(*(A + row) + column), this makes the code neat unlike
//your method which is also very correct but i prefer the old array notation.
}
int main(void){
int set_row1,set_row2,set_row3,set_column1,set_column2,set_column3;
int row1,column1,column;
int **array1 = makeMatrix(set_row1,set_column1);
int **array2 = makeMatrix(set_row2,set_column2);
int **result = makeMatrix(set_row3,set_column3);
//now we have 3 matrices that we can use//i assume you initialise them somehow
//with numbers
//now the multiplication part.we can use normal array notation since we are
// using essentialy a 2D array, your method is also corret where you allocate the
// entire array on a boxed piece of memory.
//also need to check if the 2 matrix can be multiplied.
int temp =0;
if(Column1 == Row2) //they can be multiplied together
{
for(row1=0;row1<set_row1;row1++){
for(column2=0;column2<set_column2;column2++){
for(column1=0;column1<set_column1;column1++){
temp += array1[row1][column1]*array2[column1][column2]
}
result[row1][column2] = temp;
}
}
else
//do something
#包括
//此函数返回指向新数组的指针。
int**makeMatrix(int,int);
int**makeMatrix(行、列){
int i=0;
//您的代码u使用sizeof(*int)假设您使用64位机器,这是错误的
//那么sizeof(*int)将是8字节(64位)!,而整数可以是32位,
//整数的大小完全取决于编译器,如果
//拥有64位硬件。
int**array1=malloc(sizeof(int*)*行);
对于(i=0;i如何显示您的预期输出以及您得到了什么?将int*
的倍数分配到int
应该在的位置,然后尝试访问int*
,就好像它是double*
?在退出最内部的循环并执行C[i*n+j]之后,考虑j
的值=sum
您应该看到逻辑错误。将矩阵大小都取为A=2*2,B=2*2,并且A和B中的每个元素值都取为1,答案应该是C[0]=2,C[1]=2,C[2]=2,C[3]=2。但答案不同
matrix C
0 0
7 9
sum=0;
for ( j = 0 ; j< n ; j++ )
{
double c =A[i*n + k];
double d =B[k*n + j];
sum+=c*d;
}
C[i*n + j] =sum;
for ( i = 0 ; i < n ; i++ ){
for ( k = 0 ; k < n ; k++ ){
for ( j = 0 ; j< n ; j++ ){
C[i*n + j] +=A[i*n + k]*B[k*n + j];//C initialized by 0
}
}
}
A= (int*)malloc(sizeof(int)*(n * n));//not sizeof(int*)
B= (int*)malloc(sizeof(int)*(n * n));
C= (int*)calloc(n*n, sizeof(int));
#include<stdio.h>
//this function returns a pointer to the new array.
int **makeMatrix(int,int);
int **makeMatrix(ROWS,COLS){
int i=0;
//your code u use sizeof(*int) this is wrong suppose you use a 64bit machine
//then sizeof(*int) is going to be 8bytes(64bits)!, whereas an integer can be 32bits,
//the size of the integer is completely compiler dependent, does not matter if you
//have 64bit hardware.
int **array1 = malloc(sizeof(int *)*ROWS);
for(i=0;i<Columns;i++)
array1[i] = malloc(sizeof(int)*COlUMNS);
return array1;
//we are making a 2D array here so we can use array notation to address the memory
//A[row][column] is the same as *(*(A + row) + column), this makes the code neat unlike
//your method which is also very correct but i prefer the old array notation.
}
int main(void){
int set_row1,set_row2,set_row3,set_column1,set_column2,set_column3;
int row1,column1,column;
int **array1 = makeMatrix(set_row1,set_column1);
int **array2 = makeMatrix(set_row2,set_column2);
int **result = makeMatrix(set_row3,set_column3);
//now we have 3 matrices that we can use//i assume you initialise them somehow
//with numbers
//now the multiplication part.we can use normal array notation since we are
// using essentialy a 2D array, your method is also corret where you allocate the
// entire array on a boxed piece of memory.
//also need to check if the 2 matrix can be multiplied.
int temp =0;
if(Column1 == Row2) //they can be multiplied together
{
for(row1=0;row1<set_row1;row1++){
for(column2=0;column2<set_column2;column2++){
for(column1=0;column1<set_column1;column1++){
temp += array1[row1][column1]*array2[column1][column2]
}
result[row1][column2] = temp;
}
}
else
//do something