# Which array number is biggest.

• October 8th, 2011, 01:35 PM
justutiz
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.
• October 8th, 2011, 01:49 PM
Alterah
Re: Which array number is biggest.
How do you think you would solve this problem? What have you tried?
• October 9th, 2011, 04:07 AM
justutiz
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.
• October 9th, 2011, 06:16 AM
Paul McKenzie
Re: Which array number is biggest.
Quote:

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::distance 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
• October 10th, 2011, 10:36 AM