# finding max and min integers - newbie question

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

1. ### johnGuest

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

2. ### Hendrik BelitzGuest

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

3. ### David FisherGuest

"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
4. ### David FisherGuest

"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
5. ### Karl Heinz BucheggerGuest

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
6. ### Hendrik BelitzGuest

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
7. ### Karl Heinz BucheggerGuest

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
8. ### Kamil BurzynskiGuest

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
9. ### Karl Heinz BucheggerGuest

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
10. ### Chris TheisGuest

"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
11. ### Chris TheisGuest

"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
12. ### Jeff SchwabGuest

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
13. ### Chris TheisGuest

"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
14. ### Kamil BurzynskiGuest

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
15. ### Chris TheisGuest

"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
16. ### Kamil BurzynskiGuest

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
17. ### Jeff SchwabGuest

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
18. ### Karl Heinz BucheggerGuest

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
19. ### Kamil BurzynskiGuest

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
20. ### Chris TheisGuest

"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

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