简体   繁体   English

为什么我得到这个 output?

[英]Why I am getting this output?

#include <stdio.h>

#define MAX_SIZE 1000     // Maximum array size 

int main() {
    int arr[MAX_SIZE], size, i;
    int max1, max2;
    scanf("%d", &size);
    max1 = max2 = -99999;
    /* Input array elements */ 
    for (i = 0; i < size; i++) {
        scanf("%d", &arr[i]);
    }
    /*
     * Check for first largest and second
     */
    for (i = 0; i < size; i++) {
        if (arr[i] > max1) {
            max2 = max1;
            max1 = arr[i];
        } else
        if (arr[i] > max2 && arr[i] < max1) {
            max2 = arr[i];
        }
    }
    printf("%d", max2);
    return 0;
}

when I used test case 20 10 40 4 100 output is correct 40 , but when I use 1 2 3 4 5 then output is max2 = -99999 .当我使用测试用例20 10 40 4 100 output 是正确的40 ,但是当我使用1 2 3 4 5时 output 是max2 = -99999

Can anyone please explain to me why I am not getting this?谁能向我解释为什么我没有得到这个? I traced but did not get why?我追踪但没有得到为什么?

In your program, the first input is the size of the array.在您的程序中,第一个输入是数组的大小。 So, your inputs should actually be 5 20 10 40 4 100 and 5 1 2 3 4 5 (note the extra 5 in front) so they have the correct size.因此,您的输入实际上应该是5 20 10 40 4 1005 1 2 3 4 5 (注意前面的额外 5),因此它们的大小正确。 Your first input is correct because it read in 20 as the size and tried to read in 20 numbers, and there weren't enough numbers so it just read in 0 for the rest, and 40 was still the second-largest.您的第一个输入是正确的,因为它读入20作为大小并尝试读入 20 个数字,并且没有足够的数字,所以它只是读入 rest 的 0,而 40 仍然是第二大的。 For your second input, it read in 1 as the size, so there was no second-largest number, so it printed out -99999.对于您的第二个输入,它读入 1 作为大小,因此没有第二大数字,因此它打印出 -99999。

The output is somewhat consistent with the input, assuming the input comes from a text file: output 与输入有些一致,假设输入来自文本文件:

  • the first number read is the number of entries to handle.读取的第一个数字是要处理的条目数。
  • for the first case, size is read as 20 and the program tries to read 20 numbers into arr[0] through arr[19] .对于第一种情况, size被读取为20并且程序尝试将 20 个数字读入arr[0]arr[19] As you do not test the return value of scanf() , you do not detect the invalid or missing input past the last one in the file 100 .由于您不测试scanf()的返回值,因此您不会检测到文件100中最后一个输入之后的无效或丢失输入。 scanf() fails and leaves arr[i] unchanged from index 4 on. scanf()失败并从索引4开始arr[i]不变。 arr is unintiialized, so the contents is undefined and so is the behavior of the program. arr未初始化,因此内容未定义,程序的行为也是如此。 It so happens that there are no numbers in arr[4] through arr[19] that are larger than 40. So the second largest number is printed as 40 .碰巧arr[4]arr[19]中没有大于 40 的数字。因此,第二大数字打印为40
  • for the second case, size is read as 1 and only a single number is read into arr[0] , the remaining input being ignored.对于第二种情况, size被读取为1并且只有一个数字被读入arr[0] ,其余的输入被忽略。 The loop sets max1 = 2 and max2 = max1 , whose initial value is -99999 .循环设置max1 = 2max2 = max1 ,其初始值为-99999 Hence the output.因此,output。

You should provide 5 20 10 40 4 100 and 5 1 2 3 4 5 as input to get the expected behavior consistently.您应该提供5 20 10 40 4 1005 1 2 3 4 5作为输入,以获得一致的预期行为。

You should also be more defensive with user input and report unexpected cases:您还应该对用户输入更具防御性并报告意外情况:

#include <limits.h>
#include <stdio.h>

#define MAX_SIZE 1000     // Maximum array size 

int main() {
    int arr[MAX_SIZE], size, i;
    int max1, max2, has_max;
    if (scanf("%d", &size) != 1) {
        printf("missing count of numbers\n");
        return 1;
    }
    if (size < 0 || size > MAX_SIZE) {
        printf("invalid count of numbers: %i\n", size);
        return 1;
    }
    /* Input array elements */ 
    for (i = 0; i < size; i++) {
        if (scanf("%d", &arr[i]) != 1) {
            printf("invalid input: got %i numbers instead of %i\n", i, size);
            size = i;
            break;
        }
    }
    /*
     * Check for first largest and second
     */
    max1 = max2 = INT_MIN;
    has_max = 0;
    for (i = 0; i < size; i++) {
        if (arr[i] > max1) {
            max2 = max1;
            max1 = arr[i];
            has_max++;
        } else
        if (arr[i] > max2 && arr[i] < max1) {
            max2 = arr[i];
            has_max++;
        }
    }
    if (has_max < 2) {
        printf("no second highest value\n");
    } else {
        printf("%d\n", max2);
    }
    return 0;
}

Note the method to track if there are at least 2 different values in the array.请注意跟踪数组中是否至少有 2 个不同值的方法。 Try and see if this improvement is sufficient for all cases...尝试看看这种改进是否适用于所有情况......

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

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