C程序寻找第二大数字

问题描述 投票:-1回答:1

我正在尝试找到该程序中第二大的数字,请给我最大的数字:(

#include <stdio.h>

#define VALUE_TO_STOP -999

int main(void)
{
    int num,num2,max,secondMax = 0;

    printf("Enter a number (-999 to stop): ");
    scanf("%d", &num);

    while(num2 != VALUE_TO_STOP)
    {
        printf("Enter a number (-999 for end): ");
        scanf("%d", &num2);

        if(num2 > num || num2 > max)
        {
            max = num2;
        }

        if(num2 > secondMax && num2 < max)
        {
            secondMax = num2;
        }
    }

    printf("First max: %d", max);
    printf("\nThe second max: %d", secondMax);

    return 0;
}
c
1个回答
-1
投票

我已修正您的代码。首先,您必须初始化int max

© www.soinside.com 2019 - 2024. All rights reserved.