c - 矩阵乘法 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
. 您可以轻松更改它以匹配您自己的编码风格。
推荐阅读
- c++ - 链接到方法,Doxygen C++
- javascript - 将数组的元素返回到谷歌工作表中的多列(GAS/JS)
- c# - 如何转换两个类泛型?
- laravel - 我正在尝试在默认注册表单中添加一个下拉列表以从 laravel 7 中的数据库中捕获数据
- bash - 遍历文件并在 BASH 中跳过 n 个数量
- javascript - 动态商店命令 (Discord.js)
- deep-learning - 在哪些情况下我们应该使用不是 1 的步幅?
- python - 如何修复 TypeError: Expected unicode, got pandas._libs.properties.CachedProperty
- matlab - 找到关于两个矩阵 X 和 Y 的 (x,y) 的最接近值
- java - Java随机生成5个数字并打印最大的