# Thread: having trouble using loops

1. Ah yes, loops. Didn't think of that >_<
Always initalize variables, so initialize min and max to 0x80000000 (or just INT_MIN) (max negative value for ints). That should take care of some problems.

2. Elysia, you don't want to initialize "min" to the smallest possible number, right?

I quite like dwks suggestion of using the first element in the array - it's an easy option [and you get one less compare - which will improve the performance in this case by 10% - that must be the clincher, right? ]

--
Mats

3. Originally Posted by Elysia
Always initalize variables, so initialize min and max to 0x80000000 (or just INT_MIN) (max negative value for ints). That should take care of some problems.
It's a bad idea to use constant values for INT_MIN. Of course, if you must insist, this gives you an approximate negative value:
Code:
`-((unsigned)-1 / 2 - 1)`
Leave off the -1 and you get the absolute minimum for ints on two's complement machines (which are the most common), but break it on other machine types.

Or something that would work for one- and two's complement machines:
Code:
```#define MIN(x) ((x) < (y) ? (x) : (y))
#ifndef INT_MIN
#define INT_MIN MIN((int)-((unsigned)-1 / 2), (int)-((unsigned)-1 / 2 - 1))
#endif```
Or just be standard and portable and simple and use INT_MIN!
Code:
```#include <climits>
/* ... */
int min = INT_MIN;```

4. Originally Posted by matsp
Elysia, you don't want to initialize "min" to the smallest possible number, right?
Well, now that I think of it, maybe not. Perhaps the biggest number, INT_MAX would be better. And INT_MIN for the lowest, of course.

5. If you are finding the min value after finding the max value of the numbers entered by the user, just set min = max, after the max loop and before the min loop. then you will have both without a lot of fuss.

6. D'oh . . . *brain freeze*

In other words, you want
Code:
`int max = INT_MIN, min = INT_MAX;`
Confusing, no?

Yes, indeed, initializing both elements to num[0] is a much better idea, assuming that you know that num[0] has a value. And of course, if you do this, the loop can start at 1 instead of 0, saving an iteration, as matsp has obsevered.

 clegs' idea would work, but it depends on at least one element being in the num[] array as well. So using INT_[MIN|MAX] would work as well -- and might be more efficient because those values are constants. Plus you could save that one loop iteration with this method as well! [/edit]

7. Originally Posted by clegs
If you are finding the min value after finding the max value of the numbers entered by the user, just set min = max, after the max loop and before the min loop. then you will have both without a lot of fuss.

That would be my suggestion.
But you may just as well use the first element of the array - as that saves one compare, and is more likely to be the min value - the max value is definitely NOT the min value [unless all values are equal, in which case the first value will also be the smallest and largest].

--
Mats

8. Originally Posted by dwks
D'oh . . . *brain freeze*

In other words, you want
Code:
`int max = INT_MIN, min = INT_MAX;`
Confusing, no?
No, not at all! Looks perfectly fine and dandy!
Btw, since we don't know which element of the array will hold the maximum or minimum value of all the entered ones, I believe it's quite a risk to do much else than using those constants to initalize max/min.

9. No this is what I mean:

for (int i= 0; i < 10; i++)
if (num[i] > max)
max = num[i];
cout << "The highest number is " << max<< endl;

min = max;

for (int j= 0; j < 10; j++)
if (num[i] < min)
min = num[i];
cout << "The lowest number is " << min<< endl;

If finding the max works then use it as the highest number, print it out, then set the max value to min and start the min loop. You will find the min of all numbers entered by the user.

10. Originally Posted by dwks
 clegs' idea would work, but it depends on at least one element being in the num[] array as well. So using INT_[MIN|MAX] would work as well -- and might be more efficient because those values are constants. Plus you could save that one loop iteration with this method as well! [/edit]
Yes it works - but if the array is EMPTY, the min/max value is surely "undefined" - an empty array can not have a min or max value, right? It's a bit like "divide by zero": "every possible answer is the right and wrong answer at the same time". [You could of course DEFINE that IN CASE OF EMPTY ARRAY, the max/min values are DEFINED to be [10000, -10000] or [0,0] or [INT_MIN, INT_MAX] or whatever - but an empty array contains nothing, so it can't have a min or max value].

--
Mats

11. Originally Posted by clegs
No this is what I mean:

for (int i= 0; i < 10; i++)
if (num[i] > max)
max = num[i];
cout << "The highest number is " << max<< endl;

min = max;

for (int j= 0; j < 10; j++)
if (num[i] < min)
min = num[i];
cout << "The lowest number is " << min<< endl;

If finding the max works then use it as the highest number, print it out, then set the max value to min and start the min loop. You will find the min of all numbers entered by the user.
Sure, it works But if we do:
Code:
```min = max = num[0];
for (int i= 1; i < 10; i++) {
if (num[i] > max)
max = num[i];
if (num[i] < min)
min = num[i];
}
cout << "The highest number is " << max<< endl;

cout << "The lowest number is " << min<< endl;```
We save a couple of comparisons (no need to compare num[0], and almost CERTAINLY change it), and end up with exactly the same result. By doing both compares in the same loop, we save some more computation too - there's no point in searching for the max separately from min - we still need to compare all [but the first] elements.

Of course, saving one iteration of a loop of 10 is 10%, but if the loop is 10000, it's somewhat less of a saving... [0.01%]. Doing one loop instead of 2 is a good saving in both cases. For large loops, on a compiler that allows it, you may also want to consider saying "unlikely" to the if-statement, because unless you have a set of numbers that are continually rising or falling, likelyhood is that you pretty soon have a number that is at least close to max or min, and rarely go into the "true" side of the if.

--
Mats

12. Yes it works - but if the array is EMPTY, the min/max value is surely "undefined" - an empty array can not have a min or max value, right? It's a bit like "divide by zero": "every possible answer is the right and wrong answer at the same time". [You could of course DEFINE that IN CASE OF EMPTY ARRAY, the max/min values are DEFINED to be [10000, -10000] or [0,0] or [INT_MIN, INT_MAX] or whatever - but an empty array contains nothing, so it can't have a min or max value].
But of course. If you can deal with empty arrays, then it's much easier to do so before you go calculating the minimum or maximum or average or whatever of the elements in the array.
Code:
```if(elements == 0) { /* empty */ }
else { /* calculate min/max/avg/whatever */ }```
On the other hand, if you must have a value, for example with
Code:
`int array_min(int array[], size_t elements)`
then you might have to return INT_MIN to deal with an empty array.

There's yet another disadvantage of using INT_MIN and/or INT_MAX. If all of the elements in the array are INT_MIN, for example, then the calling function couldn't tell the difference between this situation and the case of zero elements.

All told, it's best to use min = num[0]. No issues about an empty set of numbers actually having a minimum, no possible problems with very high or very low numbers, and greater efficiency!

13. So why would the program get to the point of finding the min value if the array was empty, wouldn't that be a discovery in finding the max (or before with an assert statement)? I was just adding a possible solution of finding a starting point for finding min, use the max value in the array which was found in the previous loop.

I am just a student, and that is my simplistic view. Sorry!

14. You're right, it's a viable solution. It's just that using INT_whatever is possibly more efficient, and works as well. And min = num[0] is more efficient still, assuming the array isn't empty.

15. Originally Posted by clegs
I am just a student, and that is my simplistic view. Sorry!
We're not blaming you for anything. Your suggest was not a bad one, though if you initialize max/min, it probaly wouldn't be necessary.
matsp's suggestion was the best one. We were all outsmarted