【问题标题】:Calculating and printing the sums of the diagonals of a matrix计算和打印矩阵的对角线之和
【发布时间】:2019-05-16 13:10:52
【问题描述】:

这是一个程序,它应该计算矩阵中所有对角线的总和,然后将它们打印出来。

例如。如果矩阵是

1 2 3 4 5
2 3 4 5 6
0 1 1 2 5
5 5 5 5 5
7 8 9 7 7

输出应该是

17 13 13 10 5

15 17 13 13 10

14 15 17 13 13

13 14 15 17 13

7 13 14 15 17

#include <stdio.h>

int main()
{
    int n, sum=0, i, j, sub_i, sub_j, sub1_i, sub1_j;

    scanf("%d ", &n);

    int array1[n][n];

    for(i=0;i<n;i++){
        for(j=0; j<n; j++){
            scanf("%d", &array1[i][j]);
        }
    }
    for(i=0; i<n; i++){
        for(j=0; j<n; j++){
            sub_i=i;
            sub_j=j;
            sub1_i=i;
            sub1_j=j;
            sum=0;

            if(j>i){
                while(sub_j<n){
                    sum+=array1[sub_i][sub_j];
                    sub_i++;
                    sub_j++;
                }
                while(sub_j<n){
                    array1[sub_i][sub_j]=sum;
                    sub1_i++;
                    sub1_j++;
                }
            }

            if(i>j){
                while(sub_i<n){
                    sum+=array1[sub1_i][sub1_j];
                    sub_i++;
                    sub_j++;
                }
                while(sub1_i<n){
                    array1[sub1_i][sub1_j]=sum;
                    sub1_i++;
                    sub1_j++;
                }
            }
        }
    }

    for(i=0; i<n; i++){
        for(j=0; j<n; j++){
            printf("%d ", array1[i][j]);
        }
        printf("\n");
    }
    return 0;
}

当我运行程序时,它会打印数组,就好像没有为矩阵分配值一样。有人能指出发生了什么吗?

【问题讨论】:

  • 无法复制。程序更改了它正在检查的数组——参见array1[sub_i][sub_j]=sum;——然后打印不正确的值,因为你不能正确地求和正在变化的数组的对角线。
  • @WeatherVane 所以你告诉我的是将值分配给另一个数组并打印出来
  • 您只计算从左到右的对角线。从右到左的对角线怎么样?
  • @PaulOgilvie 查看 OP 所需的输入和输出(在帖子开头),似乎他从最长 (1 - 7) 对角线开始,向上刷到 5,然后再次从“ 2 - 7" 对角线,刷到 7。这​​是一个有点奇怪的问题。是的,正如你所说,它忽略了从左到右的对角线。
  • @PaulOgilvie 该程序应该只计算从右到左对角线的总和并按上面的顺序打印它们。我没有提到我的坏事。

标签: c arrays for-loop while-loop


【解决方案1】:

引用Weather Vane的评论:

程序改变了它正在检查的数组——参见array1[sub_i][sub_j]=sum;——然后打印出不正确的值,因为你不能正确地求和正在改变的数组的对角线。

OP 已经意识到这一点

...您告诉我的是将值分配给另一个数组并打印出来。

是的,这更容易:

#include <stdio.h>

int main(void)
{
    int n;
    // Checking the input is always a good idea, but you
    // may prefer something less brutal, in case of error
    if (scanf("%d", &n) != 1  ||  n < 1)
       return 1;    

    int mat[n][n];

    for (int i = 0; i < n; ++i) {
        for (int j= 0; j < n; ++j) {
            if (scanf("%d", &mat[i][j]) != 1)
                return 1; 
        }
    }

    // Calculate and store the sum of the diagonals. Note that
    // it could be done in the previous loop, but it may be better
    // to refactor those snippets into separate functions.
    int diagonals[2 * n + 1];
    for (int i = 0; i < 2 * n + 1; ++i)
        diagonals[i] = 0;  // consider 'memset' instead of this loop

    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            diagonals[n + i - j] += mat[i][j]; 
        }    
    }

    // Now print the correct values in their position
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
            printf("%4d", diagonals[n + i - j]);
        }
        printf("\n");
    }
    return 0;
}

可测试HERE

【讨论】:

【解决方案2】:

你可以这样做:

#include <stdio.h>

#define N 5

int main()
{
  int array[N][N] = {
    1, 2, 3, 4, 5,
    2, 3, 4, 5, 6,
    0, 1, 1, 2, 5,
    5, 5, 5, 5, 5,
    7, 8, 9, 7, 7};

  for(int i = 1; i < N; ++i)
    for(int j = 1; j < N; ++j)
      array[i][j] += array[i - 1][j - 1];

  for (int i = 0; i + 1 < N; ++i)
    for (int j = 0; j + 1 < N; ++j)
      if (i == j)
        array[i][j] = array[N - 1][N - 1];
      else if (i > j)
        array[i][j] = array[N - 1][N - 1 - i + j];
      else
        array[i][j] = array[N - 1 - j + i][N - 1];

    for (int i = 0; i < N; ++i) {
      for(int j = 0; j < N; ++j)
        printf("%d ", array[i][j]);
      printf("\n");
    }
    return 0;
}

【讨论】:

  • 您的代码很好,但您的答案没有说明您在 OP 的代码中发现了什么问题,以及为什么您所做的是所需的修复(即使评论代码也会有所帮助)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-10
  • 2016-05-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多