在c中使用结构体对数组进行变量赋值的问题

发布于 2024-09-18 15:17:09 字数 1324 浏览 4 评论 0原文

所以我试图用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

优雅的叶子 2024-09-25 15:17:09

您需要释放您的对象。您需要重置tempsol。但最重要的是,您需要检查您的 mat_mult()

matrix_t mat_mult(matrix_t a, matrix_t b)
{
 /* ... */
 if(a -> c == b -> r)
 {
  /* ... */
 }
 else if(a -> r == b -> c)
 {
                                    /* BZZZZT!                 */
  answer = mat_new(a -> c, b -> r); /* BZZZZT! mat_mult(b, a); */
                                    /* BZZZZT!                 */
  return answer;
 }
 else
 {
  /* ... */
 }
}

You need to free your objects. You need to reset tempsol. But most importantly, you need to review your mat_mult().

matrix_t mat_mult(matrix_t a, matrix_t b)
{
 /* ... */
 if(a -> c == b -> r)
 {
  /* ... */
 }
 else if(a -> r == b -> c)
 {
                                    /* BZZZZT!                 */
  answer = mat_new(a -> c, b -> r); /* BZZZZT! mat_mult(b, a); */
                                    /* BZZZZT!                 */
  return answer;
 }
 else
 {
  /* ... */
 }
}
北斗星光 2024-09-25 15:17:09

似乎您的所有问题都源于将矩阵值读取为整数而不是双精度数。如果将 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.

街道布景 2024-09-25 15:17:09

这应该适用于您的示例:

matrix_t mat_new(int r,int c)
{
  matrix_t new = malloc(sizeof*new);
  new->r   = r;
  new->c   = c;
  new->mat = malloc( r*c*sizeof(double) );
  return new;
}

This should work for your example:

matrix_t mat_new(int r,int c)
{
  matrix_t new = malloc(sizeof*new);
  new->r   = r;
  new->c   = c;
  new->mat = malloc( r*c*sizeof(double) );
  return new;
}
怕倦 2024-09-25 15:17:09

您的代码不包含任何明显的错误。也许问题出在你的 mat_new() 上。您在矩阵结构中将 mat 定义为 double **mat; 的方式(我不推荐)可能会导致一些问题。

要将 2x2 矩阵分配给 mat,您需要执行以下操作:

mat = new (double*)[2];
mat[0] = new double[2];
mat[1] = new double[2];

或 m 矩阵:

mat = new (double*)[n];
for (int i=0;i<n;i++) {
  mat[i] = new double[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:

mat = new (double*)[2];
mat[0] = new double[2];
mat[1] = new double[2];

or a n by m matrix:

mat = new (double*)[n];
for (int i=0;i<n;i++) {
  mat[i] = new double[m];
}

Is this what you are doing?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文