You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言矩阵乘法程序输出空数组问题求助

解决你的矩阵乘法程序输出空数组问题

嘿,我帮你揪出了几个关键错误,这就是你的程序输出异常的根源:

1. 矩阵乘法核心计算的索引完全错误

在matrix_mul函数的三重循环里,你用了错误的数组索引来获取元素:

sum += ar1[row1][k] * ar2[column1][row1];

这里row1是矩阵1的总行数(比如你测试用的3),ar1[row1][k]已经超出了矩阵1的行索引范围(矩阵行索引是从0到row1-1),而且完全没有用到当前循环的i和j变量。正确的矩阵乘法逻辑应该是第一个矩阵的第i行 × 第二个矩阵的第j列,对应元素相乘后累加,所以应该改成:

sum += ar1[i][k] * ar2[k][j];

2. 结果矩阵的赋值索引错误

你给结果矩阵赋值时用了:

arr[row1][column2] = sum;

同样是用了固定的越界索引,根本没有把计算结果存到对应位置。正确的写法是把结果存到当前循环的i行j列:

arr[i][j] = sum;

3. 矩阵兼容性判断条件错误

你写的判断逻辑:

if(column1 != row2 || column2 != row1) {
    printf("Incompatible matrices. Try Again! ");
    return 0;
}

完全不符合矩阵乘法的规则!矩阵乘法只要求第一个矩阵的列数等于第二个矩阵的行数(也就是column1 == row2),第二个矩阵的列数和第一个矩阵的行数没有强制要求。你多出来的column2 != row1条件会把合法的矩阵对(比如你测试的3x2和2x3)错误判定为不兼容,直接终止程序。正确的判断应该是:

if(column1 != row2) {
    printf("Incompatible matrices. Try Again! ");
    return 0;
}

4. 输入提示的笔误

输入第二个矩阵时的提示文字写错了:

printf("\n\nEnter elements for matrix 1 of order %d x %d\n", row2, column2);

应该改成matrix 2,避免用户混淆:

printf("\n\nEnter elements for matrix 2 of order %d x %d\n", row2, column2);

修正后的完整代码

#include <stdio.h>
#define MAX 100
void matrix_mul(int, int, int, int, int [MAX][MAX], int [MAX][MAX]);

int main() {
    int mat_1[MAX][MAX] ,mat_2[MAX][MAX];
    int row1, row2, column1, column2;

    printf("Input number of rows for the first matrix: ");
    scanf("%d", &row1);
    printf("Input number of columns for the first matrix: ");
    scanf("%d", &column1);
    printf("Input number of rows for the second matrix: ");
    scanf("%d", &row2);
    printf("Input number of columns for the second matrix: ");
    scanf("%d", &column2);

    // 修正后的兼容性判断
    if(column1 != row2) {
        printf("Incompatible matrices. Try Again! ");
        return 0;
    }

    printf("Enter elements for matrix 1 of order %d x %d\n", row1, column1);
    for(int i=0; i<row1; i++) {
        for(int j=0; j<column1; j++)
            scanf("%d", &mat_1[i][j]);
    }

    // 修正后的提示文字
    printf("\n\nEnter elements for matrix 2 of order %d x %d\n", row2, column2);
    for(int i=0; i<row2; i++) {
        for(int j=0; j<column2; j++)
            scanf("%d", &mat_2[i][j]);
    }

    matrix_mul(row1, row2, column1, column2, mat_1, mat_2);
}

// 修正后的乘法函数
void matrix_mul(int row1, int row2, int column1, int column2, int ar1[MAX][MAX], int ar2[MAX][MAX]) {
    int arr [MAX][MAX];
    for(int i=0 ; i<row1; i++) {
        for(int j=0; j<column2; j++) {
            int sum = 0;
            // 修正后的元素获取逻辑
            for(int k=0; k<column1; k++)
                sum += ar1[i][k] * ar2[k][j];
            // 修正后的结果赋值
            arr[i][j] = sum;
        }
    }
    // 打印结果矩阵
    printf("\nResult matrix:\n");
    for(int i=0; i<row1; i++) {
        for(int j=0; j<column2; j++)
            printf("%d ", arr[i][j]);
        printf("\n");
    }
}

现在用你测试的3x2和2x3矩阵输入,程序就能正确输出乘法结果啦!

内容的提问来源于stack exchange,提问作者Nisan Abeywickrama

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:12:47