# [Solved] Comparing numbers from array

## 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!

##### 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

##### 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!

## Create an account

Register a new account