Jump to content

[Solved] Comparing numbers from array


Hest
 Share

Recommended Posts

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!
Link to comment
Share on other sites

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
Link to comment
Share on other sites

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!
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...