2D数组中每行和每列的总和(C)

时间:2017-12-08 17:50:13

标签: c arrays

我想获得数组的每一行和每列的总和,并在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;

程序进入无限循环而没有任何错误。 这段代码在标题中,但不要担心其余的代码没有任何错误。

2 个答案:

答案 0 :(得分:1)

任务的建议代码

你有:

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()r中的数组大小与c和{n中的数组大小匹配,那么建议答案(m)的“虚假”变体也会至少正确地对列进行求和。 {1}}。

/* 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中的值未记录在问题或建议的答案中,因此有必要梳理出它们所代表的含义。

答案 1 :(得分:-1)

我做了主要的;

case 8:
        for(i=0;i<n;i++)
        {
            for(j=0;j<m;j++)
            {
                printf("Enter %d.row and %d.column: ",i+1,j+1);
                scanf("%d",&array[i][j]);
            }
        }
        sum_row_column(array,n,m,i,j);
        break;

和功能;

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);
}
}

及其工作!谢谢你的帮助。