I am a newbie programmer and just started to teach myself C then decided to tackle some simple problems taken from the internet. Specifically with this one: Problem
And my solution is:
#include <stdio.h>
#include <math.h>
#include <float.h>
#include <limits.h>
int main() {
double min = DBL_MAX;
double max = DBL_MIN;
double n;
do {
scanf("%lf", &n);
if (n < 0.0001) {
break;
}
if (n > max) {
max = n;
}
if (n < min) {
min = n;
}
} while (n > 0.0000);
printf("Min: %.4f Max: %.4f\n", min, max);
return 0;
}
However, I need to run my program exactly as the input/output specified by the problem. For example, consider I am inputting in different lines:
1.1 1.2 1.3 3 6 9 11 -2 7
2.2
2.3 12
0
The program should output 12 as the max and -2 as the min, the program ends when the 0 was inputted.
nisfloatand notdouble?float n; scanf("%lf", &n);double min = 0;thenif (n < min)will never be true considering that you bailed out before withif(n < 0.0001) { break; }. So, I predict, your min will always be reported as0.0000(which is not intended). ...and btw. thewhile (n > 0.0000);is a bit use-less. In this case, afor (;;)instead ofdo/whilewould do as well.minandmaxto maximum and minimum double value. Add#include <limits.h>and use DBL_MAX and DBL_MIN constantsdouble min = DBL_MAX; double max = DBL_MIN;