【问题标题】:C Program that finds the sum of each row and column of two separate 2d arrays求两个单独二维数组的每一行和每一列之和的 C 程序
【发布时间】:2021-05-18 10:58:04
【问题描述】:

所以我要做的是创建一个 C 程序,其中代码创建 2 个不同的 2D 数组,其中填充了 100 - 1500 的随机数,我正在尝试合并一个功能,程序将打印出 1D包含arrayA和arrayB中对应行总和的数组,

我已经添加了一个功能,它可以找到两个数组中每个对应单元格的总和并将它们打印出来

但我似乎无法弄清楚如何找到每一行的总和,我最初的方法是在我的函数 sumRow 中,我最初打算简单地将我的产品数组添加到自身,但我意识到它只会添加每个对应的单元格,而不是行本身

   for (int i = 0; i < rows; i++)
   {
      printf("\t[\t");
      for (int j = 0; j < columns; j++)
      {
         sumRows[i][j] = sumArray[i][j] + sumArray[i][j];
         printf("%d\t", sumRows[i][j]);
      }
      printf("\t]\n");
   }
}

到目前为止,我有一个想法,如果在我的 sumRow 函数中,我可以简单地将所有内容加在一起并打印出来,这将自动为行和列创建一个一维数组,但我'我不太确定该怎么做

非常感谢任何帮助和提示,谢谢!

这是我的完整工作代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define MAX_SIZE 20

void randArray(int rows, int columns, int array[rows][columns]) {
   for (int i = 0; i < rows; i++){
      printf("\t[\t");
      for (int j = 0; j < columns; j++) {
         array[i][j] = rand()%1400 +101;
         printf("%d\t", array[i][j]);
      }
      printf("]\n");
   }
}

void product(int rows, int columns, int array1[rows][columns], int array2[rows][columns], int productArray[rows][columns]) {
   for (int i = 0; i < rows; i++) {
      printf("\t[\t");
      for (int j = 0; j < columns; j++) {
         productArray[i][j] =array1[i][j]*array2[i][j];
         printf("%d\t", productArray[i][j]);
      }
      printf("\t]\n");
   }
   
}

void sum(int rows, int columns, int array1[rows][columns], int array2[rows][columns], int sumArray[rows][columns])
{
   for (int i = 0; i < rows; i++)
   {
      printf("\t[\t");
      for (int j = 0; j < columns; j++)
      {
         sumArray[i][j] = array1[i][j] + array2[i][j];
         printf("%d\t", sumArray[i][j]);
      }
      printf("\t]\n");
   }
}

void sumRow(int rows, int columns, int sumArray[rows][columns], int sumRows[rows][columns]) {
   for (int i = 0; i < rows; i++)
   {
      printf("\t[\t");
      for (int j = 0; j < columns; j++)
      {
         sumRows[i][j] = sumArray[i][j] + sumArray[i][j];
         printf("%d\t", sumRows[i][j]);
      }
      printf("\t]\n");
   }
}

int main() {
   int rows = 0;                     //Variable to store the number of rows
   int columns = 0;                  //Variable to store the number of columns
   int arrayA[20][20];               //A 20x20 2D array A declaration which will be modified later on
   int arrayB[20][20];               //A 20x20 2D array B declaration which will be modified later on
   int productArray[20][20];
   int sumArray[20][20];
   int sumRows[20][20];
   int sumCols[20][20];

   printf("How many rows and columns would you like in the arrays?\n\nPlease enter the number of rows: ");
   scanf("%d", &rows);
   printf("Please enter the number of columns: ");
   scanf("%d", &columns);

   //This wil both generate the random numbers and print out the arrays
   printf("Your arrays are:\nArray A:\n");
   randArray(rows, columns, arrayA);
   printf("ArrayB\n");
   randArray(rows, columns, arrayB);


   printf("The array containing the product of both arrays is:\n");
   product(rows, columns, arrayA, arrayB, productArray);

   printf("The array containing the sum of both arrays is:\n");
   sum(rows, columns, arrayA, arrayB, sumArray);

   printf("The array containing the sum rows of both arrays is:\n");
   sumRow(rows, columns, sumArray, sumRows);

   
   return 0;
}

【问题讨论】:

  • 您计算相应单元格的总和,而不是行数。
  • 但我相信你想要sumRows[i] = sumArrayA[i][j] + sumArrayB[i][j];
  • 嗨,保罗,这就是我想要在我的函数中做的事情,但由于某种原因,它只是拒绝与我合作,我计算了相应单元格的总和,我最初试图使用只需将该数组中的行相加,但即使我尝试sumRows[i] = array1[i][j] +array2[i][j]; 也无法正常工作

标签: arrays c matrix


【解决方案1】:

使用 sumRows 作为一维数组:int sumRows[rows];

void sumRow(int rows, int columns, int sumArray[rows][columns], int sumRows[rows]) {
   for (int i = 0; i < rows; i++)
   {
      printf("\t[\t");
      for (int j = 0; j < columns; j++)
      {
         sumRows[i] += sumArray[i][j];
      }
      printf("%d\t", sumRows[i]);
      printf("\t]\n");
   }
}

【讨论】:

    【解决方案2】:

    问题是使用 VLA 数组作为参数,同时将非 VLA 对象传递给这些参数。

    函数认为每个数组的stride是columns,而实际上是20。而且当rows * lows大于20 * 20时会变得非常危险,因为可能...堆栈溢出。

    解决此问题的一种方法是首先解析 rowscolumns,然后使用语法创建数组:

    int ARRAY[rows][columns];
    

    此外,sumRows() 函数看起来很可疑。 矩阵的行之和实际上是一个向量。 我猜你的意图是沿列累积总和。

    正确的代码可能如下所示:

    void sumRow(int rows, int columns, int sumArray[rows][columns], int sumRows[rows][columns]) {
       for (int i = 0; i < rows; i++)
       {
          int cumsum = 0;
          printf("\t[\t");
          for (int j = 0; j < columns; j++) {
             cumsum += sumArray[i][j];
             sumRows[i][j] = cumsum;
             printf("%d\t", sumRows[i][j]);
          }
          printf("\t]\n");
       }
    }
    

    void sumRow(int rows, int columns, int sumArray[rows][columns], int sumRows[rows]) {
       for (int i = 0; i < rows; i++) {
          int sum = 0;
          printf("\t[\t");
          for (int j = 0; j < columns; j++)
             sum += sumArray[i][j];
          sumRows[i] = sum;
          printf("%d\n", sumRows[i]);
       }
    }
    
    ...
    
    int sumRows[rows];
    sumRow(rows, columns, sumArray, sumRows);
    

    【讨论】:

      猜你喜欢
      • 2021-12-04
      • 2019-03-22
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 2016-12-07
      • 2016-03-19
      • 2016-02-13
      • 1970-01-01
      相关资源
      最近更新 更多