【问题标题】:The minimum value is not recognize最小值无法识别
【发布时间】:2021-03-08 00:26:14
【问题描述】:

最大值被识别但不是最小值?你能帮我找出这段代码中的问题吗?还是编译器有问题?

#include<stdio.h>

int main()
{
    int e,i,sval,lval;

    printf("\nInput the length of the array:");
    scanf("%d", &e);

    int v[e];
    printf("\nInput the array element:\n");

    for(i=1; i<=e; i++)
    {
        scanf("%d", &v[i]);
    }
    for(i=1; i<=e; i++)
    {
        if(sval > v[i])
        {
            sval = v[i];
        }
        else
        {
            lval = v[i];
        }

    }

    printf("Smallest value : %d\n", sval);
    printf("Larger value : %d\n", lval);

    return 0;
}

【问题讨论】:

    标签: c max min


    【解决方案1】:

    数组在 C 中为 0

    int v[e];
    printf("\nInput the array element:\n");
    
    for(i=1; i<=e; i++)
    {
        scanf("%d", &v[i]);
    }
    

    这里:i&lt;=e 你正在访问数组边界之外,切换到

    for(i=0; i<e; i++)
    {
        scanf("%d", &v[i]);
    }
    

    同样

    for(i=1; i<=e; i++)
    {
        if(sval > v[i])
        {
            sval = v[i];
        }
        else
        {
            lval = v[i];
        }
    
    }
    

    使用前初始化变量svallval

    int e,i,sval = 0,lval = 0;
    

    【讨论】:

    • 初始化 sval=0 意味着如果我们得到一个 [2, 4, 5, 6] 列表,就永远不会找到任何低于 0 的数字,因此,0 将作为最小值。
    • 没错,因为这一点在您的回答中得到了很好的解释,所以我保留它只是为了指出错误。
    • sval 应设置为INT_MAX,任何值都将小于或等于。 lval 应设置为 INT_MIN,任何值都将大于或等于。
    【解决方案2】:

    这段代码有一些问题:

    1. C 中的数组从索引 0 开始,因此我们需要在几个地方进行调整。
    2. 需要适当地初始化最小值和最大值。
    3. 在确定循环期间哪个值最小/最大时出现逻辑问题。

    请看 cmets inline。

    #include <limits.h>
    #include <stdio.h>
    
    int main()
    {
        int e, i, min_val, max_val;
        min_val = INT_MAX;
        max_val = INT_MIN;
        // Need to initialize these to min/max values
        // We set the smallest value to the largest value because
        // presumably all other values will be lower than this. 
        // The same is true for the larger value.
    
        printf("\nInput the length of the array:");
        scanf("%d", &e);
    
        int v[e];
        printf("\nInput the array element:\n");
    
        // as mentioned, arrays start at 0
        for(i = 0; i < e; i++)
        {
            scanf("%d", &v[i]);
        }
        for(i = 0; i < e; i++)
        {
            if (min_val > v[i]) {
                min_val = v[i];
            }
            // Need a separate condition here, not an else.
            // In your example, you had the statement:
            // "If the value I am currently looking at is smaller than
            // my currently known smallest value, assign it to the larger
            // value" <- this is a logic flaw. It becomes apparent in sets
            // such as the one shown: [9, 8, 2, 6]
            // In your example, when we reach the iteration
            // where we are looking at the 6, since that is larger
            // than the currently known smallest value of 2, we would 
            // assign the 6 to the largest value, when it should be 9.
            if (max_val < v[i]) {
                max_val = v[i];
            }
        }
    
        printf("Smallest value : %d\n", min_val);
        printf("Larger value : %d\n", max_val);
    
        return 0;
    }
    
    (venv) [ttucker@zim stackoverflow]$ gcc -o min_max min_max.c 
    (venv) [ttucker@zim stackoverflow]$ ./min_max 
    
    Input the length of the array:4
    
    Input the array element:
    9
    8
    2
    6
    Smallest value : 2
    Larger value : 9
    
    

    【讨论】:

    • 只是为了好玩,假设我认为它不正确。请通过解释它是如何工作的以及为什么它应该解决问题来说服我。这也有助于消除人们认为 StackOverflow 是一个提供和寻找无偿编程工作的平台的误解。
    • @Yunnosch 完成。我听到你的误解。我通常会尝试在我的答案中添加更多细节。我想堆栈溢出为时已晚。
    • 作为一个完美的答案,我还建议为变量使用一个有意义的名称,minmax 而不是 svallval
    • 干得好。我对接受我的提议表示敬意(有时人们只是感到受到攻击,这也是完全人性化的)。只是为了外观,我建议用散文(即代码之外)对核心解决方案进行全局总结。不过,不管你是否这样做,都请我投赞成票。玩得开心。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 2016-09-25
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 2017-10-07
    相关资源
    最近更新 更多