Thread: simple problem for ' min & max'

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    7

    simple problem for ' min & max'

    Hello,

    I am a C program beginner. I want to ask how to find out min & max value. Because I tried over a week but still cannot find out what is wrong.
    Code:
    #include <.h>
    int main(){
        
        int i=0;
        float min,max,input;
        
        printf("input number");
        scanf("%f",&input);
        input=min=max;
    
        do{
        printf("input number");
        scanf("%f",&input);
        
        if(input< min){
      input=min;
      }
    
      if(input> max){
        input=max;
        }
      i=i+1;
      }while(i<3);
    
    printf("max %f and min %f", max, min);
    
    return 0;
    }
    I would like to ask what the problem is, because the results always are 0.
    Even I tried to set min =9999 and max = -9999, but the results also are 9999 and -9999.

    Thank You

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    Try this way round instead.
    min = input;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    7
    You mean's use 'instead' replaced to 'input=min=max;' ? I tried, but the result is same.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,914
    What exactly did you try?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,165
    Quote Originally Posted by kelu View Post
    You mean's use 'instead' replaced to 'input=min=max;' ? I tried, but the result is same.
    Well do you consider assignment to be like
    3 = input;

    or

    input = 3;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    58
    Hi kelu!

    Better you read at first all values in, for example diameters of parts made by a lathe-machine.
    Then you search the max value in the array.
    Now set min to max.
    After this you can find min.

    example:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(){
    
        int i=0;
        float min,max, average, range, measurments[5];
    
    
    
        /** first set all elements of the array to zero*/
        max = average = range = 0;
        for (i = 0; i < 5 ; i++)
         measurments[i] = 0;
    
        /**then read in the values */
        for (i = 0; i < 5 ; i++)
         {
          printf("input number");
          scanf("%f",&measurments[i]);
         }
    
         /** now get max needed to calculate min*/
         for (i = 0; i < 5 ; i++)
          {
           if (measurments[i] > max) max = measurments[i];
           average += measurments[i];
          }
    
          /** calculate average*/
          average /= 5;
    
          /** important: set min to maximum*/
          min = max;
    
         /** get min */
         for (i = 0; i < 5 ; i++)
          {
           if (measurments[i] < min) min = measurments[i];
          }
          /**calculate the range after get min */
          range = max - min;
    
      printf("max: %f    min: %f   average: %f   range: %f\n", max, min, average, range);
    
    return 0;
    }
    Try and see.

  7. #7
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    738
    1) Read the first input value.
    2) set min and max to the first value.
    3) Read in the remaining values and test against min and max, and adjust min and/or max accordingly.
    4) Done.

    No need to make it complicated.

  8. #8
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Take a good book for C . . . Max - Min is one of the first exercises for beginners.

    Code:
    //Maximum - Minimum
    
    
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int main(void)
    {
        int a, b, z;
        
        printf("\nGeben Sie zwei Zahlen ein a <Leertaste> b: ");
        scanf("%d %d", &a, &b);
        
        if(a == b)
        {
            printf("\nBeide Zahlen sind gleich gross!");
            exit(0);
        }
        else 
        {
            //Tenärer Operator: ((a > b) ? a : b)
            printf("\nDie groessere Zahl ist %d", z = ((a > b) ? a : b));
        }
        return(0);
    }

  9. #9
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    738
    Quote Originally Posted by Kernelpanic View Post
    Take a good book for C . . . Max - Min is one of the first exercises for beginners.
    The code you present (In German, not English as is normally used here.) only determines the max of two values.

    The OP's code currently is for 3 numbers, but what if the number of inputs is changed to 10, 20, or more?

    Th OP is on the right track, but does need correction, and better indentation.

  10. #10
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    Quote Originally Posted by rstanley View Post
    The OP's code currently is for 3 numbers, ...
    Three numbers? Where?

    ... but what if the number of inputs is changed to 10, 20, or more?
    Can he do, but does not belong to his question.

  11. #11
    Registered User
    Join Date
    Nov 2018
    Posts
    7
    Thanks, Salem, I changed the max=input. It makes the program can be output the max result. And I still hard work to find out the result for min, and others requirements for this program; as max & min just a small part in my assignment.
    Last edited by kelu; 11-23-2018 at 10:39 AM.

  12. #12
    Registered User
    Join Date
    Nov 2018
    Posts
    7
    Thanks for your help, As I known this program can be done by <stdio.h> only, also no need to use Array.
    I am reading this post every day. And also modifying this program.

  13. #13
    Registered User Kernelpanic's Avatar
    Join Date
    Sep 2018
    Location
    Berlin
    Posts
    105
    No offense meant. But in all my books about C is this min-max program gladly with the "tenaeren Operator"; authors like it. Without one or more books about a programming language one can not learn it. Good luck!

  14. #14
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    738
    Quote Originally Posted by Kernelpanic View Post
    Three numbers? Where?

    Can he do, but does not belong to his question.
    Code:
       do{
    ...
       }while(i<3);
    Actually he does input 4 numbers, one before the do while loop, and 3 inside.

    A min-max program implies more than 2 numbers. A ternary statement should only be used for testing two numbers.

  15. #15
    Registered User
    Join Date
    Nov 2018
    Location
    Amberg in upper palatinate, Bavaria
    Posts
    58
    If you don't save the values you've put in, you can only calculate min, max, average, range, not the standard deviation.
    If you don't need the standard deviation it is enough to us a code like this.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(){
    
        int i, n = 3;
        float min, max = 0, sum = 0, average = 0, range = 0, measurments;
    
    
        min = 3.4028234E38;
    
        printf("\nnumber of elements: ");
          scanf("%d", &n);
    
        /**then read in the values */
        for (i = 0; i < n ; i++)
         {
          printf("\ninput number %d: ", i);
          scanf("%f",&measurments);
          sum += measurments;
          if (i > 0)
           {
            average = sum / (i + 1);
            range = max - min;
           }
          if (measurments > max) max = measurments;
          if (measurments < min) min = measurments;
          printf("max: %f    min: %f   average: %f   range: %f\n", max, min, average, range);
         }
    
    
    
    return 0;
    }
    But it is useful when you compare more random samples by working in quality assurance
    that the random samples have the same size(number of elements).

    example:
    To control a diameter of a bore in a part made by a lathe machine
    needs to have the same size of the number of parts (may be 5 or 6 parts)
    you control every 2 hours or after produce 200 parts.

    to compare the standard deviation or other values of
    a random sample witch includes 5 workpieces
    a random sample including 12 workpieces
    and
    a random sample including 8 workpieces

    It would be a delusion of wrong statistical variances.

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