在c中使用结构体对数组进行变量赋值的问题
所以我试图用c 来乘以矩阵。然而,当我尝试将两个数组中的数字相乘,并将它们放入答案数组中时,它始终为零。这是该方法的代码,谢谢。
我的矩阵结构:
typedef struct matrix {
int r;
int c;
double **mat;
} *matrix_t;
我的矩阵乘法:
matrix_t mat_mult(matrix_t a, matrix_t b)
{
int i, j, k;
double x, temp1, temp2;
double tempsol = 0.0;
x = temp1 = temp2 = 0;
matrix_t answer;
if(a -> c == b -> r)
{
answer = mat_new(a -> r, b -> c);
for(i = 0; i < a -> r; i++)
for( j = 0; j < b -> c; j++)
{
for( k = 0; k < a -> c; k++)
{
tempsol += a->mat[i][k] * b->mat[k][j];
answer-> mat[i][j] = tempsol;
}
}
return answer;
}
else if(a -> r == b -> c)
{
answer = mat_new(a -> c, b -> r);
return answer;
}
else
{
printf("Matrices could not be multiplied");
exit(1);
return;
}
}
这也是我的 mat_new 的代码
matrix_t mat_new(int r,int c)
{
int i = 0;
double **a;
matrix_t matrix_a;
a = (double**)malloc(r *sizeof(double *));
for(i = 0; i < r; i++)
{
a[i] = (double*)malloc(c *sizeof(double));
}
matrix_a = (matrix_t) malloc ( sizeof(struct matrix));
matrix_a -> mat = a;
matrix_a -> r = r;
matrix_a -> c = c;
return matrix_a;
}
So I'm trying to multiply matrices in c. However when I try to multiply the numbers in the two arrays, and put them in an answer array its always zero. heres the code for the method, thanks.
My matrix struct:
typedef struct matrix {
int r;
int c;
double **mat;
} *matrix_t;
My matrix multiplying method:
matrix_t mat_mult(matrix_t a, matrix_t b)
{
int i, j, k;
double x, temp1, temp2;
double tempsol = 0.0;
x = temp1 = temp2 = 0;
matrix_t answer;
if(a -> c == b -> r)
{
answer = mat_new(a -> r, b -> c);
for(i = 0; i < a -> r; i++)
for( j = 0; j < b -> c; j++)
{
for( k = 0; k < a -> c; k++)
{
tempsol += a->mat[i][k] * b->mat[k][j];
answer-> mat[i][j] = tempsol;
}
}
return answer;
}
else if(a -> r == b -> c)
{
answer = mat_new(a -> c, b -> r);
return answer;
}
else
{
printf("Matrices could not be multiplied");
exit(1);
return;
}
}
heres the code for my mat_new as well
matrix_t mat_new(int r,int c)
{
int i = 0;
double **a;
matrix_t matrix_a;
a = (double**)malloc(r *sizeof(double *));
for(i = 0; i < r; i++)
{
a[i] = (double*)malloc(c *sizeof(double));
}
matrix_a = (matrix_t) malloc ( sizeof(struct matrix));
matrix_a -> mat = a;
matrix_a -> r = r;
matrix_a -> c = c;
return matrix_a;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要
释放
您的对象。您需要重置tempsol
。但最重要的是,您需要检查您的mat_mult()
。You need to
free
your objects. You need to resettempsol
. But most importantly, you need to review yourmat_mult()
.似乎您的所有问题都源于将矩阵值读取为整数而不是双精度数。如果将 read_mat() 中的 temp 更改为 int,然后将其放入矩阵中时将其转换为 double,则一切正常。
Seems like all your issues stem from reading in matrix values as integers rather than doubles. Everything works fine if you change temp in read_mat() to an int, then cast it to a double when you're putting it in the matrix.
这应该适用于您的示例:
This should work for your example:
您的代码不包含任何明显的错误。也许问题出在你的 mat_new() 上。您在矩阵结构中将 mat 定义为 double **mat; 的方式(我不推荐)可能会导致一些问题。
要将 2x2 矩阵分配给 mat,您需要执行以下操作:
或 m 矩阵:
这是您正在做的吗?
Your code doesn't contain any obvious errors. Perhaps the problem lies in your mat_new(). The way you defined mat in your matrix structure as
double **mat;
, which I wouldn't recommend, may be causing some problems.To allocate a 2x2 matrix to mat, you would need to do:
or a n by m matrix:
Is this what you are doing?