finding the two closest numbers in an array

This is a discussion on finding the two closest numbers in an array within the C Programming forums, part of the General Programming Boards category; i have a sorted array of integers and i'm trying to find the two closest numbers. anyone have an idea ...

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

    finding the two closest numbers in an array

    i have a sorted array of integers and i'm trying to find the two closest numbers. anyone have an idea of how to do this?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by ominub View Post
    anyone have an idea of how to do this?
    Yep. It involves comparing numbers.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    yes, i realize that...

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Sort it. Walk it. Subtract them as you go.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    would anyone mind explaining how to do it?

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    66
    Quote Originally Posted by quzah View Post
    Sort it. Walk it. Subtract them as you go.

    i've gotten this far, but i cant figure out how to store the smallest number while i compare it to the rest of the results.
    the whole point is printing the rwo closest numbers so it will also have to remember those two numbers.
    i'm having a hard time wrapping my mind around it.
    Last edited by ominub; 10-20-2009 at 07:42 PM. Reason: clarification

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Well, you said how. Store the smallest number (in a variable called, ooh, "smallest" maybe). (EDIT based on your edit: So you need to store two numbers (and therefore in two variables) in addition to the "smallest" above.)
    Last edited by tabstop; 10-20-2009 at 07:45 PM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There are no special C functions for doing this, so code it up like you would do it by hand.

    Say your values were: 1,3,5,6, 9. How would you find the two numbers that are closest?

    1 - 3 = diff = mingap = 2

    3 - 1 is the same as 1 - 3 for gap, so we only need to subtract with the adjacent, larger, number.

    3 - 5 = diff not lower than mingap, either.

    5 - 6 = diff = new mingap of 1

    As a practical matter, I'd prefer to work down the array values, to avoid any negative numbers or absolute value stuff.

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    Imagine you were told that you would be shown a series of cards with numbers on them. Each number is to be greater than the next. You will be shown 50 numbers, each one for as long as you require, but only one at a time. Your task is to identify the two closest together numbers. You may not write anything down, but you're free to use a basic calculator if you wish.

    Would you be able to identify the closest two numbers after all cards have been shown to you?
    This is exactly the kind of simple problem that anyone hoping to be a programmer should definitely be able to solve. If you can't come up with a logical solution then you're just not applying your mind to the problem. Think about it for a while longer until you eventually solve it on your own.

    If you then know how to solve the problem but can't code up a solution because you're new to C then that's fine, just give it a shot anyway and post what you get.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  10. #10
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    As everybody said above this is the pusdocode for it

    As array is sorted then

    Code:
    For i 0 to N - 1
    TEMP_DIFF = ARR[i+1] - ARR[i]
    
    if (TEMP_DIFF < DIFF)
    START_ELEMENT = i
    END_ELEMENT     = i  +  1
    endif
    
    DIFF = TEMP_DIFF

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code review
    By bennywhere in forum C Programming
    Replies: 16
    Last Post: 10-20-2009, 09:00 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Writing unique numbers to an array
    By yardy in forum C Programming
    Replies: 6
    Last Post: 12-27-2006, 08:15 PM
  4. question: reading numbers into an array
    By Lince in forum C Programming
    Replies: 15
    Last Post: 11-15-2006, 02:41 AM
  5. finding size of empty char array
    By darsunt in forum C Programming
    Replies: 12
    Last Post: 05-30-2006, 07:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21