计算并打印矩阵对角线的总和

时间:2018-12-15 13:35:15

标签: c arrays for-loop while-loop

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

例如如果矩阵是

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

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

您可以执行以下操作:

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