Jump to content

Algorithm needed


OmYcroN
 Share

Recommended Posts

Hi. I need an algorithm for this situation:

I start with $arr[5] = [4, 10, 5, 11, 6]

Then i add an element 5 so the new array is $arr[6] = [4, 10, 5, 11, 6, 5]

The problem is the new array must have unique numbers so the result should be $arr[6] = [4, 10, 6, 11, 7, 5] (5 updated to 6, 6 to 7)

I've tried with the recursive method but Autoit shows me "Recursion level has been exceeded - AutoIt will quit to prevent stack overflow". Any ideas ?

Link to comment
Share on other sites

What about this?

#include <Array.au3>
 
Global $arr[5] = [4, 10, 5, 11, 6]
_ArrayAdd($arr, 5)
_ArraySort($arr)
For $i = 0 To UBound($arr) - 2
If $arr[$i] = $arr[$i + 1] Then
$arr[$i + 1] += 1
EndIf
Next
_ArrayDisplay($arr)

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

Same for 2D arrays:

#include <Array.au3>
 
Global $arr[6][3] = [ [4, Random(0, 20, 1), Random(0, 20, 1)], _
[10, Random(0, 20, 1), Random(0, 20, 1)], _
[5, Random(0, 20, 1), Random(0, 20, 1)], _
[11, Random(0, 20, 1), Random(0, 20, 1)], _
[6, Random(0, 20, 1), Random(0, 20, 1)], _
[5, Random(0, 20, 1), Random(0, 20, 1)]]
_ArraySort($arr, 0, 0, 0, 0)
_ArrayDisplay($arr)
For $i = 0 To UBound($arr) - 2
If $arr[$i][0] = $arr[$i + 1][0] Then
$arr[$i + 1][0] += 1
EndIf
Next
_ArrayDisplay($arr)

Br,

UEZ

Please don't send me any personal message and ask for support! I will not reply!

Selection of finest graphical examples at Codepen.io

The own fart smells best!
Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!
¯\_(ツ)_/¯  ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ

Link to comment
Share on other sites

You can add rows or columns using ReDim. However it is advisable to avoid using ReDim and calculate the number of rows and columns you need to declare the array in the first place. Another function you might find useful is _ArrayConcatenate. Here is an example of adding a column to a one dimensional array (turning it into a two dimensional array) without using ReDim.

#include <Array.au3>
 
_Run()
 
Func _Run()
    Local $aMyArray[13] = [12,"one","two","three","four","five", _
    "six","seven","eight","nine","ten","eleven","twelve"]
    _ArrayDisplay($aMyArray)
    $aMyArray = _InsertStringLenCol($aMyArray)
    _ArrayDisplay($aMyArray)
EndFunc ;==> _Run
 
Func _InsertStringLenCol($aArray) ; Element 0 must contain the number of records => No Error Checks
    Local $aTemp[$aArray[0] +1][2]
    $aTemp[0][0] = 0 ; Length of the longest string
    $aTemp[0][1] = $aArray[0] ; The number of records
    For $i = 1 To UBound($aArray) -1
        $aTemp[$i][0] = StringLen($aArray[$i])
        $aTemp[$i][1] = $aArray[$i]
        If $aTemp[$i][0] > $aTemp[0][1] Then $aTemp[0][1] = $aTemp[$i][0]
    Next
    Return $aTemp
EndFunc ;==> _InsertStringLenCol

The added column gives the string length of each element in the original array. It's just an example. :graduated:

Edited by czardas
Link to comment
Share on other sites

Recursion example.

#include <array.au3>
main()
Func main()
    Local $a1[5] = [4, 10, 5, 11, 6]
    test($a1, 5)
    ReDim $a1[UBound($a1, 1) + 1]
    $a1[UBound($a1, 1) - 1] = 5
    _ArrayDisplay($a1)
EndFunc
Func test(ByRef $aData, $iAdd)
    For $i = 0 To UBound($aData, 1) - 1
        If $aData[$i] = $iAdd Then
            $iAdd += 1
            test($aData, $iAdd)
            $aData[$i] = $iAdd
        EndIf
    Next
EndFunc

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

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