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.
Printable View
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.
How do you think you would solve this problem? What have you tried?
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.
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