### INT_MIN, INT_MAX

I am attempting to sort the max and min values of an array. I read about <climits>, but I am unclear how to use INT_MAX and INT_MIN. What is the proper syntax for using these commands?
INT_MAX and INT_MIN are both constants; they stand for the maximum and minimum values that int can hold. These have nothing to do with finding maximum and minimum values within an array.

To get the maximum value in an array of ints, you can use a for loop with a variable that holds the highest-found number. Every time it loops, check if the next number in the array is bigger than the previously-set maximum. If it is, set it to that number and repeat. After the loop ends, the variable will be set to the highest number in the array.
Last edited on
So, currently my code reads :
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include #include #include using namespace std; /* Write a program that asks the user for 10 values that are stored in an array. Once the data is entered, write functions that find the average value, maximum value, and minimum value. (If you find this too easy, go ahead and find the median as well, but the test won't have anything so difficult.) */ int main (void) { int i = 0; int iMaths [ 10 ]; int sum = 0; float ave = 0; int max,min; max = min = iMaths [0]; for (i = 0 ; i < 10 ; i++) { cout << "Enter value " << i << endl; cin >> iMaths [ i ]; if (iMaths [i] > max) { max = iMaths [i]; } if (iMaths [i] < min) { min = iMaths [i]; } } for ( i =0 ; i < 10 ; i++) { cout << "Values entered " << i << " = " << iMaths [i] << endl; sum = sum + iMaths[i]; ave = sum/10; } cout << "The sum of all 10 numbers is " << sum << endl; cout << "The average is " << ave << endl; cout << "The largest value entered is = " << max << endl; cout << "The smallest value entered is = " << min << endl; return 0; }``````

Which outputs:

 ``` Values entered 0 = 4 Values entered 1 = 5 Values entered 2 = 6 Values entered 3 = 7 Values entered 4 = 8 Values entered 5 = 9 Values entered 6 = 6 Values entered 7 = 5 Values entered 8 = 4 Values entered 9 = 3 The sum of all 10 numbers is 57 The average is 5 The largest value entered is = 1606417240 The smallest value entered is = 3 ```

Overall, I think I would rather use <climits> and an algorithm that someone else already wrote. Surely it would be more efficient than my stuff. Feel free to explain why my max is so bonkers though...
And nicely enough, you can use 2 if statements in the same for loop, one for min and one for max.
 To get the maximum value in an array of ints, you can use a for loop with a variable that holds the highest-found number. Every time it loops, check if the next number in the array is bigger than the previously-set maximum. If it is, set it to that number and repeat. After the loop ends, the variable will be set to the highest number in the array.

That is what I did. I don't understand why the max isn't working though?

 INT_MAX and INT_MIN are both constants; they stand for the maximum and minimum values that int can hold. These have nothing to do with finding maximum and minimum values within an array.

Ok, this makes sense. Max and min values seem pretty simple; surely there is already a preprocessor library for this? Rather than re-inventing the wheel (poorly, based on my code's failure), I'd like to learn how to use functionality that already exists.
 That is what I did. I don't understand why the max isn't working though?

 ``123`` ``````int iMaths [ 10 ]; int max,min; max = min = iMaths [0];``````

That isn't what you did. Initially you set min and max to some arbitrary value that was stored in the uninitialized iMaths array. It happened to be higher than any values you entered.

The correct way to do this is to set max and min to iMaths[0] after iMaths is populated by the values you're going to be testing.
If I define min and max to iMaths [0] after it is populated, then won't they just output the value that resides in iMaths [0]? I thought it was done before hand because unpopulated arrays are all zeroed out when they are initialized?
Combining all knowledge from this thread, maybe like this?
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````#include #include #include using namespace std; /* Write a program that asks the user for 10 values that are stored in an array. Once the data is entered, write functions that find the average value, maximum value, and minimum value. (If you find this too easy, go ahead and find the median as well, but the test won't have anything so difficult.) */ int main (void) { int i = 0; int iMaths [ 10 ]; int sum = 0; float ave = 0; int max = INT_MIN; int min = INT_MAX; for (i = 0 ; i < 10 ; i++) { cout << "Enter value " << i << endl; cin >> iMaths [ i ]; if (iMaths [i] > max) { max = iMaths [i]; } if (iMaths [i] < min) { min = iMaths [i]; } } for ( i =0 ; i < 10 ; i++) { cout << "Values entered " << i << " = " << iMaths [i] << endl; sum = sum + iMaths[i]; ave = sum/10; } cout << "The sum of all 10 numbers is " << sum << endl; cout << "The average is " << ave << endl; cout << "The largest value entered is = " << max << endl; cout << "The smallest value entered is = " << min << endl; return 1; }``````
 If I define min and max to iMaths [0] after it is populated, then won't they just output the value that resides in iMaths [0]?

If you did it the way you were doing it, yes. The obvious solution would be to do determine the min/max after it was populated instead of as you were getting the input. If you want to do it while you're getting the input, you need to set them both to the first input you get, before you get any more. (Alternately you could set min to std::numeric_limits<int>::max() and max to std::numeric_limits<int>::min(). Or you could use the C versions you asked about in the original post.)

 I thought it was done before hand because unpopulated arrays are all zeroed out when they are initialized?

Your array wasn't initialized, and if it were intialized to 0, your code still wouldn't have produced the right output. min would've been... 0.
What you should be doing is separate the comparisons outside of the loop that you use to input the values. In other words, use another loop.
On line 21 `max = min = iMaths [0];`
the values of max and min, which have not yet been initialised, are set equal to iMaths [0]. That's somewhat pointless as the array at that point hasn't been initialised.

What you need to do if setting max and min at this stage, is to set min bigger than any possible data value, and set max smaller than any possible data value.

For example, you could use this:
 ``12`` `````` max = -1; min = 10000;``````

or even this:
 ``12`` `````` max = INT_MIN; min = INT_MAX;``````

Edit: I see that while I was typing, you already did this.
Last edited on
Topic archived. No new replies allowed.