0

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.

Student
  • 805
  • 1
  • 8
  • 11
X Lefora
  • 11
  • 2
  • 2
    Why `n` is `float` and not `double`? – Eugene Sh. Oct 23 '18 at 16:46
  • 2
    Save time. Enable compiler warnings. Review `float n; scanf("%lf", &n);` – chux - Reinstate Monica Oct 23 '18 at 16:48
  • If you start with `double min = 0;` then `if (n < min)` will never be true considering that you bailed out before with `if(n < 0.0001) { break; }`. So, I predict, your min will always be reported as `0.0000` (which is not intended). ...and btw. the `while (n > 0.0000);` is a bit use-less. In this case, a `for (;;)` instead of `do`/`while` would do as well. – Scheff's Cat Oct 23 '18 at 16:51
  • You are not taking any data for max-min from the user. Go through the problem statement again. – suvojit_007 Oct 23 '18 at 16:53
  • Set `min` and `max` to maximum and minimum double value. Add `#include ` and use DBL_MAX and DBL_MIN constants `double min = DBL_MAX; double max = DBL_MIN;` – KamilCuk Oct 23 '18 at 16:59
  • Review the idea of input value -2 and `}while(n>0.0000);` – chux - Reinstate Monica Oct 23 '18 at 17:51
  • It should be noted that `DBL_MIN` isn't a large negative number. It's actually the [closest positive double value to zero](https://cboard.cprogramming.com/cplusplus-programming/102785-int_min-dbl_min.html). Use `-DBL_MAX` if you actually want to use the largest negative double value. Using `DBL_MIN` will result in `min` being `1.1` in your data set. – absoluteAquarian Oct 23 '18 at 20:23

3 Answers3

2

You need to set min and max BOTH to the first value set by scanf and then check for greater and less than on later iterations. The way it works now, min starts at 0 and will never change as nothing can be lower than that without the program exiting.

int main(){
  double min = 0; 
  double max = 0; 
  float n; 
  int count = 0; 

  do{
    scanf("%f", &n);

    if(n < 0.0001){
      break;
    }  
    if( count ) {
        if(n > max){
            max = n; 

        } else if(n < min){
            min = n; 
        }  
    } else {
        max = n; 
        min = n; 
        count = 1; 
    }  

  }while(n > 0); 

  printf("Min: %.4f Max: %.4f\n", min, max);

  return 0; 
}

Also, the proper type for float is %f.

  • Hi there and welcome to SO! It will definitely help the asker if you include your code snippet of how you made it work. – BernardL Oct 23 '18 at 17:20
0

Make sure you read the problem description again, your logic in the code is tainted in comparison to what's being attempted.

According to the problem description, n can be an int as you're using it to define the number of values to be given to the program.

You can then use a for loop (for(i=0; i < n; i++){CODE HERE}) to gather more input from the user. The first number given should be your base value for the min and max values.

After you have a base value you can compare min and max to each input thereafter. (ie. if(max < input) {max = input} and if(min > input) {min = input})

If you have anymore questions feel free to inbox me and I'll try to help you work out the problem :)

Brady Ward
  • 124
  • 7
  • Hello, I have updated my question. I would appreciate it if you would reassess it again – X Lefora Oct 23 '18 at 17:29
  • Hey Lefora, sorry for the late reply, work duties! I re read the problem description and it seems somewhat confusing what they're expecting for white space. There's a few ways you could do it, although I'm not sure why we'd want to make such a complicated process for what could be a simple task, especially since scanf is limited to the parameters you define and stops reading after it encounters whitespace. The method I described in my answer is the "easier" way to do it, but would not give you multiple values on one line. – Brady Ward Oct 23 '18 at 19:43
0
  1. Using math.h you can use INFINITY and -INFINITY. http://pubs.opengroup.org/onlinepubs/007904975/basedefs/math.h.html
  2. Comparing doubles with literals in order to make decisions is tricky at best. Besides it would be better to have positive as well as negative numbers in your program.
  3. fflush(stdin) gets rid of any previous input in the buffer.
  4. Notice that max starts at -INFINITY and min starts at +INFINITY.
  5. Comparing a float with a double will result in the calculation being performed using the double type anyways. Try not to mix these without a good reason.

#include <stdio.h>
#include <math.h>

int main()
{
    double min = +INFINITY;
    double max = -INFINITY;
    double n;
    char ans = 'n';

    do
    {
        printf("Enter a number: ");
        scanf("%lf", &n);

        if(n > max)
        {
            max = n;
        }

        if(n < min)
        {
            min = n;
        }
        fflush(stdin);  // gets rid of previous input
        printf("Another number? (y/n): ");
        scanf("%c", &ans);
    }
    while(ans == 'y');

    printf("Min: %.4f Max: %.4f\n", min, max);
    return 0;
}

Output:

Enter a number: -45.6
Another number? (y/n): y
Enter a number: 23.0
Another number? (y/n): y
Enter a number: 92.3
Another number? (y/n): y
Enter a number: -100.22
Another number? (y/n): n
Min: -100.2200 Max: 92.3000

Process returned 0 (0x0)   execution time : 15.805 s
Press any key to continue.

  • User input is not just one number per line. If you have seen the sample input/output from the problem, recall that the user could input: 6 4 2 8 -2 0 99 – X Lefora Oct 23 '18 at 17:33
  • 1
    `fflush(stdin)` is UB. [Using fflush(stdin)](https://stackoverflow.com/q/2979209/2410359) – chux - Reinstate Monica Oct 23 '18 at 17:48
  • fflush is least undefined on windows. A scanf with a space would have been much better and portable. Thanks for the comment. –  Oct 24 '18 at 03:51