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

Thread: Which array number is biggest.

  1. #1
    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. #2
    Join Date
    Aug 2009
    Posts
    439

    Re: Which array number is biggest.

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

  3. #3
    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. #4
    Join Date
    Apr 1999
    Posts
    27,422

    Re: Which array number is biggest.

    Quote Originally Posted by justutiz View Post
    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

  5. #5
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,525

    Re: Which array number is biggest.

    Quote Originally Posted by Paul McKenzie View Post
    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:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, 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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center