Jump to content
Sign in to follow this  
Hest

[Solved] Comparing numbers from array

Recommended Posts

Hest

I have a little problem and I'm stuck now.

I have an array with a bunch of numbers 1, 10, 12, 20, 40, 48, 54... and so on. It can be any number.

Im my program, the user can calculate a number and now I need to find the number in the array that is bigger and closest to it.

So, if array = 1, 10, 12, 20, 40, 48, 54 and the user result is 14, I need the output to be 20.

Can anyone explain how I can do that?.

I thought about a while loop and just checking the user result and then +1 untill I get a hit, but I dont think thats the best way to do it.

Edited by Hest

Software:Model Train Calculator (Screen)Autoit3 beginner!

Share this post


Link to post
Share on other sites
Danny35d

Dim $aNumbers[7] = [1, 10, 12, 20, 40, 48, 54]

$iUserNumber = InputBox('Comparing Numbers', 'Enter a number between 1 an 54')

$iBiggerNumber = 0
For $iNumber In $aNumbers
    If $iBiggerNumber < $iUserNumber And $iNumber > $iUserNumber Then $iBiggerNumber = $iNumber
Next

MsgBox(0, 'Comparing Numbers', 'The bigger and closest number to ' & $iUserNumber & ' is ' & $iBiggerNumber & '.')


AutoIt Scripts:NetPrinter - Network Printer UtilityRobocopyGUI - GUI interface for M$ robocopy command line

Share this post


Link to post
Share on other sites
Hest

Thx alot Danny, it's working now.

Made a little change to it, because it jumped a number, if the users number was in the array. Was missing a =

If $iBiggerNumber < $iUserNumber And $iNumber >= $iUserNumber Then $iBiggerNumber = $iNumber

Edited by Hest

Software:Model Train Calculator (Screen)Autoit3 beginner!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×