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

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

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

最佳答案

引用 Weather Vane 的评论:

The program alters the array it is examining — see array1[sub_i][sub_j]=sum; — and then prints incorrect values, since you can't correctly sum the diagonals of an array that is changing.

OP 已经意识到

... what you are telling me is to assign the values to another array and print that.

是的,这更容易:

#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 .

关于计算和打印矩阵对角线的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53793695/

相关文章:

c++ - 在 C 和 C++ 中解析 typedef

c - opengl使聚光灯像手电筒一样

C - 字符串数组和函数

C编程修改快速排序

c - 用 C 抓取网页的最简单方法是什么?

c++ - 为什么 printf 在打印十六进制时只打印一个字节?

php - MongoDB 使用 PHP 返回数组来填充下拉列表

c++ - 重新声明变量

javascript - 如何在 HTML 中的 for 循环中使用文档

java - 如何在 Java 中创建循环并递增索引以用作字符串的一部分?