finding max and min integers - newbie question

Discussion in 'C++' started by john, Jan 20, 2004.

  1. john

    john Guest

    Hi

    I'm a learner and I'm trying to work out the simplest way of finding
    the maximum and minimum value from a group of integers.

    Any help would be appreciated.

    John
     
    john, Jan 20, 2004
    #1
    1. Advertising

  2. john wrote:

    > Hi
    >
    > I'm a learner and I'm trying to work out the simplest way of finding
    > the maximum and minimum value from a group of integers.


    That's simple:

    int arr[arraySize};
    int min = INT_MAX; int max = INT_MIN;
    for ( i = 0; i < arraySize; i++ )
    if ( arr < min ) min = arr;
    else if ( arr > max ) max = arr;

    --
    To get my real email adress, remove the two onkas
    --
    Dipl.-Inform. Hendrik Belitz
    Central Institute of Electronics
    Research Center Juelich
     
    Hendrik Belitz, Jan 20, 2004
    #2
    1. Advertising

  3. john

    David Fisher Guest

    "john" <> asked:

    > I'm a learner and I'm trying to work out the simplest way of finding
    > the maximum and minimum value from a group of integers.


    Assuming they are already in a non-empty vector (std::vector<int>) called
    "vec":

    #include <algorithm>

    ....

    int minValue = *(std::min_element(vec.begin(), vec.end()));
    int maxValue = *(std::min_element(vec.begin(), vec.end()));

    This takes about twice as long as finding them both simultaneously (as in
    Hendrik's solution), but it is simple ...

    David F
     
    David Fisher, Jan 20, 2004
    #3
  4. john

    David Fisher Guest

    "David Fisher" <> wrote:
    >> I'm a learner and I'm trying to work out the simplest way of finding
    >> the maximum and minimum value from a group of integers.

    >
    > Assuming they are already in a non-empty vector (std::vector<int>) called
    > "vec":
    >
    > #include <algorithm>
    >
    > ...
    >
    > int minValue = *(std::min_element(vec.begin(), vec.end()));
    > int maxValue = *(std::min_element(vec.begin(), vec.end()));


    Doh ! Spot the deliberate mistake ... :)

    David F
     
    David Fisher, Jan 20, 2004
    #4
  5. Hendrik Belitz wrote:
    >
    > john wrote:
    >
    > > Hi
    > >
    > > I'm a learner and I'm trying to work out the simplest way of finding
    > > the maximum and minimum value from a group of integers.

    >
    > That's simple:
    >
    > int arr[arraySize};
    > int min = INT_MAX; int max = INT_MIN;
    > for ( i = 0; i < arraySize; i++ )
    > if ( arr < min ) min = arr;
    > else if ( arr > max ) max = arr;
    >


    What would your code give in the case of:

    int arr[] = { 5 };

    min -> 5
    max -> INT_MAX

    Not quite (But the modification is trivial and left
    as an exercise for the OP)!

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jan 20, 2004
    #5
  6. Karl Heinz Buchegger wrote:

    > Hendrik Belitz wrote:
    >>
    >> john wrote:
    >>
    >> > Hi
    >> >
    >> > I'm a learner and I'm trying to work out the simplest way of finding
    >> > the maximum and minimum value from a group of integers.

    >>
    >> That's simple:
    >>
    >> int arr[arraySize};
    >> int min = INT_MAX; int max = INT_MIN;
    >> for ( i = 0; i < arraySize; i++ )
    >> if ( arr < min ) min = arr;
    >> else if ( arr > max ) max = arr;
    >>

    >
    > What would your code give in the case of:
    >
    > int arr[] = { 5 };
    >
    > min -> 5
    > max -> INT_MAX


    No, it won't:

    int arr[] = { 5 };

    min -> 5
    max -> 5

    which is correct

    --
    To get my real email adress, remove the two onkas
    --
    Dipl.-Inform. Hendrik Belitz
    Central Institute of Electronics
    Research Center Juelich
     
    Hendrik Belitz, Jan 20, 2004
    #6
  7. Hendrik Belitz wrote:
    >
    > Karl Heinz Buchegger wrote:
    >
    > > Hendrik Belitz wrote:
    > >>
    > >> john wrote:
    > >>
    > >> > Hi
    > >> >
    > >> > I'm a learner and I'm trying to work out the simplest way of finding
    > >> > the maximum and minimum value from a group of integers.
    > >>
    > >> That's simple:
    > >>
    > >> int arr[arraySize};
    > >> int min = INT_MAX; int max = INT_MIN;
    > >> for ( i = 0; i < arraySize; i++ )
    > >> if ( arr < min ) min = arr;
    > >> else if ( arr > max ) max = arr;
    > >>

    > >
    > > What would your code give in the case of:
    > >
    > > int arr[] = { 5 };
    > >
    > > min -> 5
    > > max -> INT_MAX

    >
    > No, it won't:
    >
    > int arr[] = { 5 };
    >
    > min -> 5
    > max -> 5
    >
    > which is correct


    Run the program and try it!
    In the above max will never be set, since there
    is an else in it!

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jan 20, 2004
    #7
  8. On Tue, 20 Jan 2004 11:49:36 +0100, Karl Heinz Buchegger wrote:
    > Hendrik Belitz wrote:
    >> >> int arr[arraySize};
    >> >> int min = INT_MAX; int max = INT_MIN;
    >> >> for ( i = 0; i < arraySize; i++ )
    >> >> if ( arr < min ) min = arr;
    >> >> else if ( arr > max ) max = arr;
    >> >
    >> > What would your code give in the case of:
    >> >
    >> > int arr[] = { 5 };
    >> >
    >> > min -> 5
    >> > max -> INT_MAX

    >>
    >> No, it won't:

    > Run the program and try it!
    > In the above max will never be set, since there
    > is an else in it!


    Actually there is too much else in code :) Removing 'else' statement
    from code above will remove bug.

    --
    Best regards from
    Kamil Burzynski
     
    Kamil Burzynski, Jan 20, 2004
    #8
  9. Karl Heinz Buchegger wrote:
    >
    > Hendrik Belitz wrote:
    > >
    > > Karl Heinz Buchegger wrote:
    > >
    > > > Hendrik Belitz wrote:
    > > >>
    > > >> john wrote:
    > > >>
    > > >> > Hi
    > > >> >
    > > >> > I'm a learner and I'm trying to work out the simplest way of finding
    > > >> > the maximum and minimum value from a group of integers.
    > > >>
    > > >> That's simple:
    > > >>
    > > >> int arr[arraySize};
    > > >> int min = INT_MAX; int max = INT_MIN;
    > > >> for ( i = 0; i < arraySize; i++ )
    > > >> if ( arr < min ) min = arr;
    > > >> else if ( arr > max ) max = arr;
    > > >>
    > > >
    > > > What would your code give in the case of:
    > > >
    > > > int arr[] = { 5 };
    > > >
    > > > min -> 5
    > > > max -> INT_MAX

    > >
    > > No, it won't:
    > >
    > > int arr[] = { 5 };
    > >
    > > min -> 5
    > > max -> 5
    > >


    Wrong. But I am wrong too. It gives

    min -> 5
    max -> INT_MIN

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jan 20, 2004
    #9
  10. john

    Chris Theis Guest

    "Hendrik Belitz" <> wrote in message
    news:buive2$92lb$-juelich.de...
    > Karl Heinz Buchegger wrote:
    >
    > > Hendrik Belitz wrote:


    > >> That's simple:


    Despite its simplicity it turns out to be tricky :)

    > >>
    > >> int arr[arraySize};


    Here we've got a typo!

    > >> int min = INT_MAX; int max = INT_MIN;
    > >> for ( i = 0; i < arraySize; i++ )
    > >> if ( arr < min ) min = arr;
    > >> else if ( arr > max ) max = arr;
    > >>


    You are lacking the definition of i!

    > >
    > > What would your code give in the case of:
    > >
    > > int arr[] = { 5 };
    > >
    > > min -> 5
    > > max -> INT_MAX

    >
    > No, it won't:
    >
    > int arr[] = { 5 };
    >
    > min -> 5
    > max -> 5
    >
    > which is correct
    >


    Yes, this is correct but it is not what your program delivers. If it does
    then I'd suggest to get rid of the compiler you are using. The result is

    min -> 5
    max -> INT_MIN

    due to the superficial else. Although removing the else only will not be
    sufficient, for in that case you will introduce another bug due to the lack
    of missing curly brackets in the for loop.

    The correct solution is:

    const int arraySize = 1;
    int arr[arraySize] = {5};
    int min = INT_MAX; int max = INT_MIN;
    for( int i = 0; i < arraySize; ++i ) {
    if ( arr < min ) min = arr;
    if ( arr > max ) max = arr;
    }

    Or simply write

    for ( int i = 0; i < arraySize; arr < min ? min = arr : i = i, arr
    > max ? max = arr[i++] : i++ ) {}


    Regards
    Chris
     
    Chris Theis, Jan 20, 2004
    #10
  11. john

    Chris Theis Guest

    "Kamil Burzynski" <> wrote in message
    news:...
    > On Tue, 20 Jan 2004 11:49:36 +0100, Karl Heinz Buchegger wrote:
    > > Hendrik Belitz wrote:
    > >> >> int arr[arraySize};
    > >> >> int min = INT_MAX; int max = INT_MIN;
    > >> >> for ( i = 0; i < arraySize; i++ )
    > >> >> if ( arr < min ) min = arr;
    > >> >> else if ( arr > max ) max = arr;
    > >> >
    > >> > What would your code give in the case of:
    > >> >
    > >> > int arr[] = { 5 };
    > >> >
    > >> > min -> 5
    > >> > max -> INT_MAX
    > >>
    > >> No, it won't:

    > > Run the program and try it!
    > > In the above max will never be set, since there
    > > is an else in it!

    >
    > Actually there is too much else in code :) Removing 'else' statement
    > from code above will remove bug.
    >


    Unfortunately this will only replace one bug by another due to missing curly
    brackets. See my other post.

    Regards
    Chris
     
    Chris Theis, Jan 20, 2004
    #11
  12. john

    Jeff Schwab Guest

    David Fisher wrote:
    > "David Fisher" <> wrote:
    >
    >>>I'm a learner and I'm trying to work out the simplest way of finding
    >>>the maximum and minimum value from a group of integers.

    >>
    >>Assuming they are already in a non-empty vector (std::vector<int>) called
    >>"vec":
    >>
    >>#include <algorithm>
    >>
    >>...
    >>
    >>int minValue = *(std::min_element(vec.begin(), vec.end()));
    >>int maxValue = *(std::min_element(vec.begin(), vec.end()));

    >
    >
    > Doh ! Spot the deliberate mistake ... :)
    >
    > David F


    You are forgiven, since your STL-based solution is very neat. :)
     
    Jeff Schwab, Jan 20, 2004
    #12
  13. john

    Chris Theis Guest

    "David Fisher" <> wrote in message
    news:hW6Pb.7512$...
    > "john" <> asked:
    >
    > > I'm a learner and I'm trying to work out the simplest way of finding
    > > the maximum and minimum value from a group of integers.

    >
    > Assuming they are already in a non-empty vector (std::vector<int>) called
    > "vec":
    >
    > #include <algorithm>
    >
    > ...
    >
    > int minValue = *(std::min_element(vec.begin(), vec.end()));
    > int maxValue = *(std::min_element(vec.begin(), vec.end()));


    I guess you mean *(std::max_element(vec.begin(), vec.end())); here

    >
    > This takes about twice as long as finding them both simultaneously (as in
    > Hendrik's solution), but it is simple ...
    >


    Why not sort the vector and thus you have the min & the max value at once?

    Regards
    Chris
     
    Chris Theis, Jan 20, 2004
    #13
  14. On Tue, 20 Jan 2004 14:18:39 +0100, Chris Theis wrote:
    >
    > "Kamil Burzynski" <> wrote in message
    > news:...
    >> On Tue, 20 Jan 2004 11:49:36 +0100, Karl Heinz Buchegger wrote:
    >> > Hendrik Belitz wrote:
    >> >> >> int arr[arraySize};
    >> >> >> for ( i = 0; i < arraySize; i++ )
    >> >> >> if ( arr < min ) min = arr;
    >> >> >> else if ( arr > max ) max = arr;

    >> Actually there is too much else in code :) Removing 'else' statement
    >> from code above will remove bug.

    > Unfortunately this will only replace one bug by another due to missing curly
    > brackets. See my other post.


    Are you referring to a typo in declaration of array?

    Apart from that I dont see the way that bugs may be _replaced_ here.. of course
    additional (superfluous) curly brackets would be welcome after if, but
    in this code there is no bugs related to it (i.e. after else removed).
    At least I don't see them ;)

    --
    Best regards from
    Kamil Burzynski
     
    Kamil Burzynski, Jan 20, 2004
    #14
  15. john

    Chris Theis Guest

    "Kamil Burzynski" <> wrote in message
    news:...
    > On Tue, 20 Jan 2004 14:18:39 +0100, Chris Theis wrote:
    > >
    > > "Kamil Burzynski" <> wrote in message
    > > news:...
    > >> On Tue, 20 Jan 2004 11:49:36 +0100, Karl Heinz Buchegger wrote:
    > >> > Hendrik Belitz wrote:
    > >> >> >> int arr[arraySize};
    > >> >> >> for ( i = 0; i < arraySize; i++ )
    > >> >> >> if ( arr < min ) min = arr;
    > >> >> >> else if ( arr > max ) max = arr;
    > >> Actually there is too much else in code :) Removing 'else' statement
    > >> from code above will remove bug.

    > > Unfortunately this will only replace one bug by another due to missing

    curly
    > > brackets. See my other post.

    >
    > Are you referring to a typo in declaration of array?
    >
    > Apart from that I dont see the way that bugs may be _replaced_ here.. of

    course
    > additional (superfluous) curly brackets would be welcome after if, but
    > in this code there is no bugs related to it (i.e. after else removed).
    > At least I don't see them ;)
    >


    Changing the code from

    for ( i = 0; i < arraySize; i++ )
    if ( arr < min ) min = arr;
    else if ( arr > max ) max = arr;

    to

    for ( i = 0; i < arraySize; i++ )
    if ( arr < min ) min = arr;
    if ( arr > max ) max = arr;

    is equivalent to the following (with better formatting to illustrate what
    I'm aiming at)

    for ( i = 0; i < arraySize; i++ )
    if ( arr < min ) min = arr;

    if ( arr > max ) max = arr;

    The second if statement is not included in the loop as their is no
    indication of an execution block. Hence, it will be executed only once after
    the loop has finished and this is certainly not what we want :)

    Best regards
    Chris
     
    Chris Theis, Jan 20, 2004
    #15
  16. On Tue, 20 Jan 2004 15:04:11 +0100, Chris Theis wrote:
    >
    > "David Fisher" <> wrote in message
    > news:hW6Pb.7512$...
    >> This takes about twice as long as finding them both simultaneously (as in
    >> Hendrik's solution), but it is simple ...

    > Why not sort the vector and thus you have the min & the max value at once?


    This may be solution sometimes, but generally it is bad idea:
    - if you put such code to function like find_min_max() - nobody will
    expect that this function will modify this vector, and it may get
    const argument
    - if you will copy instead of in-place sort it can eat much memory
    - sorting is much slower than just reading all values once (and 2
    comparisons)
    - etc.

    IMHO worst of all is that you will modify original vector, which may be
    not acceptable.

    --
    Best regards from
    Kamil Burzynski
     
    Kamil Burzynski, Jan 20, 2004
    #16
  17. john

    Jeff Schwab Guest

    Chris Theis wrote:
    > "David Fisher" <> wrote in message
    > news:hW6Pb.7512$...
    >
    >>"john" <> asked:
    >>
    >>
    >>>I'm a learner and I'm trying to work out the simplest way of finding
    >>>the maximum and minimum value from a group of integers.

    >>
    >>Assuming they are already in a non-empty vector (std::vector<int>) called
    >>"vec":
    >>
    >>#include <algorithm>
    >>
    >>...
    >>
    >>int minValue = *(std::min_element(vec.begin(), vec.end()));
    >>int maxValue = *(std::min_element(vec.begin(), vec.end()));

    >
    >
    > I guess you mean *(std::max_element(vec.begin(), vec.end())); here
    >
    >
    >>This takes about twice as long as finding them both simultaneously (as in
    >>Hendrik's solution), but it is simple ...
    >>

    >
    >
    > Why not sort the vector and thus you have the min & the max value at once?
    >
    > Regards
    > Chris


    "At once?" Why would one replace an O(N) operation with an O(N log N)
    operation requiring more storage?
     
    Jeff Schwab, Jan 20, 2004
    #17
  18. Kamil Burzynski wrote:
    >
    > > Unfortunately this will only replace one bug by another due to missing curly
    > > brackets. See my other post.

    >
    > Are you referring to a typo in declaration of array?


    No.
    Original code (reformatted to emphasize the problem)

    for ( i = 0; i < arraySize; i++ )
    if ( arr < min )
    min = arr;
    else
    if ( arr > max )
    max = arr;

    One can clearly see now, that the second if is only executed
    in case arr is not a new minumum. That means: if the
    array only holds a minimum (as was the case with the test
    case I provided), max will never be set.
    (I know that you have catched that problem. Nevertheless
    I wanted to demonstrate how simple errors can crop into
    a source code if one doesn't pay attention to the simplest
    of all rules: write neat code and don't squeeze everything
    in one or two single lines! Formatting and indentation
    are your friends! )

    Now drop the else, as you suggested (again reformatted)

    for ( i = 0; i < arraySize; i++ )
    if ( arr < min )
    min = arr;

    if ( arr > max )
    max = arr;


    See it now?

    >
    > Apart from that I dont see the way that bugs may be _replaced_ here.. of course
    > additional (superfluous) curly brackets would be welcome after if, but
    > in this code there is no bugs related to it (i.e. after else removed).


    There are. You want the second if to be under the control of the for
    loop. Without the curly brackets, it won't.

    --
    Karl Heinz Buchegger
     
    Karl Heinz Buchegger, Jan 20, 2004
    #18
  19. On Tue, 20 Jan 2004 16:09:10 +0100, Karl Heinz Buchegger wrote:
    > Kamil Burzynski wrote:
    > Now drop the else, as you suggested (again reformatted)
    >
    > for ( i = 0; i < arraySize; i++ )
    > if ( arr < min )
    > min = arr;
    >
    > if ( arr > max )
    > max = arr;
    >
    >
    > See it now?


    > There are. You want the second if to be under the control of the for
    > loop. Without the curly brackets, it won't.


    Ah, yes, I didnt saw it. Thats the reason I always put brackets, too :)
    Long time ago I didnt.. but after wasting 2 days hunting such stupid bug
    I've learned it ;)


    --
    Best regards from
    Kamil Burzynski
     
    Kamil Burzynski, Jan 20, 2004
    #19
  20. john

    Chris Theis Guest

    "Kamil Burzynski" <> wrote in message
    news:...
    [SNIP]
    > Ah, yes, I didnt saw it. Thats the reason I always put brackets, too :)
    > Long time ago I didnt.. but after wasting 2 days hunting such stupid bug
    > I've learned it ;)


    :) Welcome to the club. I guess that's a trap everybody has to fall into.

    Cheers
    Chris
     
    Chris Theis, Jan 20, 2004
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Lois
    Replies:
    1
    Views:
    3,403
    Ryan Stewart
    Dec 27, 2004
  2. Camp Fire
    Replies:
    0
    Views:
    338
    Camp Fire
    Nov 2, 2004
  3. Eugeny Myunster
    Replies:
    16
    Views:
    8,965
    user923005
    Apr 28, 2008
  4. jimgardener

    finding min and max of a double[]

    jimgardener, Jul 18, 2008, in forum: Java
    Replies:
    6
    Views:
    460
    Roedy Green
    Jul 19, 2008
  5. carmen

    Converting hrs and min to just min

    carmen, Aug 10, 2004, in forum: ASP General
    Replies:
    4
    Views:
    151
    carmen
    Aug 10, 2004
Loading...

Share This Page