【发布时间】: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];也无法正常工作