【问题标题】:matrix addition using c使用 c 进行矩阵加法
【发布时间】:2015-10-10 22:06:10
【问题描述】:

我正在分配使用 C 添加矩阵的任务,我使用的是 Dev C++ IDE 版本 5.71,我尝试使用此代码但它没有编译

#include "stdio.h"

int main(){
    int a, b ,c ,d, mat1[20][20],mat2[20][20], matAdd[20];

    //a = row number ,b = column no 
    printf("Enter the number of rows and columns:\n");

    scanf("%d%d", &a,&b);

    printf("Enter elements of matrix one:\n");

    for (c =0; c < a; c++){
        for (d=0; d < b; d++){
            scanf("%d", &mat1[c][d]);
         }
    }
    printf("Enter elements of matrix two:\n");

    for (c =0; c < a; c++){
        for (d = 0; d < b; d++){
            scanf("%d", &mat2[c][d]);
        }
    }

    printf("Addition of the two matrices is below:\n");

    for (c =0; c < a; c++){
        for (d=0; d < b; d++){

        matAdd[c][d] = mat1[c][d] + mat2[c][d]; 
        printf("%d\t",matAdd[c][d]); 
    }
    printf("%n");
    getch();
    return 0 ;
}

这是我收到的错误。

Line:35 Col:12 [Error] subscripted value is neither an array nor pointer nor vector
Line:36 Col:26 [Error] subscripted value is neither an array nor pointer nor vector
Line:45 Col:1 [Error] expected declaration or statement at end of input

【问题讨论】:

  • 请编辑您的代码并将您的编译器错误以文本形式提供给我们。
  • 错误在附图中
  • 寻求调试帮助的问题(“为什么这段代码不起作用?”)必须包括所需的行为、特定的问题或错误以及在问题本身中重现它所需的最短代码。没有明确问题陈述的问题对其他读者没有用处。请参阅:如何创建最小、完整和可验证的示例。

标签: c matrix addition


【解决方案1】:

有一个错字:你的定义 matAdd[20] 应该是 matAdd[20][20]

【讨论】:

  • 矩阵乘法需要 3 个嵌套循环:2 个循环迭代结果矩阵的每个元素,并且对于每个这样的元素,另一个迭代来计算乘以适当的行和列的结果。有关示例,请参见 c4learn.com/c-programs/c-program-to-multiply-two-3-x-3.html
  • 但是你真的应该先自己努力一点,找出答案,然后再发布到 Stack Overflow。
猜你喜欢
  • 2016-03-23
  • 2012-05-30
  • 1970-01-01
  • 1970-01-01
  • 2013-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多