Thread: having trouble using loops

  1. #16
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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. #17
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #18
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by Elysia View Post
    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;
    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.

  4. #19
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by matsp View Post
    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.
    Last edited by Elysia; 11-28-2007 at 03:01 PM.

  5. #20
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    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. #21
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

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

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by clegs View Post
    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
    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.

  8. #23
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by dwks View Post
    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.
    Last edited by Elysia; 11-28-2007 at 03:13 PM.

  9. #24
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    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. #25
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dwks View Post
    [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]
    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.

  11. #26
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by clegs View Post
    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
    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.

  12. #27
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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!
    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.

  13. #28
    Registered User
    Join Date
    Sep 2007
    Location
    Arizona
    Posts
    164
    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. #29
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    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.

  15. #30
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by clegs View Post
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. trouble scanning in... and link listing
    By panfilero in forum C Programming
    Replies: 14
    Last Post: 11-21-2005, 12:58 PM
  4. a little trouble with for loops
    By melee in forum C Programming
    Replies: 6
    Last Post: 10-19-2004, 01:46 AM
  5. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM