简体   繁体   English

除非存在另一个printf,否则Printf不起作用

[英]Printf does not work unless another printf is present

I was trying to solve a challenge in C on HackerRank. 我试图解决HackerRank上C语言中的挑战。 There is a square matrix with n rows and columns.The challenge is to print the absolute difference between the two sums of the matrix's diagonals as a single integer. 有一个具有n行和n列的正方形矩阵,难题是将矩阵对角线的两个和之间的绝对差打印为单个整数。

Here's the link: https://www.hackerrank.com/challenges/diagonal-difference 这是链接: https : //www.hackerrank.com/challenges/diagonal-difference

I'm able to get the correct answer but the printf() statement at the end of the code does not work unless I add an unnecessary printf("") before the for loop statement. 我能够得到正确的答案,但是除非在for循环语句之前添加不必要的printf(“”),否则代码末尾的printf()语句不起作用。 Could someone explain why this is happening? 有人可以解释为什么会这样吗?

Thanks in advance 提前致谢

Here's the code: 这是代码:

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main()
{
    int n; 
    scanf("%d",&n);
    int a[n][n],sum_d1=0,sum_d2=0,a_i,a_j;
    for( a_i = 1; a_i <= n; a_i++)
    {
       for(a_j = 1; a_j <= n; a_j++)
       {
           scanf("%d",&a[a_i][a_j]);
           if(a_j==a_i)
               sum_d1=sum_d1+a[a_i][a_j];
       }
    }
    printf("");// the last printf works only when this statement is present
    for(a_i=1;a_i<=n;a_i++)
    {
        for(a_j=n;a_j>0;a_j--)
        {
            if((a_i+a_j)==(n+1))
                sum_d2=sum_d2+a[a_i][a_j];
        }
    }
    printf("%d",abs(sum_d1-sum_d2)); //this doesn't work if there is no printf above
    return 0;
}

The trailing newline is the key here. 尾随换行符是这里的关键。 Standard out only flushes when it encounters a line ending for performance reasons. 标准输出仅在遇到由于性能原因而终止的行时才刷新。 If you change to fprintf(stderr, ...) then your code will work as is. 如果更改为fprintf(stderr,...),则您的代码将按原样运行。 You could also use fflush(stdout) after your printf's. 您也可以在printf之后使用fflush(stdout)。

As a couple of other people have mentioned, your main issue is your array declaration / use. 正如其他几个人提到的那样,您的主要问题是数组声明/使用。 Arrays in C have their first index from 0, not from 1. As such, when you try and store things in your array, or access them again, it is out of the boundary of the array, and may act in a way unintended. C中的数组的第一个索引从0开始,而不是从1开始。因此,当您尝试将事物存储在数组中或再次访问它们时,它超出了数组的边界,并且可能以意外的方式起作用。

Array example: Array[2][3] 数组示例:Array [2] [3]

    0  1 
0  a|  b | 
1  c|  d | 
2  e|  f |

Array[0][0] == a 
Array[1][1] == d 
Array[1][2] == f

In your code, if someone enters "1", an array of size [1][1] is created - a single element. 在您的代码中,如果有人输入“ 1”,则会创建一个大小为[1] [1]的数组-一个元素。 This should be accessed using a[0][0], but in the code is being accessed by a[1][1], which is an invalid element, as it is not a part of the array. 应该使用a [0] [0]来访问它,但是在代码中却被a [1] [1]来访问,a [1] [1]是无效元素,因为它不是数组的一部分。

Without knowing exactly what you intend your code to do, I cannot fix it entirely, but a good start would be as below: 在不完全知道您打算如何执行代码的情况下,我无法完全修复它,但是如下所示是一个好的开始:

#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include <stdbool.h>

int main(){
    int n; 
    scanf("%d",&n);
    int a[n][n],sum_d1=0,sum_d2=0,a_i,a_j;
    for( a_i = 0; a_i < n; a_i++){
       for(a_j = 0; a_j < n; a_j++){
           scanf("%d",&a[a_i][a_j]);
           if(a_j==a_i)
               sum_d1=sum_d1+a[a_i][a_j];
       }
    }
    //printf("");// the last printf works only when this statement is present
    for(a_i=0;a_i<n;a_i++)
    {
        for(a_j=n-1;a_j>=0;a_j--)
        {
            if((a_i+a_j)==(n+1))
                sum_d2=sum_d2+a[a_i][a_j];
        }
    }
    printf("%d",abs(sum_d1-sum_d2)); //this doesn't work if there is no printf above
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM