Which array number is biggest.
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

# Thread: Which array number is biggest.

1. Junior Member
Join Date
Oct 2011
Posts
13

## Which array number is biggest.

Hi, how can i know which array number is biggest?

I need to know the number ant which.

Example: a[5] = {1, 2, 5, 4, 3}

Bigest number is third and this is number 5.

2. Member
Join Date
Aug 2009
Posts
440

## Re: Which array number is biggest.

How do you think you would solve this problem? What have you tried?

3. Junior Member
Join Date
Oct 2011
Posts
13

## Re: Which array number is biggest.

I have a mind with a lot of 'if'. If number 1 is biggest than other, then first and so on. but i think you guys have a greatest idea.

4. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: Which array number is biggest.

Originally Posted by justutiz
but i think you guys have a greatest idea.
The "greatest idea" is to use max_element. There is no need for if() statements.

http://www.cplusplus.com/reference/a...m/max_element/

To get the position, use std:istance to get the "distance" between the return value of max_element, and &a[0].

The max_element will return a pointer to the largest element, so all you need to do is get the distance between that pointer and the beginning of the a[] array.

http://www.cplusplus.com/reference/s...ator/distance/

In other words, two lines of C++ which I won't write (it's all in the links describing these two functions).

Regards,

Paul McKenzie

5. Elite Member Power Poster
Join Date
Aug 2000
Location
New York, NY, USA
Posts
5,656

## Re: Which array number is biggest.

Originally Posted by Paul McKenzie
The "greatest idea" is to use max_element. There is no need for if() statements.
Something tells me that this is a pre-STL exercise.
“for” loop, may be?

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•