【问题标题】:How to sum 2D arrays in C/C++如何在 C/C++ 中对二维数组求和
【发布时间】:2018-11-06 10:17:31
【问题描述】:

我无法在 c 中添加两个二维数组 可能是什么问题 ?我正在尝试从用户输入添加两个多维数组,但输出不正确。 下面是我的代码

#include<stdio.h>
#include<conio.h>
void main()
{
     int m,n,p,q,c,d,k;
     int first[10][10],second[10][10],sum[10][10];
     clrscr();
     printf("\nEnter the number of rows and columns of the first matrix");
     scanf("%d %d",&m,&n);
     printf("\nEnter the elements of the first matrix");
     for (c = 0;c < m;c++)
         for (d = 0;d < n;d++)
             scanf("%d",&first[c][d]);
     printf("\nEnter the number of rows and columns of the second matrix");
     scanf("%d %d",&p,&q);
     printf("\nEnter the elements of the second matrix");
     for (c = 0;c < p;c++)
         for (d = 0;d < p;d++)
             scanf("%d",&second[c][d]);
     for (c = 0;c < m;c++)
         for (d = 0;d < n;d++)
             for (k = 0;k < p;k++)
             {
                  sum[c][d] = first[c][k] + second[k][d];
             }
     printf("\nThe sum of the two matrices is : \n\n");
     for (c = 0;c < m;c++)
         for (d = 0;d < n;d++)
         {
              printf("%d",&sum[c][d]);
              if (d == n-1)
              {
                  printf("\n\n");
              }
         }
     getch();
}

【问题讨论】:

  • c 还是 c++ ?请格式化您的代码
  • c++。我在 TurboC++ 中使用它
  • for (d = 0;d &lt; p;d++) 似乎是错误的。 d 应该一直运行到 q,不是吗?
  • @BBKiVines int m,n,p,q,c,d,k; -- 如果您使用了更有意义的变量名,您可能更容易发现其中一个错误。
  • 为什么允许两者有不同的维度?如果m != pn != q 你的总和是错误的

标签: c++ c arrays


【解决方案1】:

改变这个:

for (d = 0;d < p;d++)

到这里:

for (d = 0;d < q;d++)

当您填充第二个数组时,因为您使用了第一个维度而不是第二个维度。


改变这个:

printf("%d", &sum[c][d]);

到这里:

printf("%d", sum[c][d]);

因为你想打印一个整数。


PS:这是 C,不是 C++。

【讨论】:

    【解决方案2】:

    请检查所有索引的范围。你有一些错误,其中之一是 d 应该运行到 q (我猜)为你的变量使用更好的名称,这样你就可以记住你在做什么。另外,如果您想在最后打印总和,为什么要使用:

    printf("%d", &sum[c][d]);
    

    你不需要那里的 &

    【讨论】:

      【解决方案3】:

      当你考虑两个矩阵相加时,那么两个矩阵的维度应该是相同的。所以你不需要输入不同矩阵的维度。 其次,尽量使用一些有意义的变量,合理安排代码,让用户更容易理解代码。 您提供的代码是 C 语言的。

      您的代码中的问题是:

         printf(" %d \n",&sum[c][d]);
      

          for (c = 0;c < m;c++)
          for (d = 0;d < n;d++)
          for (k = 0;k < p;k++)
          {
           sum[c][d] = first[c][k] + second[k][d];
          }
      

      对您的代码的改进可以是:

              #include<stdio.h>
              #include<conio.h>
              int main()
              {
               int m,n,c,d;
               int first[10][10],second[10][10],sum[10][10];
               clrscr();
               printf("\nEnter the number of rows and columns of the first matrix");
               scanf("%d %d",&m,&n);
      
               printf("\nEnter the elements of the first matrix");
               for (c = 0;c < m;c++)
                 for (d = 0;d < n;d++)
                    scanf("%d",&first[c][d]);
      
                    printf("\nEnter the elements of the second matrix");  //Both the matrix 
                    should have same dimensions in case of addition
      
               for (c = 0;c < m;c++)
                 for (d = 0;d < n;d++)
                   scanf("%d",&second[c][d]); 
      
               for (c = 0;c < m;c++)   //Addition matrix have same dimensions 
                 for (d = 0;d < n;d++)
                {
                   sum[c][d] = first[c][d] + second[c][d];
                }
      
               printf("\nThe sum of the two matrices is : \n\n");
               for (c = 0;c < m;c++)
                  for (d = 0;d < n;d++)
                   {
                     printf(" %d \n",sum[c][d]);
                   }
      
               getch();
               return 0;
              }
      

      【讨论】:

      • 其次...你使用相同的变量名;)
      • 我刚刚修复了他的代码。让他很容易理解。 @user463035818
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-22
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      • 1970-01-01
      相关资源
      最近更新 更多