# 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. ## 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. >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.

3. 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. 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. ## 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. ## 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. ## 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. ## 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. ## 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.