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

C语言矩阵乘法代码仅2x2正常其他维度输出错误如何排查

错误分析
  • 核心错误:矩阵输入、打印阶段的行列遍历边界完全颠倒
    你定义的矩阵A为m1行、n1列,矩阵B为m2行、n2列,但代码中输入矩阵A时外层循环上限写的是列数n1、内层循环上限写的是行数m1,刚好把行、列的遍历顺序搞反。矩阵B的输入、两个矩阵的打印逻辑都存在同样的问题。只有当矩阵行数等于列数(比如2x2)时,这个错误不会暴露,其他尺寸矩阵会出现数据读写错位,最终导致计算结果异常。
  • 次要问题:变量重复定义
    你已经在main函数开头声明了j1变量,输入矩阵A的内层循环额外加了int修饰j1,会引发重复定义的编译警告。
修正方案

调整所有矩阵输入、打印环节的循环边界,匹配矩阵实际的行列数:

  1. 矩阵A输入的外层循环控制行号,范围为1m1,内层循环控制列号,范围为1n1
  2. 矩阵B输入的外层循环控制行号,范围为1m2,内层循环控制列号,范围为1n2
  3. 两个矩阵的打印环节同步调整对应行列的循环边界
  4. 去掉输入矩阵A内层循环j1前的多余int修饰符
修正后完整代码
#include <stdio.h>
#include <stdlib.h>
int main()
{
    int a[20][20], b[20][20], mul[20][20];
    int m1, n1, m2, n2;
    int i1, j1, i2, j2, i3, j3, k;
    int sum = 0;
    printf("Enter the number of Rows and Column for Matrix A: ");
    scanf("%d %d", &m1, &n1);
    printf("Enter the number of Rows and Column for Matrix B: ");
    scanf("%d %d", &m2, &n2);

    if (n1 == m2)
    {
        printf("Insert elements in matrix A-\n");
        
        for (i1 = 1; i1 <= m1; i1++)
        {
            for (j1 = 1; j1 <= n1; j1++)
            {
                printf("Enter A[%d][%d]: ", i1, j1);
                scanf("%d", &a[i1][j1]);
            }
        }
        
        printf("\n");
        
        printf("Insert elements in matrix B-\n");
        
        for (i2 = 1; i2 <= m2; i2++)
        {
            for (j2 = 1; j2 <= n2; j2++)
            {
                printf("Enter B[%d][%d]: ", i2, j2);
                scanf("%d", &b[i2][j2]);
            }
        }

        printf("\n");

        printf("Matrix A-\n");
        
        for (int i = 1; i <= m1; i++)
        {
            for (int j = 1; j <= n1; j++)
            {
                printf("%d ", a[i][j]);
            }
            printf("\n");
        }

        printf("Matrix B-\n");
        
        for (int i = 1; i <= m2; i++)
        {
            for (int j = 1; j <= n2; j++)
            {
                printf("%d ", b[i][j]);
            }
            printf("\n");
        }

        for (i3 = 1; i3 <= m1; i3++)
        {
            for (j3 = 1; j3 <= n2; j3++)
            {
                sum = 0;
                for (k = 1; k <= m2; k++)
                {
                    sum = sum + a[i3][k] * b[k][j3];
                }
                mul[i3][j3] = sum;
            }
        }

        printf("\nThe Multiplication Matrix of the two entered Matrices is:\n");
        
        for (int i = 1; i <= m1; i++)
        {
            for (int j = 1; j <= n2; j++)
            {
                printf("%d ", mul[i][j]);
            }
            printf("\n");
        }
    }
    else
    {
        printf("The given information is not valid.\nFor the multiplication of two matrices (AxB), the no. of Columns of A should be equal to the no. of Rows of B.\nCheck the given Data.\n");
        printf("Your given Data:-\nRow & Column of A = %d, %d\nRow & Column of B = %d, %d\n", m1, n1, m2, n2);
    }
    return 0;
}

内容的提问来源于stack exchange,提问作者Darshika Jadhav

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.26 21:15:00