Home > Software design >  trying to find the maximum and minimum of numbers inputted into the command line
trying to find the maximum and minimum of numbers inputted into the command line

Time:10-21

Basically what I'm trying to do is read numbers of the command line and then print the maximum and the minimum number only. It worked for the maximum so my thougth process is basically to just copy that and invert it, but that doesnt work. Any suggestions welcome.

#include <stdio.h>
#include <limits.h>   

int main (int argc, char **argv) 
{
    double max = INT_MIN;
    double min = INT_MAX;

 //maximum   
    for (int i = 1; i < argc; i  ) 
        {                
        double n = 0;                                  
        if (sscanf (argv[i], "%lf", &n) == 1) 
            {      
                if (n > max)                            
                max = n;                            
            }
        }

 //minimum
    for (int j = 1; j > argc; --j) 
    {               
        double m = 0;                                  
        if (sscanf (argv[j], "%lf", &m) == 1) 
            {      
                if (m < min)                        
                min = m;                            
            }
    }

    printf ("max: %lf\nmin: %lf", max, min);
}

Would also be appreciated for hints on how to do a total for numbers read on the command line. For example the output would be something like: 11 numbers read; max: 9.52; min: -13.47.

CodePudding user response:

You seem to be mixing up two principles here, let's shed some light, using this example for input values:

4 6 8 2

Result:

min : 2
max : 8

How to get them? (Pseudo-code)

int max = INT_MIN;
int min = INT_MAX;

for (int index = 0; index < argc; index  )
{
  if (argv[index] > max) 
    { max = argv[index]; }
  if (argv[index] < min) 
    { min = argv[index]; }
  write_output(index, max, min);
}

When running this, these will be the values of max and min:

index    max    min
    0      4      4
    1      6      4
    2      8      4
    3      8      2  => final values

What's the story with inversing about? Well, imagine that, instead of just looking for maximum and minimum, you are sorting the values, then you can simply pick the maximum and minimum at both ends of the sorted result (or take the same end when sorting in the inverse order). As, however, in this particular case, you are not sorting, there is no need to inverse anything.

CodePudding user response:

Using @Dominique answer, and your method of handling inputs arguments, I would do something like this:

#include <stdio.h>
#include <limits.h>   

int main (int argc, char **argv)  {
    double max = INT_MIN;
    double min = INT_MAX;
    int correctly_read_numbers = 0;

    for (int i = 1; i < argc; i  ) {               
        double n = 0;
        int ret = sscanf (argv[i], "%lf", &n);
        if (ret == 1) {      
            if (n > max)                            
                max = n; 
            if (n < min)
                min = n;
            correctly_read_numbers  ;
        }
    }
    printf ("%d numbers read; max: %lf\nmin: %lf", correctly_read_numbers, max, min);
}

I have not compiled neither tried this code, but it seams to work the way you want.

It's a good practice to avoid as much as possible unnecessary loops. In that case it's not much of a problem, but when coming to the case of nested loops it could become a real time consuming process.

  •  Tags:  
  • c
  • Related