Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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
 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
 
 
 
David Fisher
Guest
Posts: n/a
 
      01-20-2004
"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


 
Reply With Quote
 
David Fisher
Guest
Posts: n/a
 
      01-20-2004
"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


 
Reply With Quote
 
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

 
Reply With Quote
 
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
 
Reply With Quote
 
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

 
Reply With Quote
 
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
 
Reply With Quote
 
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

 
Reply With Quote
 
Chris Theis
Guest
Posts: n/a
 
      01-20-2004

"Hendrik Belitz" <> wrote in message
news:buive2$92lb$...
> 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
finding min and max of a double[] jimgardener Java 6 07-19-2008 09:42 AM
What the fastest algorithm for find max/min in an array of integers? Eugeny Myunster C Programming 16 04-28-2008 07:24 PM
CSS min-width, max-width, and min-height with display:inline Lois HTML 1 12-27-2004 03:03 AM
Finding min/max of numarray RecordArray Camp Fire Python 0 11-02-2004 05:37 AM
Converting hrs and min to just min carmen ASP General 4 08-10-2004 08:31 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57