Write a program that would take 5 integers and prints the largest number and the smallest number
编写一个程序,该程序将采用5个整数并打印最大数字和最小数字
i tried this code
我试过这段代码
#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;
}
but there's somthing wrong!
但是有些不对劲!
please can any one help to solve this ? and if we can do this with while loop please explaine
请任何人帮忙解决这个问题?如果我们可以用while循环做这个请解释
thanks
谢谢
4 个解决方案
#1
1
In expressions like this
在这样的表达式中
if ( num1 >= num2, num3, num4, num5 )
there is used so-called comma operator and the result of the whole expression corresponds to the value num5 != 0
.
使用所谓的逗号运算符,整个表达式的结果对应于值num5!= 0。
As you asked in fact two questions: the one is about how to write a program for this assignment
正如你实际上问的两个问题:一个是关于如何为这个任务编写程序
take 30 integers and prints the largest number and the smallest number
取30个整数并打印最大数字和最小数字
and other one is about how to write a program for this assignment
另一个是关于如何为这个作业编写程序
take 5 integers and prints the largest number and the smallest number
取5个整数并打印最大数字和最小数字
Then I will show a demonstrative program that performs the both assignments.:)
然后我将展示一个执行两个任务的演示程序。:)
#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;
}
Its output might look like
它的输出可能看起来像
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
You can yourself correct any typo in the program that you'll find.:)
你可以自己纠正你会找到的程序中的任何拼写错误。:)
#2
2
if ( num1 >= num2, num3, num4, num5 )
Is not how you compare multiple items. It is valid C code, but the comma operator executes each piece of code, left to right and the result of the statement is the result of the last piece. So your code basically becomes:
不是你如何比较多个项目。它是有效的C代码,但逗号运算符从左到右执行每段代码,语句的结果是最后一段的结果。所以你的代码基本上变成:
if (num5)
Which is not what you wanted. This is the way to compare multiple items:
这不是你想要的。这是比较多个项目的方法:
if ( num1 >= num2 && num1 >= num3 && num1 >= num4 && num1 >= num5 )
Also,
也,
( "num4 = largest" );
really does nothing. I think you meant
真的什么也没做。我想你的意思
largest = num4;
A more robust way to tackle this problem is to first, ask the user how many numbers they want to compare. This does not limit you to just 5 or 30 inputs. You can then use a for
loop to get the inputs, comparing as you go:
解决此问题的更有效方法是首先询问用户他们想要比较多少个数字。这并不仅限于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);
Note, needs error checking. Ex: you should check the return value from scanf
to make sure you got a number.
注意,需要错误检查。例如:你应该检查scanf的返回值,以确保你得到一个数字。
#3
1
Here is a sample program using arrays.
这是一个使用数组的示例程序。
#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);
}
#4
0
Here it is without using arrays.
这里没有使用数组。
#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);
}
#1
1
In expressions like this
在这样的表达式中
if ( num1 >= num2, num3, num4, num5 )
there is used so-called comma operator and the result of the whole expression corresponds to the value num5 != 0
.
使用所谓的逗号运算符,整个表达式的结果对应于值num5!= 0。
As you asked in fact two questions: the one is about how to write a program for this assignment
正如你实际上问的两个问题:一个是关于如何为这个任务编写程序
take 30 integers and prints the largest number and the smallest number
取30个整数并打印最大数字和最小数字
and other one is about how to write a program for this assignment
另一个是关于如何为这个作业编写程序
take 5 integers and prints the largest number and the smallest number
取5个整数并打印最大数字和最小数字
Then I will show a demonstrative program that performs the both assignments.:)
然后我将展示一个执行两个任务的演示程序。:)
#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;
}
Its output might look like
它的输出可能看起来像
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
You can yourself correct any typo in the program that you'll find.:)
你可以自己纠正你会找到的程序中的任何拼写错误。:)
#2
2
if ( num1 >= num2, num3, num4, num5 )
Is not how you compare multiple items. It is valid C code, but the comma operator executes each piece of code, left to right and the result of the statement is the result of the last piece. So your code basically becomes:
不是你如何比较多个项目。它是有效的C代码,但逗号运算符从左到右执行每段代码,语句的结果是最后一段的结果。所以你的代码基本上变成:
if (num5)
Which is not what you wanted. This is the way to compare multiple items:
这不是你想要的。这是比较多个项目的方法:
if ( num1 >= num2 && num1 >= num3 && num1 >= num4 && num1 >= num5 )
Also,
也,
( "num4 = largest" );
really does nothing. I think you meant
真的什么也没做。我想你的意思
largest = num4;
A more robust way to tackle this problem is to first, ask the user how many numbers they want to compare. This does not limit you to just 5 or 30 inputs. You can then use a for
loop to get the inputs, comparing as you go:
解决此问题的更有效方法是首先询问用户他们想要比较多少个数字。这并不仅限于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);
Note, needs error checking. Ex: you should check the return value from scanf
to make sure you got a number.
注意,需要错误检查。例如:你应该检查scanf的返回值,以确保你得到一个数字。
#3
1
Here is a sample program using arrays.
这是一个使用数组的示例程序。
#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);
}
#4
0
Here it is without using arrays.
这里没有使用数组。
#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);
}