矩阵乘法 C
Matrix Multiplication C
我已经一遍又一遍地检查代码,一切都应该没问题,所以我不明白。我看过一个视频,内容几乎相同,代码也相同,但我一直收到 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
这样的转换错误。您可以轻松更改它以匹配您自己的编码风格。
我已经一遍又一遍地检查代码,一切都应该没问题,所以我不明白。我看过一个视频,内容几乎相同,代码也相同,但我一直收到 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
这样的转换错误。您可以轻松更改它以匹配您自己的编码风格。