我想将两个多维数组相乘以形成一个矩阵。我有这个功能。这应该在理论上起作用。但是,我只是得到0和大/笨拙的数字。有人可以帮我弄这个吗?在C中乘以两个数组
int **matrix_mult(int **a, int **b, int nr1, int nc1, int nc2)
{
int **c;
int i,j,k,l;
c = malloc(sizeof(int *)*nr1);
if (c == NULL){
printf("Insuff memm");
}
for(l=0;l<nr1;l++){
c[l] = malloc(sizeof(int)*nc1);
if (c[l] == NULL){
printf("Insuff memm");
}
}//for loop
for (i=0;i<nr1;i++){
for (j=0;j<nc2;j++){
for (k=0;k<nc1;k++){
c[i][j] = (a[i][k]) * (b[k][j]);
}
}
}
return(c);
}
一个main()和一些示例矩阵的最小示例将得心应手 – Spacedman 2010-12-10 12:12:20
与您的问题并不真正相关,但是当malloc失败时,您不应该仅仅打印“Insuff mem”,您应该至少停止使用未分配的空间。 – buddhabrot 2010-12-10 12:22:36
@ user373466,我已经花了更多的时间在这方面比我应该有。一票或两票和接受将不胜感激。 – AlastairG 2010-12-10 14:27:19