Jump to content

Comparing Numbers?


 Share

Recommended Posts

I was sitting here wondering, how do you compare multiple numbers with each others to see which is the biggest?

The idea is fairly simple, like: If $a > $b Then. But how does it look when you have multiple numbers?. For example...

$a = 10
$b = 4
$c = 65
$d = 43
$e = 19

Years ago we went thru this in school, but cant remember how it was done! I just remember the code was "fairly" simple when done right :think:. I just cant do it right, thats the problem :)

First i would guess $a would be compared to existing numbers, $b->$e to see if its bigger then those... but just cant figure it out. Was hoping someone knew how it was done :(

"I'm paper, rock is fine, nerf scissors!!!"

Link to comment
Share on other sites

  • Moderators

I made a udf to do this:

http://www.autoitscript.com/forum/index.ph...ndpost&p=151083

Here is an example:

$a = 10
$b = 4
$c = 65
$d = 43
$e = 19
$avArray = StringSplit($a & ',' & $b & ',' & $c & ',' & $d & ',' & $e, ',')

ArraySortNum($avArray)
MsgBox(0, 'Example', 'Largest Number There Was: ' & $avArray[1] & @CR & 'Smallest Number There Was: ' & $avArray[5])

Func ArraySortNum(ByRef $nArray, $Ascending = 0, $Start = 1)
    For $i = $Start To UBound($nArray) - 2
        Local $SE = $i
        If $Ascending = 0 Then
            For $x = $i To UBound($nArray) - 1
                If Number($nArray[$SE]) < Number($nArray[$x]) Then $SE = $x
            Next
        Else
            For $x = $i To UBound($nArray) - 1
                If Number($nArray[$SE]) > Number($nArray[$x]) Then $SE = $x
            Next
        EndIf
        Local $HLD = $nArray[$i]
        $nArray[$i] = $nArray[$SE]
        $nArray[$SE] = $HLD
    Next
EndFunc
Edited by SmOke_N

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Link to comment
Share on other sites

i just wrote something quickly (you beat me to it smoke)

Also remember that the For..Next statement($j) only goes up to 100 and won't work with larger numbers

MsgBox(0, @error, _Biggest("10,4,65,43,19"))

Func _Biggest($s_num)
    Dim $biggest[2]
    $biggest[0] = 0
    $i_num = StringSplit($s_num, ",")
    For $i = 1 To $i_num[0]
        For $j = 1 To 100
            If $i_num[$i] > $j And $j > $biggest[0] Then
                $biggest[0] = $j
                $biggest[1] = $i_num[$i]
            EndIf
        Next
    Next
    Return $biggest[1]
EndFunc
Edited by RazerM
My Programs:AInstall - Create a standalone installer for your programUnit Converter - Converts Length, Area, Volume, Weight, Temperature and Pressure to different unitsBinary Clock - Hours, minutes and seconds have 10 columns each to display timeAutoIt Editor - Code Editor with Syntax Highlighting.Laserix Editor & Player - Create, Edit and Play Laserix LevelsLyric Syncer - Create and use Synchronised Lyrics.Connect 4 - 2 Player Connect 4 Game (Local or Online!, Formatted Chat!!)MD5, SHA-1, SHA-256, Tiger and Whirlpool Hash Finder - Dictionary and Brute Force FindCool Text Client - Create Rendered ImageMy UDF's:GUI Enhance - Enhance your GUIs visually.IDEA File Encryption - Encrypt and decrypt files easily! File Rename - Rename files easilyRC4 Text Encryption - Encrypt text using the RC4 AlgorithmPrime Number - Check if a number is primeString Remove - remove lots of strings at onceProgress Bar - made easySound UDF - Play, Pause, Resume, Seek and Stop.
Link to comment
Share on other sites

The code smoke had was wonderful, just what i was looking for, thanks!

I ran into a new problem however :think:

Im using Random to generate the numbers atm and i noticed that sometimes same number poped up. Is there a way to stop same numbers from appearing?. I kinda thought it started to get quite complicated atm, sorry about it.

I made this but cant get it to work properly atm

While $loop = 0

    $a = Random(1,35,1)
    $b = Random(1,35,1)
    $c = Random(1,35,1)
    $d = Random(1,35,1)
    $e = Random(1,35,1)
    
    If $a = $b or $c or $d or $e Then 
        ToolTip("Error: " & $a & " : " & $b & " : " & $c & " : " & $d & " : " & $e, 0,0)
        Sleep(200)
        $loop = 0
    EndIf
    
    If $b = $a or $c or $d or $e Then
        ToolTip("Error: " & $a & " : " & $b & " : " & $c & " : " & $d & " : " & $e, 0,0)
        Sleep(200)
        $loop = 0
    EndIf

    If $c = $a or $b or $d or $e Then
        ToolTip("Error: " & $a & " : " & $b & " : " & $c & " : " & $d & " : " & $e, 0,0)
        Sleep(200)
        $loop = 0
    EndIf
    
    If $d = $a or $b or $c or $e Then
        ToolTip("Error: " & $a & " : " & $b & " : " & $c & " : " & $d & " : " & $e, 0,0)
        Sleep(200)
        $loop = 0
    EndIf
    If $e = $a or $b or $c or $d Then
        ToolTip("Error: " & $a & " : " & $b & " : " & $c & " : " & $d & " : " & $e, 0,0)
        Sleep(200)
        $loop = 0
    ElseIf
        $loop = 1
    EndIf
    
WEnd

I added the tooltip and sleep, just to see if the numbers really appeared more then once. The problem is most of the time they dont, and the script wont continue thru the loop.

Edited by huldu

"I'm paper, rock is fine, nerf scissors!!!"

Link to comment
Share on other sites

This is wrong

If $a = $b or $c or $d or $e Then

You must say

If $a = $b Or $$a = $c or $a = $d or $a = $e

$a = Random(1, 35, 1)

Do
  $b = Random(1, 35, 1)
Until $b <> $a

Do
  $c = Random(1, 35, 1) 
Until $c <> $a And $c <> $b 

Do
  $d = Random(1, 35, 1)
Until $d <> $c And $d <> $b And $d <> $a

MsgBox(0,"Numbers", $a & "," & $b & ", " & $c & ", " & $d)

It would be a lot easier to use arrays instead of separate variables....

; Initialize array of integers 1 to 35
Dim $numberSet[35]
For $i = 0 To 34
    $numberSet[$i] = $i + 1
Next

; Read elements randomly and put into new array
; Each time we decrease the index range...
Dim $resultSet[35]
For $i = 34 to 0 step -1
    $randIndex = Random(0, $i, 1)
    $resultSet[$i] = $numberSet[$randIndex]
    
; replace used number with highest unsed one
    $numberSet[$randIndex] = $numberSet[$i]
Next

; $resultSet and Output will contain all numbers 1 to 35 in random order (each occurs once)
$output = ""
For $i = 0 to 34
    $output = $output & $resultSet[$i] & ", "
Next
$output = StringTrimRight($output, 2)
MsgBox(4096,"", $output)
Use Mozilla | Take a look at My Disorganized AutoIt stuff | Very very old: AutoBuilder 11 Jan 2005 prototype I need to update my sig!
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...