MAX and MIN

This is a discussion on MAX and MIN within the C Programming forums, part of the General Programming Boards category; Why isn't INT_MIN and INT_MAX returning the numbers I want them to? What exactly am I doing wrong? Code: #include ...

  1. #1
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Question MAX and MIN

    Why isn't INT_MIN and INT_MAX returning the numbers I want them to? What exactly am I doing wrong?

    Code:
    #include <stdio.h>
    #include <limits.h>
    
    main()
    {
    	int count=0;
    	int sum=0;
    	int input=0;
    
    	printf("Enter a number\n");
    	
    	while (scanf("%d",&input)!=EOF)
    	{
    		sum+=input;
    		count++;
    
    		if(count==20)
    			break;
    
    		printf("Enter another number\n");
    	}
    	
    	printf("The total of all entered numbers is %d\n",sum);
    	
    	printf("The number of entries are %d\n",count);
    	
    	printf("The minimum value entered is %d\n",INT_MIN);
    	
    	printf("The maximum value entered is %d\n",INT_MAX);
    	
    	printf("The average of all the numbers is"); 
    	
    	printf(" %d with a remainder of %d\n\n",sum/count,sum%count);
    }

  2. #2
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >What exactly am I doing wrong?
    INT_MIN and INT_MAX are the smallest and largest possible values for the integer type, not the smallest and largest numbers entered in your program.
    *Cela*

  3. #3
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357
    Originally posted by Cela
    >What exactly am I doing wrong?
    INT_MIN and INT_MAX are the smallest and largest possible values for the integer type, not the smallest and largest numbers entered in your program.
    So what do I use to find the smallest and largest numbers in my program?

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Declare two new variables. One is for the smallest value and one is for the largest value. Then check each value the user entered against these values and change them as necessary.

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    You'll need...

    You'll need to check, if the current number (the number that you received now) is bigger than the biggest number from previous input.

    Code:
    int nBiggest, nNumInput;
    
    //get the number here
    if(nNumInput > nBiggest)
      nBiggest = nNumInput;
    //continue the program here
    And for the minumum value, you should get an idea how to make it, by reading what I wrote for the max number

  6. #6
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Smile Re: You'll need...

    Originally posted by Vber
    You'll need to check, if the current number (the number that you received now) is bigger than the biggest number from previous input.

    Code:
    int nBiggest, nNumInput;
    
    //get the number here
    if(nNumInput > nBiggest)
      nBiggest = nNumInput;
    //continue the program here
    And for the minumum value, you should get an idea how to make it, by reading what I wrote for the max number
    Thank you, Vber. The program worked fine after I did what you said.

    Isn't there a way, though, to do the same thing using INT_MIN and INT_MAX?

  7. #7
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    No, not exactly.

    I don't understand why you need to use INT_MIN and INT_MAX, this is just to indicate the min and max value of an integer, not the min and max value of the input from your program.

  8. #8
    Un Artiste Extraordinaire volk's Avatar
    Join Date
    Dec 2002
    Posts
    357

    Re: No, not exactly.

    Originally posted by Vber
    I don't understand why you need to use INT_MIN and INT_MAX, this is just to indicate the min and max value of an integer, not the min and max value of the input from your program.
    I just thought that it was considered a good thing when a programmer knows how to solve a problem in as many ways as possible.

    Are you saying that it's impossible to do that with INT_MIN and INT_MAX, or are you saying that it's just not worth it, and the other way is much better?

  9. #9
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Hmmm...

    I just thought that it was considered a good thing when a programmer knows how to solve a problem in as many ways as possible.
    You're right, of course is good.
    But in this case, I don't see why to use INT_MIN and INT_MAX to solve this problem.



    Are you saying that it's impossible to do that with INT_MIN and INT_MAX, or are you saying that it's just not worth it, and the other way is much better?
    You cannot change the value of INT_MIN or INT_MAX, they are declared and their value cannot be changed, so with this info, you cannot solve this problem using INT_MIN or INT_MAX, and if you can, you'll need to make some math operations, and you'll just spend time on it

    Another thing about your code, declare your main() function as int: int main() and then, at the end of the program, return an value.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help finding the max and min of a 2d array
    By finalreign in forum C Programming
    Replies: 6
    Last Post: 04-29-2009, 11:39 AM
  2. Max Min Problem
    By smithc2005 in forum C Programming
    Replies: 7
    Last Post: 10-22-2008, 10:38 AM
  3. Ranged numbers
    By Desolation in forum Game Programming
    Replies: 8
    Last Post: 07-25-2006, 10:02 PM
  4. Variable Min and Max Program
    By EdanD in forum C Programming
    Replies: 8
    Last Post: 06-30-2004, 07:48 PM
  5. Double output!
    By Spurnout in forum C++ Programming
    Replies: 3
    Last Post: 11-02-2002, 02:35 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21