gpt4 book ai didi

c - 二维数组(C)中每一行和每一列的总和

转载 作者:行者123 更新时间:2023-11-30 20:32:23 25 4
gpt4 key购买 nike

我想获取数组的每一行和每一列的总和,然后用C一张一张地打印出来。我完成了一半的工作,但无法继续进行下去。这是我的代码:

int n,m;
void sum_row_column(int array[n][m],int r,int c,int i,int j)
{
int sum_of_column1=0;
int sum_of_row1;

for(j=0,i=0;i<r;i++)
{
sum_of_column1=sum_of_column1+array[i][j];
if(i==r-1)
{
printf("\nSum of %d.column: %d\n",j+1,sum_of_column1);
j=j+1;
}
}

for(i=0,j=0;j<r;j++)
{
sum_of_row1=sum_of_row1+array[i][j];
if(j=c-1)
{
printf("\nSum of %d.row: %d\n",j+1,sum_of_row1);
i=i+1;
}
}


如您所见,它仅打印第一行的总和和列的总和。我以为如果加

i=0;




printf("\nSum of %d.column: %d\n",j+1,sum_of_column1);
j=j+1


行,它可以移动第二列并对其求和。但是当我添加

i=0;


程序进入无限循环,没有任何错误。
该代码位于头文件中,但在其余代码中请不要担心没有任何错误。

最佳答案

任务的建议代码

你有:

int n,m;
void sum_row_column(int array[n][m],int r,int c,int i,int j)
{


尽管可以编译,但是它的代码定义不明确,如果全局变量 nm设置不正确,则不必要地导致失败。不需要该级别的耦合,并且如果您确实使用该级别的耦合,则变量 rc是多余的。您应该使用:

void print_sum_rows_cols(int r, int c, int array[r][c])
{


这允许使用VLA(可变长度数组)符号将代码用于打印任何形状的矩阵。变量 nm应该是不必要的;参数 ij似乎也毫无意义,因为您忽略了传递给函数的值,并在循环开始时将它们都设置为 0

汇总列很简单:

for (int col = 0; col < c; col++)
{
int colsum = 0;
for (int row = 0; row < r; row++)
colsum += array[row][col];
printf("Sum for col %d = %d\n", col, colsum);
}


对行求和也很简单:

for (int row = 0; row < r; row++)
{
int rowsum = 0;
for (int col = 0; col < c; col++)
rowsum += array[row][col];
printf("Sum for row %d = %d\n", row, rowsum);
}


组装MCVE( Minimal, Complete, Verifiable Example):

#include <stdio.h>

static void print_sum_rows_cols(int r, int c, int array[r][c])
{
for (int col = 0; col < c; col++)
{
int colsum = 0;
for (int row = 0; row < r; row++)
colsum += array[row][col];
printf("Sum for col %d = %d\n", col, colsum);
}
for (int row = 0; row < r; row++)
{
int rowsum = 0;
for (int col = 0; col < c; col++)
rowsum += array[row][col];
printf("Sum for row %d = %d\n", row, rowsum);
}
}

static void dump_matrix(const char *tag, int rows, int cols, int matrix[rows][cols])
{
printf("%s (%dx%d):\n", tag, rows, cols);
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
printf("%3d", matrix[r][c]);
putchar('\n');
}
}

int main(void)
{
int a1[3][4] =
{
{ 68, 78, 50, 46, },
{ 64, 12, 47, 1, },
{ 86, 10, 84, 62, },
};
int a2[5][3] =
{
{ 4, 30, 19, },
{ 79, 58, 20, },
{ 95, 12, 24, },
{ 20, 37, 72, },
{ 17, 0, 53, },
};

dump_matrix("A1", 3, 4, a1);
print_sum_rows_cols(3, 4, a1);

dump_matrix("A2", 5, 3, a2);
print_sum_rows_cols(5, 3, a2);

return 0;
}


(重命名的) print_sum_rows_cols()函数中的代码保留了问题的变量名,尽管它们不是我在自己的代码中使用的名称。 (对于这些,请参阅“其他答案的分析”部分。)

并输出:

A1 (3x4):
68 78 50 46
64 12 47 1
86 10 84 62
Sum for col 0 = 218
Sum for col 1 = 100
Sum for col 2 = 181
Sum for col 3 = 109
Sum for row 0 = 242
Sum for row 1 = 124
Sum for row 2 = 242
A2 (5x3):
4 30 19
79 58 20
95 12 24
20 37 72
17 0 53
Sum for col 0 = 215
Sum for col 1 = 137
Sum for col 2 = 188
Sum for row 0 = 53
Sum for row 1 = 157
Sum for row 2 = 131
Sum for row 3 = 129
Sum for row 4 = 70


请注意,这会打印输入,以便可以对照输入手动检查输出。这是调试中的有用技术。

解构替代答案

answer(修订版1)中, Jozeph显示了据称有效的代码。当以正统格式缩进时,代码为:


int n;
int m;

void sum_row_column(int array[n][m], int r, int c, int i, int j)
{
int sumOfColumn = 0;
int sumOfRow = 0;
printf("\n");
for (i = 0; i < r; i++)
{
sumOfRow = 0;
for (j = 0; j < c; j++)
{
sumOfRow += array[i][j];
}
printf("Sum of %d.row=%d\n", i + 1, sumOfRow);
}
printf("\n");
for (j = 0; j < r; j++)
{
sumOfColumn = 0;
for (i = 0; i < c; i++)
{
sumOfColumn += array[i][j];
}
printf("Sum of %d.column=%d\n", j + 1, sumOfColumn);
}
}



此代码存在多个问题,其中一些已经确定,但在此处以略有不同的形式重复出现:


矩阵的大小由文件范围(全局)变量 nm中的值标识。
矩阵的大小也由参数 rc标识。
参数 ij中的值未使用;它们只是定义两个局部循环变量的一种特殊方式。
假设可以使用 assert(n == r && m == c);,则第一对嵌套循环(对行进行求和)就可以了。
假设有可能使用 assert(n == r && m == c && n == m):(方阵),第二对嵌套循环(将列求和)是可以的,但这主要是偶然的而不是设计。
但是,如果全局变量与参数不同步,则第一对嵌套循环中的计算是不正确的,因为循环使用的大小为 r x c,但是该函数假定数组的大小为 m x n
同样,如果全局变量与参数不同步,或者矩阵不是正方形,则第二对嵌套循环中的计算不正确。它甚至不会为每一列产生一个“列总和”。


这是我原始答案的改编,其中包括建议的答案(如上所示),并添加了一些调试(只读)打印。它还包括我最初建议的功能的一个较小变体-变量被重命名以适合我的偏见(循环变量的简称,函数参数的较长的名称等),以及调试打印代码。该代码还会在列总和之前打印行总和,以匹配建议答案的顺序,并且它标识从行1,列1而不是行0,列0开始的行。

我的答案( bogus_sum_rows_cols())还有一个'假'变体,它使用 nm变量来调整数组的大小;当 nmrc不同步时,它将产生错误的答案。提议的答案( bogus_sum_row_column())还有一个'假'变体,如果 rc中的数组大小与 nm都匹配,则至少可以正确地对列求和。

/* SO 47719-9719 - Abuse of VLA definitions (and notations) */
#include <stdio.h>

static int debug = 0;

// Original answer (renamed variables, reordered results, and print row/col + 1)
static void print_sum_rows_cols(int rows, int cols, int array[rows][cols])
{
for (int r = 0; r < rows; r++)
{
int rowsum = 0;
for (int c = 0; c < cols; c++)
{
if (debug)
printf(" a[%d][%d] = %d", r, c, array[r][c]);
rowsum += array[r][c];
}
if (debug)
putchar('\n');
printf("Sum for row %d = %d\n", r + 1, rowsum);
}
putchar('\n');
for (int c = 0; c < cols; c++)
{
int colsum = 0;
for (int r = 0; r < rows; r++)
{
if (debug)
printf(" a[%d][%d] = %d", r, c, array[r][c]);
colsum += array[r][c];
}
if (debug)
putchar('\n');
printf("Sum for col %d = %d\n", c + 1, colsum);
}
}

static int n; // Rows
static int m; // Columns

// Adapted print_sum_rows_cols with size from n, m
// Using n, m is unnecessary coupling (and can lead to erroneous results)
static void bogus_sum_rows_cols(int array[n][m], int rows, int cols)
{
for (int r = 0; r < rows; r++)
{
int rowsum = 0;
for (int c = 0; c < cols; c++)
{
if (debug)
printf(" a[%d][%d] = %d", r, c, array[r][c]);
rowsum += array[r][c];
}
if (debug)
putchar('\n');
printf("Sum for row %d = %d\n", r, rowsum);
}
putchar('\n');
for (int c = 0; c < cols; c++)
{
int colsum = 0;
for (int r = 0; r < rows; r++)
{
if (debug)
printf(" a[%d][%d] = %d", r, c, array[r][c]);
colsum += array[r][c];
}
if (debug)
putchar('\n');
printf("Sum for col %d = %d\n", c, colsum);
}
}

// Original answer from OP - bogus results (debug printing added).
// Using n, m is unnecessary coupling (and can lead to erroneous results)
// Using i, j as arguments is bogus; the values passed in are ignored
// The 'sum column' loops are completely bogus. The outer loop should
// iterate over columns [0..c) and the inner loop over rows [0..r).
// See print_sum_rows_cols() for the correct (minimal) interface.
static void sum_row_column(int array[n][m], int r, int c, int i, int j)
{
int sumOfColumn = 0;
int sumOfRow = 0;
printf("\n");
for (i = 0; i < r; i++)
{
sumOfRow = 0;
for (j = 0; j < c; j++)
{
if (debug)
printf(" a[%d][%d] = %d", i, j, array[i][j]);
sumOfRow += array[i][j];
}
if (debug)
putchar('\n');
printf("Sum of %d.row=%d\n", i + 1, sumOfRow);
}
printf("\n");
for (j = 0; j < r; j++)
{
sumOfColumn = 0;
for (i = 0; i < c; i++)
{
if (debug)
printf(" a[%d][%d] = %d", i, j, array[i][j]);
sumOfColumn += array[i][j];
}
if (debug)
putchar('\n');
printf("Sum of %d.column=%d\n", j + 1, sumOfColumn);
}
}

// Semi-fixed answer from OP - bogus results (debug printing added).
// Using n, m is unnecessary coupling (and can lead to erroneous results)
// Using i, j as arguments is bogus; the values passed in are ignored
// The 'sum column' loops are completely bogus. The outer loop should
// iterate over columns [0..c) and the inner loop over rows [0..r).
// See print_sum_rows_cols() for the correct (minimal) interface.
static void bogus_sum_row_column(int array[n][m], int r, int c, int i, int j)
{
int sumOfColumn = 0;
int sumOfRow = 0;
printf("\n");
for (i = 0; i < r; i++)
{
sumOfRow = 0;
for (j = 0; j < c; j++)
{
if (debug)
printf(" a[%d][%d] = %d", i, j, array[i][j]);
sumOfRow += array[i][j];
}
if (debug)
putchar('\n');
printf("Sum of %d.row=%d\n", i + 1, sumOfRow);
}
printf("\n");
for (j = 0; j < c; j++) // c, not r
{
sumOfColumn = 0;
for (i = 0; i < r; i++) // r, not c
{
if (debug)
printf(" a[%d][%d] = %d", i, j, array[i][j]);
sumOfColumn += array[i][j];
}
if (debug)
putchar('\n');
printf("Sum of %d.column=%d\n", j + 1, sumOfColumn);
}
}

static void dump_matrix(const char *tag, int rows, int cols, int matrix[rows][cols])
{
printf("Matrix %s (%dx%d):\n", tag, rows, cols);
for (int r = 0; r < rows; r++)
{
for (int c = 0; c < cols; c++)
printf("%3d", matrix[r][c]);
putchar('\n');
}
}

static void test_sequence(int v_debug, int v_n, int v_m, const char *tag,
int rows, int cols, int matrix[rows][cols])
{
debug = v_debug;
n = v_n;
m = v_m;
dump_matrix(tag, rows, cols, matrix);
printf("\nprint_sum_rows_cols():\n");
print_sum_rows_cols(rows, cols, matrix);
printf("\nbogus_sum_rows_cols() - (n = %d, m = %d):\n", n, m);
bogus_sum_rows_cols(matrix, rows, cols);
printf("\nsum_row_column()(n = %d, m = %d):\n", n, m);
sum_row_column(matrix, rows, cols, -1, -1);
printf("\nbogus_sum_row_column()(n = %d, m = %d):\n", n, m);
bogus_sum_row_column(matrix, rows, cols, -1, -1);
putchar('\n');
}

static void test_matrix_summation(const char *tag, int rows, int cols, int matrix[rows][cols])
{
test_sequence(1, 2, 2, tag, rows, cols, matrix);
test_sequence(0, rows, cols, tag, rows, cols, matrix);
}

int main(void)
{
int a1[3][4] =
{
{ 68, 78, 50, 46, },
{ 64, 12, 47, 1, },
{ 86, 10, 84, 62, },
};
int a2[5][3] =
{
{ 4, 30, 19, },
{ 79, 58, 20, },
{ 95, 12, 24, },
{ 20, 37, 72, },
{ 17, 0, 53, },
};
int a3[3][3] =
{
{ 96, 84, 13, },
{ 63, 29, 80, },
{ 97, 98, 48, },
};

test_matrix_summation("A1", 3, 4, a1);
test_matrix_summation("A2", 5, 3, a2);
test_matrix_summation("A3", 3, 3, a3);

return 0;
}


所示代码的输出为:

Matrix A1 (3x4):
68 78 50 46
64 12 47 1
86 10 84 62

print_sum_rows_cols():
a[0][0] = 68 a[0][1] = 78 a[0][2] = 50 a[0][3] = 46
Sum for row 1 = 242
a[1][0] = 64 a[1][1] = 12 a[1][2] = 47 a[1][3] = 1
Sum for row 2 = 124
a[2][0] = 86 a[2][1] = 10 a[2][2] = 84 a[2][3] = 62
Sum for row 3 = 242

a[0][0] = 68 a[1][0] = 64 a[2][0] = 86
Sum for col 1 = 218
a[0][1] = 78 a[1][1] = 12 a[2][1] = 10
Sum for col 2 = 100
a[0][2] = 50 a[1][2] = 47 a[2][2] = 84
Sum for col 3 = 181
a[0][3] = 46 a[1][3] = 1 a[2][3] = 62
Sum for col 4 = 109

bogus_sum_rows_cols() - (n = 2, m = 2):
a[0][0] = 68 a[0][1] = 78 a[0][2] = 50 a[0][3] = 46
Sum for row 0 = 242
a[1][0] = 50 a[1][1] = 46 a[1][2] = 64 a[1][3] = 12
Sum for row 1 = 172
a[2][0] = 64 a[2][1] = 12 a[2][2] = 47 a[2][3] = 1
Sum for row 2 = 124

a[0][0] = 68 a[1][0] = 50 a[2][0] = 64
Sum for col 0 = 182
a[0][1] = 78 a[1][1] = 46 a[2][1] = 12
Sum for col 1 = 136
a[0][2] = 50 a[1][2] = 64 a[2][2] = 47
Sum for col 2 = 161
a[0][3] = 46 a[1][3] = 12 a[2][3] = 1
Sum for col 3 = 59

sum_row_column()(n = 2, m = 2):

a[0][0] = 68 a[0][1] = 78 a[0][2] = 50 a[0][3] = 46
Sum of 1.row=242
a[1][0] = 50 a[1][1] = 46 a[1][2] = 64 a[1][3] = 12
Sum of 2.row=172
a[2][0] = 64 a[2][1] = 12 a[2][2] = 47 a[2][3] = 1
Sum of 3.row=124

a[0][0] = 68 a[1][0] = 50 a[2][0] = 64 a[3][0] = 47
Sum of 1.column=229
a[0][1] = 78 a[1][1] = 46 a[2][1] = 12 a[3][1] = 1
Sum of 2.column=137
a[0][2] = 50 a[1][2] = 64 a[2][2] = 47 a[3][2] = 86
Sum of 3.column=247

bogus_sum_row_column()(n = 2, m = 2):

a[0][0] = 68 a[0][1] = 78 a[0][2] = 50 a[0][3] = 46
Sum of 1.row=242
a[1][0] = 50 a[1][1] = 46 a[1][2] = 64 a[1][3] = 12
Sum of 2.row=172
a[2][0] = 64 a[2][1] = 12 a[2][2] = 47 a[2][3] = 1
Sum of 3.row=124

a[0][0] = 68 a[1][0] = 50 a[2][0] = 64
Sum of 1.column=182
a[0][1] = 78 a[1][1] = 46 a[2][1] = 12
Sum of 2.column=136
a[0][2] = 50 a[1][2] = 64 a[2][2] = 47
Sum of 3.column=161
a[0][3] = 46 a[1][3] = 12 a[2][3] = 1
Sum of 4.column=59

Matrix A1 (3x4):
68 78 50 46
64 12 47 1
86 10 84 62

print_sum_rows_cols():
Sum for row 1 = 242
Sum for row 2 = 124
Sum for row 3 = 242

Sum for col 1 = 218
Sum for col 2 = 100
Sum for col 3 = 181
Sum for col 4 = 109

bogus_sum_rows_cols() - (n = 3, m = 4):
Sum for row 0 = 242
Sum for row 1 = 124
Sum for row 2 = 242

Sum for col 0 = 218
Sum for col 1 = 100
Sum for col 2 = 181
Sum for col 3 = 109

sum_row_column()(n = 3, m = 4):

Sum of 1.row=242
Sum of 2.row=124
Sum of 3.row=242

Sum of 1.column=222
Sum of 2.column=130
Sum of 3.column=200

bogus_sum_row_column()(n = 3, m = 4):

Sum of 1.row=242
Sum of 2.row=124
Sum of 3.row=242

Sum of 1.column=218
Sum of 2.column=100
Sum of 3.column=181
Sum of 4.column=109

Matrix A2 (5x3):
4 30 19
79 58 20
95 12 24
20 37 72
17 0 53

print_sum_rows_cols():
a[0][0] = 4 a[0][1] = 30 a[0][2] = 19
Sum for row 1 = 53
a[1][0] = 79 a[1][1] = 58 a[1][2] = 20
Sum for row 2 = 157
a[2][0] = 95 a[2][1] = 12 a[2][2] = 24
Sum for row 3 = 131
a[3][0] = 20 a[3][1] = 37 a[3][2] = 72
Sum for row 4 = 129
a[4][0] = 17 a[4][1] = 0 a[4][2] = 53
Sum for row 5 = 70

a[0][0] = 4 a[1][0] = 79 a[2][0] = 95 a[3][0] = 20 a[4][0] = 17
Sum for col 1 = 215
a[0][1] = 30 a[1][1] = 58 a[2][1] = 12 a[3][1] = 37 a[4][1] = 0
Sum for col 2 = 137
a[0][2] = 19 a[1][2] = 20 a[2][2] = 24 a[3][2] = 72 a[4][2] = 53
Sum for col 3 = 188

bogus_sum_rows_cols() - (n = 2, m = 2):
a[0][0] = 4 a[0][1] = 30 a[0][2] = 19
Sum for row 0 = 53
a[1][0] = 19 a[1][1] = 79 a[1][2] = 58
Sum for row 1 = 156
a[2][0] = 58 a[2][1] = 20 a[2][2] = 95
Sum for row 2 = 173
a[3][0] = 95 a[3][1] = 12 a[3][2] = 24
Sum for row 3 = 131
a[4][0] = 24 a[4][1] = 20 a[4][2] = 37
Sum for row 4 = 81

a[0][0] = 4 a[1][0] = 19 a[2][0] = 58 a[3][0] = 95 a[4][0] = 24
Sum for col 0 = 200
a[0][1] = 30 a[1][1] = 79 a[2][1] = 20 a[3][1] = 12 a[4][1] = 20
Sum for col 1 = 161
a[0][2] = 19 a[1][2] = 58 a[2][2] = 95 a[3][2] = 24 a[4][2] = 37
Sum for col 2 = 233

sum_row_column()(n = 2, m = 2):

a[0][0] = 4 a[0][1] = 30 a[0][2] = 19
Sum of 1.row=53
a[1][0] = 19 a[1][1] = 79 a[1][2] = 58
Sum of 2.row=156
a[2][0] = 58 a[2][1] = 20 a[2][2] = 95
Sum of 3.row=173
a[3][0] = 95 a[3][1] = 12 a[3][2] = 24
Sum of 4.row=131
a[4][0] = 24 a[4][1] = 20 a[4][2] = 37
Sum of 5.row=81

a[0][0] = 4 a[1][0] = 19 a[2][0] = 58
Sum of 1.column=81
a[0][1] = 30 a[1][1] = 79 a[2][1] = 20
Sum of 2.column=129
a[0][2] = 19 a[1][2] = 58 a[2][2] = 95
Sum of 3.column=172
a[0][3] = 79 a[1][3] = 20 a[2][3] = 12
Sum of 4.column=111
a[0][4] = 58 a[1][4] = 95 a[2][4] = 24
Sum of 5.column=177

bogus_sum_row_column()(n = 2, m = 2):

a[0][0] = 4 a[0][1] = 30 a[0][2] = 19
Sum of 1.row=53
a[1][0] = 19 a[1][1] = 79 a[1][2] = 58
Sum of 2.row=156
a[2][0] = 58 a[2][1] = 20 a[2][2] = 95
Sum of 3.row=173
a[3][0] = 95 a[3][1] = 12 a[3][2] = 24
Sum of 4.row=131
a[4][0] = 24 a[4][1] = 20 a[4][2] = 37
Sum of 5.row=81

a[0][0] = 4 a[1][0] = 19 a[2][0] = 58 a[3][0] = 95 a[4][0] = 24
Sum of 1.column=200
a[0][1] = 30 a[1][1] = 79 a[2][1] = 20 a[3][1] = 12 a[4][1] = 20
Sum of 2.column=161
a[0][2] = 19 a[1][2] = 58 a[2][2] = 95 a[3][2] = 24 a[4][2] = 37
Sum of 3.column=233

Matrix A2 (5x3):
4 30 19
79 58 20
95 12 24
20 37 72
17 0 53

print_sum_rows_cols():
Sum for row 1 = 53
Sum for row 2 = 157
Sum for row 3 = 131
Sum for row 4 = 129
Sum for row 5 = 70

Sum for col 1 = 215
Sum for col 2 = 137
Sum for col 3 = 188

bogus_sum_rows_cols() - (n = 5, m = 3):
Sum for row 0 = 53
Sum for row 1 = 157
Sum for row 2 = 131
Sum for row 3 = 129
Sum for row 4 = 70

Sum for col 0 = 215
Sum for col 1 = 137
Sum for col 2 = 188

sum_row_column()(n = 5, m = 3):

Sum of 1.row=53
Sum of 2.row=157
Sum of 3.row=131
Sum of 4.row=129
Sum of 5.row=70

Sum of 1.column=178
Sum of 2.column=100
Sum of 3.column=63
Sum of 4.column=194
Sum of 5.column=107

bogus_sum_row_column()(n = 5, m = 3):

Sum of 1.row=53
Sum of 2.row=157
Sum of 3.row=131
Sum of 4.row=129
Sum of 5.row=70

Sum of 1.column=215
Sum of 2.column=137
Sum of 3.column=188

Matrix A3 (3x3):
96 84 13
63 29 80
97 98 48

print_sum_rows_cols():
a[0][0] = 96 a[0][1] = 84 a[0][2] = 13
Sum for row 1 = 193
a[1][0] = 63 a[1][1] = 29 a[1][2] = 80
Sum for row 2 = 172
a[2][0] = 97 a[2][1] = 98 a[2][2] = 48
Sum for row 3 = 243

a[0][0] = 96 a[1][0] = 63 a[2][0] = 97
Sum for col 1 = 256
a[0][1] = 84 a[1][1] = 29 a[2][1] = 98
Sum for col 2 = 211
a[0][2] = 13 a[1][2] = 80 a[2][2] = 48
Sum for col 3 = 141

bogus_sum_rows_cols() - (n = 2, m = 2):
a[0][0] = 96 a[0][1] = 84 a[0][2] = 13
Sum for row 0 = 193
a[1][0] = 13 a[1][1] = 63 a[1][2] = 29
Sum for row 1 = 105
a[2][0] = 29 a[2][1] = 80 a[2][2] = 97
Sum for row 2 = 206

a[0][0] = 96 a[1][0] = 13 a[2][0] = 29
Sum for col 0 = 138
a[0][1] = 84 a[1][1] = 63 a[2][1] = 80
Sum for col 1 = 227
a[0][2] = 13 a[1][2] = 29 a[2][2] = 97
Sum for col 2 = 139

sum_row_column()(n = 2, m = 2):

a[0][0] = 96 a[0][1] = 84 a[0][2] = 13
Sum of 1.row=193
a[1][0] = 13 a[1][1] = 63 a[1][2] = 29
Sum of 2.row=105
a[2][0] = 29 a[2][1] = 80 a[2][2] = 97
Sum of 3.row=206

a[0][0] = 96 a[1][0] = 13 a[2][0] = 29
Sum of 1.column=138
a[0][1] = 84 a[1][1] = 63 a[2][1] = 80
Sum of 2.column=227
a[0][2] = 13 a[1][2] = 29 a[2][2] = 97
Sum of 3.column=139

bogus_sum_row_column()(n = 2, m = 2):

a[0][0] = 96 a[0][1] = 84 a[0][2] = 13
Sum of 1.row=193
a[1][0] = 13 a[1][1] = 63 a[1][2] = 29
Sum of 2.row=105
a[2][0] = 29 a[2][1] = 80 a[2][2] = 97
Sum of 3.row=206

a[0][0] = 96 a[1][0] = 13 a[2][0] = 29
Sum of 1.column=138
a[0][1] = 84 a[1][1] = 63 a[2][1] = 80
Sum of 2.column=227
a[0][2] = 13 a[1][2] = 29 a[2][2] = 97
Sum of 3.column=139

Matrix A3 (3x3):
96 84 13
63 29 80
97 98 48

print_sum_rows_cols():
Sum for row 1 = 193
Sum for row 2 = 172
Sum for row 3 = 243

Sum for col 1 = 256
Sum for col 2 = 211
Sum for col 3 = 141

bogus_sum_rows_cols() - (n = 3, m = 3):
Sum for row 0 = 193
Sum for row 1 = 172
Sum for row 2 = 243

Sum for col 0 = 256
Sum for col 1 = 211
Sum for col 2 = 141

sum_row_column()(n = 3, m = 3):

Sum of 1.row=193
Sum of 2.row=172
Sum of 3.row=243

Sum of 1.column=256
Sum of 2.column=211
Sum of 3.column=141

bogus_sum_row_column()(n = 3, m = 3):

Sum of 1.row=193
Sum of 2.row=172
Sum of 3.row=243

Sum of 1.column=256
Sum of 2.column=211
Sum of 3.column=141


您可以调整打开或关闭调试的时间,以获得不同的输出量,但是仔细检查结果会发现各种差异,这些差异与 main()程序中定义的矩阵的行和列的实际总和有关。

当全局变量与参数同步时,所提出的答案会在平方矩阵上获得“正确”的答案,这一事实说明了进行全面测试的重要性。

如果该代码仅用于处理平方矩阵,则不会同时具有 nm(并且既不需要 rc)。但是您不应该将函数与所有全局变量联系在一起。您应该定义 rc(如果矩阵必须为正方形,则只能定义其中之一)。

这也说明了为什么MCVE( Minimal, Complete, Verifiable Example)很重要。变量 nm中的值未记录在问题或建议的答案中,因此有必要弄清楚它们表示的含义。

关于c - 二维数组(C)中每一行和每一列的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47719719/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com