×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
• Talk With Other Members
• Be Notified Of Responses
• Keyword Search
Favorite Forums
• Automated Signatures
• Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

#### Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

# Algorithm to compare number sets (Mind bender????)

## Algorithm to compare number sets (Mind bender????)

(OP)
Hi,
I have the following dilemma:

I want to compare sets of 2 or 3 numbers with each other to find the closest match. Lets say I want to compare 2 numbers for the following example. I have a multi-dimensional array X[5][2] containing the following arbitrary data:

X[0][0] = 1, X[0][1] = 2
X[1][0] = 2, X[1][1] = 4
X[2][0] = 5, X[2][1] = 8
X[3][0] = 7, X[3][1] = 9
X[4][0] = 3, X[4][1] = 1

Therefore my sets are:

(1, 2)
(2, 4)
(5, 8)
(7, 9)
(3, 1)

Now I want to find the index (pair) in the array that contains the closest match to the following arbitrary sets of numbers:

(4, 9)
(5, 6)
(9, 1)

My initial solution was to take the number pairs and turn them into numbers themselves.

For example the array sets would become:
(1, 2) -> 12
(2, 4) -> 24
(5, 8) -> 58
(7, 9) -> 79
(3, 1) -> 31

And my number sets to compare would become:

(4, 9) -> 49
(5, 6) -> 56
(9, 1) -> 91

Ok, now I would go through each number I created from the sets in the array and subtract my number from it. Keeping in mind to subtract the smaller number from the larger. The number created from the array set, that when subtracted from my number with the smallest difference, would be my closest match...right?

So:

49 best matches 58 -> 58 - 49 = 9 (smallest difference)
56 best matches 58 -> 58 - 56 = 2 (smallest difference)
91 best matches 79... wait a minute! The difference is being heavily weighted on the "9" in (9, 1). Neither of these two numbers should take precedence so (3, 1) is a better match, but my system does not pick this up! I would also like to be able to do this with sets of 3 or 4 numbers. Any mathematicians out there who can give me a better algorithm? Obviously creating numbers out of the set and subtracting them is not the way to go.

Any help is GREATLY appreciated!!!!! Thanks in advance.

Rob Marriott
rob@career-connections.net

### RE: Algorithm to compare number sets (Mind bender????)

(OP)
Hi,
By closest, I do mean the smallest difference. I guess the best analogy of what I am doing would be to think of these sets as plotted points on a grid. I plot five points. Now I want to take a single arbitrary point and find the closest of the five points. To make things slightly harder, I REALLY want to use sets of 3, not 2. ie. (x, y, z)
I am using an array of structures. The structure has one  variable for x, one for y, and one for z. Then I have a single-dimension array that holds the individual structures or "points". I want to write a function that receives as parameters the arbitrary x, y, and z points and returns the index of the single-dimension array containing the closest point.
Thanks,

Rob Marriott
rob@career-connections.net

### RE: Algorithm to compare number sets (Mind bender????)

(OP)
Thankyou all for looking at my thread but I think I found the solution already.

Rob Marriott
rob@career-connections.net

#### Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

#### Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Close Box

# Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

• Talk To Other Members
• Notification Of Responses To Questions
• Favorite Forums One Click Access
• Keyword Search Of All Posts, And More...

Register now while it's still free!