The following code prints the maximum and the biggest negative value (if we have -10 and -5, -5 is bigger) of numbers entered unitl a symbol is reached. My question is if there is a better way to find the largest negative value (avoiding INT_MIN or other clumsy ways of this kind).
#include <stdio.h>
#include <conio.h>
#include <limits.h>
int main()
{
float max=0, n, bnn=INT_MIN;
while(1){
printf("Enter an integer:");
if(scanf("%f", &n)!=1){
break;
}
if (n>max)
max=n;
else if(n>=bnn)
bnn=n;
}
printf("The maximum number of the entered ones is:%0.2f\n", max);
printf("The biggest negative number is:%0.2f", bnn);
getch();
}
Start bnn at 0.0 Then
if ((n < 0) && ((bnn == 0.0) || (n > bnn)))
bnn = n;
Fun with NaN.
Comparing with Not-A-Number always returns false.
#include <stdio.h>
#include <limits.h>
int main() {
float max = 0.0 / 0.0; // NaN
float bnn = 0.0 / 0.0;
while (1) {
float n;
printf("Enter a number:");
if (scanf("%f", &n) != 1) {
break;
}
if (!(n <= max))
max = n;
// if by "biggest", OP means most negative
// if ((n < 0.0) && (!(n <= bnn)))
// if by "biggest", OP means greatest
if ((n < 0.0) && (!(n >= bnn)))
bnn = n;
}
printf("The maximum number of the entered ones is:%0.2f\n", max);
printf("The biggest negative number is:%0.2f\n", bnn);
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.