Velocity Reviews > C++ > finding max and min integers - newbie question

# finding max and min integers - newbie question

john
Guest
Posts: n/a

 01-20-2004
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

Hendrik Belitz
Guest
Posts: n/a

 01-20-2004
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[i] < min ) min = arr[i];
else if ( arr[i] > max ) max = arr[i];

--
To get my real email adress, remove the two onkas
--
Dipl.-Inform. Hendrik Belitz
Central Institute of Electronics
Research Center Juelich

David Fisher
Guest
Posts: n/a

 01-20-2004

> 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
Guest
Posts: n/a

 01-20-2004
"David Fisher" <(E-Mail Removed)> 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

Karl Heinz Buchegger
Guest
Posts: n/a

 01-20-2004
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[i] < min ) min = arr[i];
> else if ( arr[i] > max ) max = arr[i];
>

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
http://www.velocityreviews.com/forums/(E-Mail Removed)

Hendrik Belitz
Guest
Posts: n/a

 01-20-2004
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[i] < min ) min = arr[i];
>> else if ( arr[i] > max ) max = arr[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

--
To get my real email adress, remove the two onkas
--
Dipl.-Inform. Hendrik Belitz
Central Institute of Electronics
Research Center Juelich

Karl Heinz Buchegger
Guest
Posts: n/a

 01-20-2004
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[i] < min ) min = arr[i];
> >> else if ( arr[i] > max ) max = arr[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

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

--
Karl Heinz Buchegger
(E-Mail Removed)

Kamil Burzynski
Guest
Posts: n/a

 01-20-2004
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[i] < min ) min = arr[i];
>> >> else if ( arr[i] > max ) max = arr[i];
>> >
>> > 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

Karl Heinz Buchegger
Guest
Posts: n/a

 01-20-2004
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[i] < min ) min = arr[i];
> > >> else if ( arr[i] > max ) max = arr[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
> >

Wrong. But I am wrong too. It gives

min -> 5
max -> INT_MIN

--
Karl Heinz Buchegger
(E-Mail Removed)

Chris Theis
Guest
Posts: n/a

 01-20-2004

"Hendrik Belitz" <(E-Mail Removed)> wrote in message
news:buive2\$92lb\$(E-Mail Removed)-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[i] < min ) min = arr[i];
> >> else if ( arr[i] > max ) max = arr[i];
> >>

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[i] < min ) min = arr[i];
if ( arr[i] > max ) max = arr[i];
}

Or simply write

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

Regards
Chris