二维数组(或 table)中每列和每行的元素总和,打印出意外值(在 C 中)
Sum of elements, per single column and per single row, in 2D array (or table), prints out unexpected values (in C)
我正在使用 Atom 编辑器 (1.24.0 x64) 和 gpp 编译器(3.0.7,MinGW 中的 GCC)。我可以在此处添加有关系统环境的任何其他有用信息吗?顺便说一句,我只有一个学期的编程,所以我还不知道如何调试。
问题介绍
我正在尝试对使用二维数组生成的 table 中的值求和。一个双 while 循环中有 3 个和。执行金额为:
- table中的所有元素都加起来了。这里没问题。
- 同一行的所有元素相加。问题在这里。返回值与预期不同。它们的值不同,我认为是内存垃圾。
- 同一列中的所有元素相加。问题也出在这里。同上问题。
当然,我期望的是正确的值。我真的不知道问题出在哪里。我已经尝试过不同的方法。我想不出更多的事情,就是在这里和那里更改一些循环或打印结果以查看循环的行为方式。当我打印出列和行的结果时,我得到的是我认为的内存地址(6487568 等)。
结果和预期结果
这里可以看到结果:
2 3 4
5 8 0
7 5 6
The sum of every element in the chart is: 40
The sum of every element per row is:
8 //Should add up to 9
12 //Should add up to 13
4200743 //Should add up to 18
The sum of every element per column is:
15 16 4200611
//Should add up to:
//14 16 10
代码
我认为问题出在这里:
/*This sums up the the elements, for the entire table,
for every single row and for every single column.*/
while (row < ROWS)
{
column = 0;
while (column < COLUMNS)
{
sum += array[row][column];
sumRow[row] += array[row][column];
sumColumn[column] += array[row][column];
column++;
}
row++;
}
这是完整的 code。由于某些原因,在线编译器在添加所有元素时返回的不是预期结果40,而是29。其他结果也不同。
我已经尝试了额外的方法,但没有成功:
sum += array[row][column];
sumRow[row][COLUMNS+1] += array[row][column];
sumColumn[ROWS+1][column] += array[row][column];
另一个:
sum += array[row][column];
sumRow[row] += array[row][column];
sumColumn[column] += array[row][column];
附带说明:我怀疑不允许在 C 中进行此类求和,但由于我只是编程的初学者,所以我真的不知道。我已经在互联网上查看过这是否可能但没有成功。
sumRow
和 sumColumn
在开始对它们进行累加之前未初始化为零
memset()
这些变量在使用之前全部归零
创建数组或矩阵时,其元素具有随机值。并且 sumRow[row] += array[row][column];
在第一次迭代中 sumRow[row] 可以 non-zero 求和之前的值。所以用零初始化它,它应该 运行 没问题。也有同样的情况。
您似乎忘记用零初始化数组 sumRow
和 sumColumn
.
如果数组不是可变长度数组,那么您可以在声明时初始化它们。例如
enum { ROWS = 3, COLUMNS = 3 };
int sumRow[ROWS] = { 0 };
int sumColumn[COLUMNS] = { 0 };
考虑到累加器最好使用类型 long long int
而不是 int
,
例如
enum { ROWS = 3, COLUMNS = 3 };
long long int sumRow[ROWS] = { 0 };
long long int sumColumn[COLUMNS] = { 0 };
如果数组是可变长度数组,那么您可以在声明它们时不对它们进行初始化。在这种情况下,您可以使用在 header <string.h>
.
中声明的标准函数 memset
这是一个演示程序。
#include <stdio.h>
#include <string.h>
void output_sums( size_t rows, size_t cols, int a[rows][cols] )
{
long long int sumRow[rows];
long long int sumColumn[cols];
long long int sum = 0;
size_t i, j;
memset( sumRow, 0, rows * sizeof( long long int ) );
memset( sumColumn, 0, cols * sizeof( long long int ) );
i = 0;
while ( i < rows)
{
j = 0;
while ( j < cols )
{
sum += a[i][j];
sumRow[i] += a[i][j];
sumColumn[j] += a[i][j];
j++;
}
i++;
}
for ( i = 0; i < rows; i++ )
{
for ( j = 0; j < cols; j++ )
{
printf( "%d\t", a[i][j] );
}
putchar( '\n' );
}
printf( "\nThe sum of every element in the chart is: %lld\n", sum );
puts( "\nThe sum of every element per row is:" );
for ( i = 0; i < rows; i++ )
{
printf( "%lld\n", sumRow[i] );
}
puts( "\nThe sum of every element per column is:" );
for ( j = 0; j < cols; j++ )
{
printf( "%lld\t", sumColumn[j] );
}
}
#define N 3
int main(void)
{
int a[N][N] =
{
{ 2, 3, 4 },
{ 5, 8, 0 },
{ 7, 5, 6 }
};
output_sums( N, N, a );
return 0;
}
它的输出是
2 3 4
5 8 0
7 5 6
The sum of every element in the chart is: 40
The sum of every element per row is:
9
13
18
The sum of every element per column is:
14 16 10
编辑: 在您提供对原始代码的引用之后,除了使用 non-initialized 数组外,数组的大小也不正确:
int sumColumn[2], sumRow[2],
^^^ ^^^
尽量不要使用幻数。使用 instaed 命名常量。
我正在使用 Atom 编辑器 (1.24.0 x64) 和 gpp 编译器(3.0.7,MinGW 中的 GCC)。我可以在此处添加有关系统环境的任何其他有用信息吗?顺便说一句,我只有一个学期的编程,所以我还不知道如何调试。
问题介绍
我正在尝试对使用二维数组生成的 table 中的值求和。一个双 while 循环中有 3 个和。执行金额为:
- table中的所有元素都加起来了。这里没问题。
- 同一行的所有元素相加。问题在这里。返回值与预期不同。它们的值不同,我认为是内存垃圾。
- 同一列中的所有元素相加。问题也出在这里。同上问题。
当然,我期望的是正确的值。我真的不知道问题出在哪里。我已经尝试过不同的方法。我想不出更多的事情,就是在这里和那里更改一些循环或打印结果以查看循环的行为方式。当我打印出列和行的结果时,我得到的是我认为的内存地址(6487568 等)。
结果和预期结果
这里可以看到结果:
2 3 4
5 8 0
7 5 6
The sum of every element in the chart is: 40
The sum of every element per row is:
8 //Should add up to 9
12 //Should add up to 13
4200743 //Should add up to 18
The sum of every element per column is:
15 16 4200611
//Should add up to:
//14 16 10
代码
我认为问题出在这里:
/*This sums up the the elements, for the entire table,
for every single row and for every single column.*/
while (row < ROWS)
{
column = 0;
while (column < COLUMNS)
{
sum += array[row][column];
sumRow[row] += array[row][column];
sumColumn[column] += array[row][column];
column++;
}
row++;
}
这是完整的 code。由于某些原因,在线编译器在添加所有元素时返回的不是预期结果40,而是29。其他结果也不同。
我已经尝试了额外的方法,但没有成功:
sum += array[row][column];
sumRow[row][COLUMNS+1] += array[row][column];
sumColumn[ROWS+1][column] += array[row][column];
另一个:
sum += array[row][column];
sumRow[row] += array[row][column];
sumColumn[column] += array[row][column];
附带说明:我怀疑不允许在 C 中进行此类求和,但由于我只是编程的初学者,所以我真的不知道。我已经在互联网上查看过这是否可能但没有成功。
sumRow
和 sumColumn
在开始对它们进行累加之前未初始化为零
memset()
这些变量在使用之前全部归零
创建数组或矩阵时,其元素具有随机值。并且 sumRow[row] += array[row][column];
在第一次迭代中 sumRow[row] 可以 non-zero 求和之前的值。所以用零初始化它,它应该 运行 没问题。也有同样的情况。
您似乎忘记用零初始化数组 sumRow
和 sumColumn
.
如果数组不是可变长度数组,那么您可以在声明时初始化它们。例如
enum { ROWS = 3, COLUMNS = 3 };
int sumRow[ROWS] = { 0 };
int sumColumn[COLUMNS] = { 0 };
考虑到累加器最好使用类型 long long int
而不是 int
,
例如
enum { ROWS = 3, COLUMNS = 3 };
long long int sumRow[ROWS] = { 0 };
long long int sumColumn[COLUMNS] = { 0 };
如果数组是可变长度数组,那么您可以在声明它们时不对它们进行初始化。在这种情况下,您可以使用在 header <string.h>
.
memset
这是一个演示程序。
#include <stdio.h>
#include <string.h>
void output_sums( size_t rows, size_t cols, int a[rows][cols] )
{
long long int sumRow[rows];
long long int sumColumn[cols];
long long int sum = 0;
size_t i, j;
memset( sumRow, 0, rows * sizeof( long long int ) );
memset( sumColumn, 0, cols * sizeof( long long int ) );
i = 0;
while ( i < rows)
{
j = 0;
while ( j < cols )
{
sum += a[i][j];
sumRow[i] += a[i][j];
sumColumn[j] += a[i][j];
j++;
}
i++;
}
for ( i = 0; i < rows; i++ )
{
for ( j = 0; j < cols; j++ )
{
printf( "%d\t", a[i][j] );
}
putchar( '\n' );
}
printf( "\nThe sum of every element in the chart is: %lld\n", sum );
puts( "\nThe sum of every element per row is:" );
for ( i = 0; i < rows; i++ )
{
printf( "%lld\n", sumRow[i] );
}
puts( "\nThe sum of every element per column is:" );
for ( j = 0; j < cols; j++ )
{
printf( "%lld\t", sumColumn[j] );
}
}
#define N 3
int main(void)
{
int a[N][N] =
{
{ 2, 3, 4 },
{ 5, 8, 0 },
{ 7, 5, 6 }
};
output_sums( N, N, a );
return 0;
}
它的输出是
2 3 4
5 8 0
7 5 6
The sum of every element in the chart is: 40
The sum of every element per row is:
9
13
18
The sum of every element per column is:
14 16 10
编辑: 在您提供对原始代码的引用之后,除了使用 non-initialized 数组外,数组的大小也不正确:
int sumColumn[2], sumRow[2],
^^^ ^^^
尽量不要使用幻数。使用 instaed 命名常量。