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.
This is a discussion on having trouble using loops within the C++ Programming forums, part of the General Programming Boards category; Ah yes, loops. Didn't think of that >_< Always initalize variables, so initialize min and max to 0x80000000 (or just ...
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.
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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:
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.Code:-((unsigned)-1 / 2 - 1)
Or something that would work for one- and two's complement machines:
Or just be standard and portable and simple and use INT_MIN!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
Code:#include <climits> /* ... */ int min = INT_MIN;
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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.
D'oh . . . *brain freeze*
In other words, you want
Confusing, no?Code:int max = INT_MIN, min = INT_MAX;
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.
[edit] 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]
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Last edited by Elysia; 11-28-2007 at 02:13 PM.
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.
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Sure, it works But if we do:
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.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;
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
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
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.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].
On the other hand, if you must have a value, for example withCode:if(elements == 0) { /* empty */ } else { /* calculate min/max/avg/whatever */ }
then you might have to return INT_MIN to deal with an empty array.Code:int array_min(int array[], size_t elements)
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!
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
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!
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.
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.