Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare Numbers

Status
Not open for further replies.

Praxden

Programmer
Jun 1, 2001
31
US
What i want to do is compare each number in the list to each other and count the numbers that are within say 5 of each other. The numbers are listed in different columns (35 total). Heres a better example.

232 I 230 I 120 I 12 I 23 I 27 I 21 ----- Result would be 4

because 23 27 and 21 are within 5 of each other and 232 and 230 are within 5 of each other.



Thanks

Praxden
 
...count the numbers that are within say 5 of each other...

...23 27 and 21 are within 5 of each other...

Not sure I understand your usage of "each other." 23 is within 5 of 21 and 27, but 21 and 27 are not within 5 of "each other" by my usage of the English language.

If I follow your example, then if the numbers are 1,6,11,16,21,26,...,171 (35 numbers) the result would be 34?

How about this list?
1,2,11,12,21,22,31,32,...,161,162,171 (35 numbers)
Would the result be 17?

It would be helpful if you could provide more comprehensive test data to illustrate what you are after.


 
While I agree with Zathras, your example does not exactly 'fit' the verbage, I am assuming that it is just a mental slip on your part. If so, the following coould be of use:

Code:
Public Function basCntClose(intClose As Integer, ParamArray ValAry() As Variant) As Integer

    Dim MyAry() As Variant
    Dim varTmp As Variant
    Dim Idx As Long
    Dim blnSrtd As Boolean
    Dim varClose As Long

    'Michael Red    2/16/04 _
     To count the set of values which are 'Close" to each other ?

    'Example usage
    '? basCntClose(5, 232, 230, 120, 12, 23, 27, 21)
    '3

    ReDim MyAry(UBound(ValAry))

    'local copy of the values
    While Idx <= UBound(ValAry)

        MyAry(Idx) = ValAry(Idx)

        Idx = Idx + 1
    Wend

    'Sort the set
    Idx = 0
    While blnSrtd = False
        blnSrtd = True

        Idx = 0
        While Idx < UBound(MyAry)

            If (MyAry(Idx) > MyAry(Idx + 1)) Then
                blnSrtd = False
                varTmp = MyAry(Idx)
                MyAry(Idx) = MyAry(Idx + 1)
                MyAry(Idx + 1) = varTmp
            End If

            Idx = Idx + 1
        Wend

    Wend

    Idx = 0
    While Idx < UBound(MyAry)

        If (MyAry(Idx + 1) - MyAry(Idx) <= intClose) Then
            varClose = varClose + 1
        End If

        Idx = Idx + 1
    Wend

    basCntClose = varClose

End Function




MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
Michael/Zathras,

Thanks for responding. Although 21 is not close to 27. I was thinking it would loop thru the circuit adding one for each therefore something like this:

a, b, c, d ,e,f
b, c, d ,e,f
c, d ,e,f
d ,e,f
so in the loop it would compare abs(21- 23)<5
inttot = inttot+1
then later it would compare abs(23-27)<5
inttot = inttot+1

Michael thank for supplying the code let me play with it.


Praxden
 
Praxden, all well and good. But normally we (programmers) should let the specs determine the code, and not force it the other way around.

So, I repeat...

It would be helpful if you could provide more comprehensive test data to illustrate what you are after.

Better still would be a description of the overall problem you are trying to solve.
 
Zathras,

Thanks for your interest. The specs and the code are in line with each other. What I am trying to do is track the croosing of planetary aspects with other planitary aspects based on the angle form the sun. If you would like I could sned you some data and a grapgh I created which might help you understand what I'm trying to accomplish. If so please contact me at my personal email jbrooks@triad.rr.com


Thanks

Praxden
 
Sounds interesting. But the only test data you have supplied so far is

232 230 120 12 23 27 21 ----- Result would be 4

because 23 27 and 21 are within 5 of each other and 232 and 230 are within 5 of each other.

I can't quite see how you come up with a result of 4. I could see 5 (23,27,21,232,230 each with at least one near neighbor) or 3 (21,27 both near 23, and 232 near 230).

Incidentally, 3 is the answer returned from the function provided by MichaelRed.

If I'm going to be able to provide further assistance, you'll have to provide a bit more guidance and/or test data to indicate exactly what calculation you want.
 
Zathras,

Give me a day to look over my data and make sure I am presenting it properly. Since this is research on my part, I might not be thinking about it correctly. I do appreciate your help.


Praxden,


 
Zathra,


Michaels code works correctly, I apologize for not properly expressing my need

Thanks


Praxden
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top