C语言:编写一个程序,它将采用30个整数并打印最大数字和最小数字

时间:2016-11-21 17:21:36

标签: c algorithm loops if-statement for-loop

编写一个程序,该程序将采用5个整数并打印最大数字和最小数字

我试过这段代码

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int num1, num2, num3, num4, num5;
    int largest;
    int smallest;

    printf ( "\nEnter five integers." );
    printf ( "\nAnd I will give you the smallest and the largest." );

        scanf( "%d%d%d%d%d",  &num1, &num2, &num3, &num4, &num5 );

            if ( num1 >= num2, num3, num4, num5 )
            {
                ( "num1 = largest" );
            }

            if ( num1 <= num2, num3, num4, num5 )
            {
                ( "num1 = smallest" );
            }

            if ( num2 >= num1, num3, num4, num5 )
            {
                ( "num2 = largest" );
            }

            if ( num2 <= num1, num3, num4, num5 )
            {
                ( "num2 = smallest" );
            }

            if ( num3 >= num1, num2, num4, num5 )
            {
                ( "num3 = largest" );
            }

            if ( num3 <= num1, num2, num4, num5 )
            {
                ( "num3 = smallest" );
            }

            if ( num4 >= num1, num2, num3, num5 )
            {
                ( "num4 = largest" );
            }

            if ( num4 <= num1, num2, num3, num5 )
            {
                ( "num4 = smallest" );
            }

            if ( num5 >= num1, num2, num3, num4 )
            {
                ( "num5 = largest" );
            }

            if ( num5 <= num1, num2, num3, num4 )
            {
                ( "num5 = smallest" );
            }

            printf ( "The largest integer is %d.", largest);
            printf ( "The smallest integer is %d.", smallest);

    return 0;
}

但是有些错误!

请任何人帮忙解决这个问题? 如果我们可以用while循环来做这个,请解释

感谢

4 个答案:

答案 0 :(得分:2)

if ( num1 >= num2, num3, num4, num5 )

不是你如何比较多个项目。它是有效的C代码,但是逗号运算符从左到右执行每段代码,语句的结果是最后一段的结果。所以你的代码基本上变成了:

if (num5)

这不是你想要的。这是比较多个项目的方法:

if ( num1 >= num2 && num1 >= num3 && num1 >= num4 && num1 >= num5 )

另外,

( "num4 = largest" );

什么也没做。我想你的意思是

largest = num4;

解决此问题的更有效方法是首先询问用户他们想要比较多少个数字。这并不仅限于5或30个输入。然后,您可以使用for循环来获取输入,然后进行比较:

// How many numbers the user wants to input
int numInput = 0;
printf("How many numbers to compare? ");
scanf(" %d", &numInput);

// Initialize
int largest = INT_MIN;
int smallest = INT_MAX;

int input;

// Loop 
for (int i = 0; i < numInput; i++) {
    // Get next number
    printf("Enter #: ");
    scanf(" %d", &input)) {
    // Compare
    if (input > largest) largest = input;
    if (input < smallest) smallest = input;
}
// Print results
printf("Largest: %d, smallest: %d\r\n", largest, smallest);

注意,需要错误检查。例如:您应该检查scanf的返回值,以确保您有一个号码。

答案 1 :(得分:1)

以下是使用数组的示例程序。

#include <stdio.h>
int main()
{
    int n[5], i, largest, smallest;

    scanf("%d%d%d%d%d", &n[0], &n[1], &n[2], &n[3], &n[4]);
    smallest = largest = n[0];
    for (i = 1; i < 5; i++) {
        if ( smallest > n[i])
            smallest = n[i];
        if ( largest < n[i] )
            largest = n[i];
    }
    printf("%d\t%d\n", smallest, largest);
}

答案 2 :(得分:1)

在这样的表达中

if ( num1 >= num2, num3, num4, num5 )

使用了所谓的逗号运算符,整个表达式的结果对应于值num5 != 0

正如你实际上问的两个问题:一个是关于如何为这个作业编写程序

  

取30个整数并打印最大数字和最小数字

和另一个是关于如何为这个作业编写程序

  

取5个整数并打印最大数字和最小数字

然后我将展示一个执行两项任务的示范程序。:)

#include <stdio.h>

int main(void) 
{
{   
    const size_t N = 30;

    int value;  
    int smallest, largest;

    printf ( "\nEnter %zu integers.", N );
    printf ( "\nAnd I will give you the smallest and the largest.\n" );

    size_t i = 0;

    while ( i < N && scanf( "%d", &value ) == 1 )
    {
        if ( i++ == 0 )
        {
            smallest = largest = value; 
        }
        else
        {
            if ( value < smallest )
            {
                smallest = value;
            }
            else if ( largest < value )
            {
                largest = value;
            }
        }
    }

    if ( i != 0 )
    {
        printf( "\nAmong %zu entered values "
                "the smallest is %d and the largest is %d\n",
                i, smallest, largest );
    }
}

{
    int num1, num2, num3, num4, num5;

    printf ( "\nEnter five integers." );
    printf ( "\nAnd I will give you the smallest and the largest.\n" );

    scanf( "%d%d%d%d%d",  &num1, &num2, &num3, &num4, &num5 );

    if ( !( num2 < num1 ) && !( num3 < num1 ) && !( num4 < num1 ) && !( num5 < num1 ) )
    {
        printf( "The smallest number is the first number with value %d\n", num1 );
    }
    else if ( !( num3 < num2 ) && !( num4 < num2 ) && !( num5 < num2 ) )
    {
        printf( "The smallest number is the second number with value %d\n", num2 );
    }
    else if ( !( num4 < num3 ) && !( num5 < num3 ) )
    {
        printf( "The smallest number is the third number with value %d\n", num3 );
    }
    else if ( !( num5 < num4 ) )
    {
        printf( "The smallest number is the fouth number with value %d\n", num4 );
    }
    else
    {
        printf( "The smallest number is the fifth number with value %d\n", num5 );
    }

    if ( !( num1 < num2 ) && !( num1 < num3 ) && !( num1 < num4 ) && !( num1 < num5 ) )
    {
        printf( "The largest number is the first number with value %d\n", num1 );
    }
    else if ( !( num2 < num3 ) && !( num2 < num4 ) && !( num2 < num5 ) )
    {
        printf( "The largest number is the second number with value %d\n", num2 );
    }
    else if ( !( num3 < num4 ) && !( num3 < num5 ) )
    {
        printf( "The largest number is the third number with value %d\n", num3 );
    }
    else if ( !( num4 < num5 ) )
    {
        printf( "The largest number is the fouth number with value %d\n", num4 );
    }
    else
    {
        printf( "The largest number is the fifth number with value %d\n", num5 );
    }
}
    return 0;
}

它的输出可能看起来像

Enter 30 integers.
And I will give you the smallest and the largest.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

Among 30 entered values the smallest is 1 and the largest is 30

Enter five integers.
And I will give you the smallest and the largest.
1 2 3 4 5
The smallest number is the first number with value 1
The largest number is the fifth number with value 5

你可以自己纠正你会找到的程序中的任何拼写错误。:)

答案 3 :(得分:0)

这里没有使用数组。

#include <stdio.h>
int main()
{
    int input, i, largest, smallest;

    scanf("%d", &input);
    smallest = largest = input;
    for (i = 1; i < 5; i++) {
            scanf("%d", &input);
            if ( smallest > input)
                    smallest = input;
            if ( largest < input )
                    largest = input;
    }
    printf("%d\t%d\n", smallest, largest);
}