-
October 8th, 2011, 01:35 PM
#1
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
#2
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
#3
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
#4
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
-
October 10th, 2011, 10:36 AM
#5
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?
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|