Thread: Best way to find maximum number

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    5

    Best way to find maximum number

    Okay, so this is not actually some problem but i realy need help here.
    There are given some numbers (ex. 53, 67, 172, 18, 942, 342) and i need to find the largest one.
    What is the best and easiest way to do this ?

    Thank you.

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by mutombo View Post
    Okay, so this is not actually some problem but i realy need help here.
    There are given some numbers (ex. 53, 67, 172, 18, 942, 342) and i need to find the largest one.
    What is the best and easiest way to do this ?

    Thank you.
    Think about it this way. Before you traverse your array (or whatever data structure you're using) you have a maximum of 0. Then as you traverse your array, you compare the number with your current maximum. If the number is larger, make that the new maximum. When you've finished going through the array, you've got the largest number.
    This doesn't work so well for negative numbers though.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    5
    Quote Originally Posted by QuantumPete View Post
    Think about it this way. Before you traverse your array (or whatever data structure you're using) you have a maximum of 0. Then as you traverse your array, you compare the number with your current maximum. If the number is larger, make that the new maximum. When you've finished going through the array, you've got the largest number.
    This doesn't work so well for negative numbers though.

    QuantumPete
    That's easy to do, but i have another problem in here now.
    The numbers are stored in 2 arrays, so when i find the maximum i also need to find out where that number is placed. Example:
    x[1] = 10
    x[2] = 152
    y[1] = 199
    y[2] = 987

    In this case it is easy to find that 987 is the maximum, but i also need to know that the maximum number is placed in y[2].

    Any ideas ?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, as before, you start with a max that is really small (e.g. 0). Then you walk along the array(s) and whenever you find a new "biggest so far", you set a second and third variable to indicate which position and which array it is.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Arrays, how to find if some number is the element of an array.
    By InvariantLoop in forum C++ Programming
    Replies: 14
    Last Post: 03-18-2006, 02:43 AM
  2. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 11:21 PM
  3. Print Maximum number
    By rmathus in forum C Programming
    Replies: 3
    Last Post: 10-07-2003, 06:33 AM
  4. Replies: 2
    Last Post: 08-03-2003, 10:01 AM
  5. Can't find the lowest number in array?
    By Nate2430 in forum C++ Programming
    Replies: 1
    Last Post: 11-20-2001, 10:21 AM