Jump to content

[Solved] Array Trouble


Recommended Posts

I hate solving for trinomials, so I thought I'd make a script to do it for me. I am having trouble getting the correct values assigned to the correct dimension in the correct array. I want the array in this format:

$Array[factor group #][1 for first # in group; 2 for second #] = 1st or 2nd number in group

As an example, for the squared term in 2x^2+2x+1=0, I want:

$Array[1][1] = 1

$Array[1][2] = 2

$Array[2][1] = 2

$Array[2][2] = 1

I hope I made this clear enough. If there is already an example of this on the forum and I missed it, then please tell me. Thanks in advance.

Edited by dantay9
Link to comment
Share on other sites

I hate solving for trinomials, so I thought I'd make a script to do it for me. I am having trouble getting the correct values assigned to the correct dimension in the correct array. I want the array in this format:

$Array[factor group #][1 for first # in group; 2 for second #] = 1st or 2nd number in group

As an example, for the squared term in 2x^2+2x+1=0, I want:

$Array[1][1] = 1

$Array[1][2] = 2

$Array[2][1] = 2

$Array[2][2] = 1

I hope I made this clear enough. If there is already an example of this on the forum and I missed it, then please tell me. Thanks in advance.

1) AutoIt arrays are 0-based, so your references should be [0][0] and [0][1], then [1][0] and [1][1], etc.

2) Trinomial means "three terms" not three factors, so what do you mean by "factor group"?

3) What do "first #" and "second #" mean? The first term is 2x^2, so which of the "groups" you gave represents that? The second term is 2x, and I guess the second example you gave matches (2x^1). The third term (1) is not represented at all in your example.

:P

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

1) Sorry. My mistake. I do need the arrays to be zero based. That is not a problem.

2) The factors of 4 are 1, 2, and 4. The factor groups I am referring to are 1 & 4 and 2 & 2. The group number is just an index number. The first dimension is just an index so I can sort the groups.

3) The first and second number are referring to the groups. For example, 1 is the first number and 4 is the second number (in the example above).

Here is an example based on the factors of 6. I want this in the array:

$Array[0][1] = 1

$Array[0][2] = 6

$Array[1][1] = 2

$Array[1][2] = 3

Link to comment
Share on other sites

Here is what I was looking for. Sorry about my post being hard to understand.

$AllSquaredTermFactors = SquareFactors($Trinomial); contains all groups of factors
ReDim $FactorGroups[UBound($AllSquaredTermFactors)][2]
$x = 0
For $element In $AllSquaredTermFactors
    $TempArray = StringSplit($element, ",", 2)
    $FactorGroups[$x][0] = $TempArray[0]
    $FactorGroups[$x][1] = $TempArray[1]
    $x += 1
Next
_ArrayDisplay($FactorGroups)

Func GetFactors($Number)
    Local $x
    Local $Factors = ""
    Local $SquareRoot = Ceiling(Sqrt(Abs($Number)))
    For $x = 1 To $SquareRoot
        If Mod($Number, $x) = 0 Then
            $Factors = $Factors & $x & "," & $Number / $x & "|"
        EndIf
    Next
    $Factors = StringTrimRight($Factors, 1)
    $AFactors = StringSplit($Factors, "|", 2)
    Return $AFactors
EndFunc  ;==>GetFactors
Func SquareFactors($Function)
    Local $Position, $SquaredTerm, $Factors
    $Position = StringInStr($Function, "x^2")
    If $Position <= 1 Then
        $SquaredTerm = "1"
    Else
        $SquaredTerm = StringLeft($Trinomial, $Position - 1)
    EndIf
    $Trinomial = StringTrimLeft($Function, $Position)
    $SquaredTerm = Number($SquaredTerm)
    $Factors = GetFactors($SquaredTerm)
    Return $Factors
EndFunc  ;==>SquareFactors
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...