Thread: simple problem for ' min & max'

  1. #16
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    739
    @rusyoldguy

    The OP didn't ask about "Standard Deviation". The OP is a beginner, and is learning C, programming a simple program.

    As for your more advanced program, first I would never assume a fixed number of entries. What if redirecting input from a file with unknown number of values? For the OP's simple program, setting the number of values to be entered is fine.

    And, secondly, I would never set min to some predefined high value! However if you do, please use the predefined value for the compiler in float.h. "FLT_MIN" or "FLT_MAX"

    min = max = first input number!!!

  2. #17
    Registered User
    Join Date
    Nov 2018
    Posts
    7
    Thanks for rstanley explained this is looping program. And thanks rusyoldguy told me the min value. And thanks all.
    Finally, I am trying to write "how many times of inputs are better than twice inputs for the last time".

    Actually, this looping program can be for the user choose their enter times, as entering 10 times, then this program will get 10 inputs, then calculate the min, max, average...etc. Then find out the"how many times of inputs are better than twice inputs for the last time" in 10 times.
    Last edited by kelu; 11-24-2018 at 11:44 AM.

  3. #18
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote Originally Posted by rstanley View Post
    A min-max program implies more than 2 numbers. A ternary statement should only be used for testing two numbers.
    Three variables I think is also ok, and it work too, but with four I have a problem. This is just an interesting exercise, of course. Three was easy, but four? - Somewhere exist an error in reasoning.

    Maybe I should open a new thread?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {     
      int i=0;
      int min, max, max2, zahl;
        int number_comp[4];
        int *zgr = &number_comp[0];   
      
        for(int j = 0; j < 4; j++)
        {
            printf("Input number: ");
            scanf("%d", &zahl);
            number_comp[j] = zahl;
        }
     
     max = (zgr[0] > zgr[1] ? ((zgr[0] > zgr[2]) ? zgr[0] : zgr[2]) : zgr[1]);
     printf("Von 3 Zahlen die groesste: %d\n\n", max);
     
     //Runs not always correct
     max2 = (zgr[0] > zgr[1] ? ((zgr[0] > zgr[2]) ? ((zgr[0] > zgr[3]) ? zgr[0] : zgr[3]) : zgr[1]) : zgr[3]);
     printf("Von 4 Zahlen die groesste: %d\n", max2);
     
        return(0);
    }

  4. #19
    Registered User
    Join Date
    Dec 2017
    Posts
    965
    Max of 4 using the conditional operator.
    Code:
    #include <stdio.h>
     
    int main() {
        int a, b, c, d;
        scanf("%d%d%d%d", &a, &b, &c, &d);
        int m = a >= b
                ? a >= c
                  ? a >= d ? a : d
                  : c >= d ? c : d
                : b >= c
                  ? b >= d ? b : d
                  : c >= d ? c : d;
        printf("%d\n", m);
    }
    If you want the truth to stand clear before you, never be for or against. - Sent-ts'an

  5. #20
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    739
    Quote Originally Posted by Kernelpanic View Post
    Three variables I think is also ok, and it work too, but with four I have a problem. This is just an interesting exercise, of course. Three was easy, but four? - Somewhere exist an error in reasoning.

    Maybe I should open a new thread?
    ...
    I think that would be a good idea, as this goes beyond the OP's original question.

  6. #21
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    739
    @Kernelpanic and @john.c

    The use of nested ternary operators is not recommended, ESPECIALLY, more than two.

    A min - max algorithm is much better for the programmer, and maintainer, let alone for a less experienced programmer. Plus, it makes for a generic function where you can send in an array of numbers of various lengths, to be added to your personal or company library.

  7. #22
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Last post for this "just for fun"-problem. Thanks John.C now it runs correct; with minimum too.

    Three numbers is really maximum more are nonsense.
    @rstanley, for a practical application, a properly program for any numbers is better, of course.

    For fun :
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {     
      //int i=0;
      int min, min2, max, max2, max3, zahl;
        int number_comp[4];
        int *zgr = &number_comp[0];   
      
        for(int j = 0; j < 4; j++)
        {
            printf("Input number: ");
            scanf("%d", &zahl);
            number_comp[j] = zahl;
        }
        
        max = (zgr[0] > zgr[1] ? ((zgr[0] > zgr[2]) ? zgr[0] : zgr[2]) : zgr[1]);
        printf("Von 3 Zahlen die groesste: %d\n\n", max);
     
        max2 = zgr[0] >= zgr[1] 
                        ? zgr[0] >= zgr[2]
                        ? zgr[0] >= zgr[3] ? zgr[0] : zgr[3]
                        : zgr[2] >= zgr[3] ? zgr[2] : zgr[3]
                        : zgr[1] >= zgr[2]
                        ? zgr[1] >= zgr[3] ? zgr[1] : zgr[3]
                        : zgr[2] >= zgr[3] ? zgr[2] : zgr[3];
        printf("Von 4 Zahlen die groesste: %d\n\n", max2);
            
        max3 = zgr[0] >= zgr[1] ? zgr[0] >= zgr[2] ? zgr[0] >= zgr[3] ? zgr[0] : zgr[3]    : zgr[2] >= zgr[3] ? zgr[2] : zgr[3] : zgr[1] >= zgr[2] ? zgr[1] >= zgr[3] ? zgr[1] : zgr[3] : zgr[2] >= zgr[3] ? zgr[2] : zgr[3];
        printf("Von 4 Zahlen die groesste: %d\n\n", max3);
        
        min2 = zgr[0] <= zgr[1] ? zgr[0] <= zgr[2] ? zgr[0] <= zgr[3] ? zgr[0] : zgr[3]    : zgr[2] <= zgr[3] ? zgr[2] : zgr[3] : zgr[1] <= zgr[2] ? zgr[1] <= zgr[3] ? zgr[1] : zgr[3] : zgr[2] <= zgr[3] ? zgr[2] : zgr[3];
        printf("Von 4 Zahlen die kleinste: %d\n\n", min2);
        
        return(0);}

  8. #23
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Max-Min with an array for any numbers.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    //Quantity
    #define ANZAHL 5 
    
    
    int maximum_aus_feld(int *eingabe, int groesse);
    int minimum_aus_feld(int *eingabe, int groesse);
    
    
    int main(void)
    {
        int zahl;  
        int number_comp[ANZAHL];
        int *zgr = &number_comp[0];   
      
        for(int j = 0; j < ANZAHL; j++)
        {
            printf("Input number: ");
            scanf("%d", &zahl);
            number_comp[j] = zahl;
        }
        printf("Maximum = %d -- Minimum = %d\n", maximum_aus_feld(zgr, ANZAHL), 
                                                 minimum_aus_feld(zgr, ANZAHL));
        
        return(0);
    }
    
    
    int maximum_aus_feld(int *eingabe, int groesse)
    {
        //Auf Anfang setzen - Put to the beginning
        int maximum = eingabe[0], i;
        for(i = 1; i < groesse; i++) 
        {
            //If "eingabe[]"(Input) greater than maximum is eingabe[]
            if(eingabe[i] > maximum)
            {
                maximum = eingabe[i];
            }
        }
        return maximum;
    }
    
    
    int minimum_aus_feld(int *eingabe, int groesse)
    {
        int minimum = eingabe[0], i;
        for(i = 1; i < groesse; i++) 
        {
            //The same like maximum only "<"
            if(eingabe[i] < minimum)
            {
                minimum = eingabe[i];
            }
        }
        return minimum;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple problem, at least I think it should be simple
    By knik653 in forum C Programming
    Replies: 7
    Last Post: 10-31-2018, 01:05 PM
  2. A very simple problem but enough difficult | Loop problem
    By Kevinphp7 in forum C++ Programming
    Replies: 13
    Last Post: 06-10-2015, 11:03 AM
  3. Simple program, simple problem
    By KAUFMANN in forum C Programming
    Replies: 5
    Last Post: 02-16-2011, 01:16 PM
  4. Simple program...simple problem?
    By deadherorising in forum C Programming
    Replies: 2
    Last Post: 03-12-2009, 08:37 PM
  5. Simple program, not so simple problem
    By nolsen in forum C++ Programming
    Replies: 2
    Last Post: 01-18-2008, 10:28 AM

Tags for this Thread