c - 矩阵乘法 C

标签 c arrays matrix-multiplication bus-error

我已经一遍又一遍地检查代码,一切都应该没问题,所以我不明白。我看过一个视频,内容几乎相同,代码也相同,但我一直收到 BUS 错误。

我尝试了一个 2x2 矩阵并且没有成功,但是当我做了一个 2x3 时却没有成功

#include<stdio.h>

void main(void)

 {
   int i,j,k,x,y; //i denotes rows, j denotes columns which depends on the matrix 
   int C[x][y]; //denotes the multiplied matrix

//declaring matrix A
   int A[2][3]={ {1,2,3},{4,5,6} };

   printf("MATRIX A\n");

   for(i=0; i<2; i++) //selecting the row
    {
        for(j=0; j<3; j++) // selecting the column
        {
            printf("%d|",A[i][j]);
        }
        printf("\n\n"); //to declare the spacing 
    }


//declaring matrix B
   int B[3][2]={ {7,8},{9,10},{11,12} };

   printf("MATRIX B\n");

    for(i=0; i<3; i++) //selecting the row
        {
            for(j=0; j<2; j++) // selecting the column
            {
             printf("%3d|",B[i][j]);
            }
            printf("\n\n");
         }


//multiplying the A & B matrix
   printf("MULTIPLICATION OF MATRIX A & B\n");

     for(x=0; x<2; x++)
        {
            for(y=0; y<2; y++)
            {
                for(k=0;k<3;k++)
                {
                    C[x][y] = C[x][y] + A[x][k]*B[k][y];
                }
            }          
        }
    for(x=0; x<2; x++)
        {
            for(y=0; y<2; y++)
            {
                printf("%3d|", C[x][y]);
            }
            printf("\n");
        }
}

它应该简单地将 2 个矩阵相乘

最佳答案

正如其他人在评论中提到的,您的错误是您试图使用未初始化的变量作为维度声明一个可变长度数组。

我建议将矩阵运算放入库函数中,而不是重复自己。您可以通过将维度作为参数传递来使它们与可变数组形状一起使用,如下所示:

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>

#define M 2
#define N 3
#define P 2

void int2d_print( ptrdiff_t m, ptrdiff_t n, const int a[m][n] )
/* A bare-bones routine to print matrices containing small integers
 * to stdout.
 */
{
  // Sanity-checking the parameters:
  assert(m > 0);
  assert(n > 0);
  assert(a);

  for ( ptrdiff_t i = 0; i < m; ++i ) {
    fputc( '[', stdout );

    for ( ptrdiff_t j = 0; j < n; ++j ) {
      printf( " %4d", a[i][j] );
    }

    fputs( " ]\n", stdout );
  }
  fputc( '\n', stdout );
}

int* int2d_mul( ptrdiff_t m, ptrdiff_t n, ptrdiff_t p,
                const int a[m][n],
                const int b[n][p],
                int c[m][p] )
/* Sets the array c = ab.  Returns (int*)c.
 */
{
  // Sanity-checking the parameters:
  assert(m > 0);
  assert(n > 0);
  assert(p > 0);
  assert(a);
  assert(b);
  assert(c);

 /* There are better algorithms than this, and it is a good candidate for
  * parallelization.
  */
  for( ptrdiff_t i = 0; i < m; ++i )
    for ( ptrdiff_t j = 0; j < p; ++j ) {
      int x = 0;
      for ( ptrdiff_t k = 0; k < n; ++k ) {
        x += a[i][k] * b[k][j];
      }
      c[i][j] = x;
    }

  return (int*)c;
}

// Test driver for the previous functions:
int main(void)
{
  // Declaring these static is redundant in this context.  
  static const int a[M][N]={ {1,2,3},{4,5,6} };
  static const int b[N][P]={ {7,8},{9,10},{11,12} };
  static int c[M][P];

  printf("MATRIX B\n");
  int2d_print( M, N, a );

  printf("MATRIX B\n");
  int2d_print( N, P, b );

  printf("PRODUCT OF A & B\n");
  int2d_mul( M, N, P, a, b, c );
  int2d_print( M, P, c );

  return EXIT_SUCCESS;
}

我个人更喜欢将 ptrdiff_t 用于数组下标,因为它的宽度正确,更容易检测上溢和下溢,并避免像臭名昭著的 -3 > 1U< 这样的转换错误。您可以轻松更改它以匹配您自己的编码风格。

关于c - 矩阵乘法 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57366678/

相关文章:

c - C语言中如何找到大字符串中的小字符串?

performance - 不同优化下的 Fortran 矩阵乘法性能

python - NumPy/SciPy 中的多线程整数矩阵乘法

c程序,通过字符串递增

C - 合并排序的合并部分

c - 在 C 中对数字进行排序

3d - 如何用任何编程语言将两个 3D 矩阵相互相乘

c - 重复使用相同的线程

java - 在 Java 和 C 之间发送 int[]s

c - 用C中的信号灯和信号同步过程