Jump to content
Sign in to follow this  
sharkos

Whats wrong in here? (arrays)

Recommended Posts

sharkos

Im trying to write Gaussian elimination method.

I have array (column 0 and row 0 are empty always):

11 | 12 | 13 | 14
21 | 22 | 23 | 24
31 | 32 | 33 | 34

In Stage 1 first array ($A) is modyfied by function 'calc' and saved in second array and it looks like these ( * - updated):

11  | 12  | 13  | 14
21* | 22* | 23* | 24*
31* | 32* | 33* | 34*

In Stage 2 second array ($A2) is modyfied and saved in to $A3, should look like these:

11 | 12  | 13  | 14
21 | 22  | 23  | 24
31 | 32* | 33* | 34*

But it dosent. Function 'calc' is called 3 times like it should be, but array is changed like these:

11    | 12    | 13    | 14
21(+1)* | 22(+1)* | 23(+1)* | 24(+1)*
31(+2)* | 32*    | 33*   | 34*

Heres my code:

#Include <Array.au3>

$gi=3
$gj=$gi+1
Dim $A[$gi+1][$gj+1]
Dim $A2[$gi+1][$gj+1]
Dim $A3[$gi+1][$gj+1]

$A[1][1] = 2
$A[1][2] = 2
$A[1][3] = 2
$A[1][4] = 2

$A[2][1] = 1
$A[2][2] = 2
$A[2][3] = 4
$A[2][4] = 0

$A[3][1] = 2
$A[3][2] = 2
$A[3][3] = 1
$A[3][4] = -2

$A2=$A
$A3=$A

; Stage 1 <=====================
For $ti=2 To $gi Step +1
    For $tj=1 To $gj Step +1
        $A2[$ti][$tj] = Call("calc", $A, $ti, $tj, 1)
    Next
Next
_ArrayDisplay($A2, "Window")

;Stage 2 <=====================
For $ti=3 To $gi Step +1
    For $tj=2 To $gj Step +1
        $A3[$ti][$tj] = Call("calc", $A2, $ti, $tj, 2)
    Next
Next
_ArrayDisplay($A3, "Window")

Func calc($T, $i, $j, $c)
    $temp = $T[$i][$j] - ($T[$i][$c]/$T[$c][$c]) * $T[$c][$j]
    MsgBox(0,"","$T["&$i&"]["&$j&"] - ($T["&$i&"]["&$c&"]/$T["&$c&"]["&$c&"]) * $T["&$c&"]["&$j&"] = "&$temp);FOR DEBUG ONLY
    Return $temp
EndFunc

Whats wrong in Step 2?

Share this post


Link to post
Share on other sites
Authenticity

11 | 12  | 13  | 14
21 | 22  | 23  | 24
31 | 32* | 33* | 34*

If this is the desired result then I see you need to take a good break, lol, this is exactly what the second stage does. Additionally, you can just call the function like this calc($A, $ti, $tj, 1) without using the call function. Step +1 is the default for the For...Next loop, no need to include it.

Share this post


Link to post
Share on other sites
sharkos

Input:

Posted Image

Step 1 result:

Posted Image

Step 2 result:

Posted Image

Step 2 desired result:

Posted Image

Whats going on?

Share this post


Link to post
Share on other sites
KaFu

#Include <Array.au3>

$gi=3
$gj=$gi+1
Dim $A[$gi+1][$gj+1]
Dim $A2[$gi+1][$gj+1]
Dim $A3[$gi+1][$gj+1]

$A[1][1] = 2
$A[1][2] = 2
$A[1][3] = 2
$A[1][4] = 2

$A[2][1] = 1
$A[2][2] = 2
$A[2][3] = 4
$A[2][4] = 0

$A[3][1] = 2
$A[3][2] = 2
$A[3][3] = 1
$A[3][4] = -2

$A2=$A

; Stage 1 <=====================
For $ti = 2 To $gi
    For $tj=1 To $gj
        $A2[$ti][$tj] = calc($A, $ti, $tj, 1)
    Next
Next
_ArrayDisplay($A2, "Window")

$A3=$A2

;Stage 2 <=====================
For $ti = 3 To $gi
    For $tj=2 To $gj
        $A3[$ti][$tj] = calc($A2, $ti, $tj, 2)
    Next
Next

_ArrayDisplay($A3, "Window")

Func calc($T, $i, $j, $c)
    $temp = $T[$i][$j] - ($T[$i][$c]/$T[$c][$c]) * $T[$c][$j]
    ;MsgBox(0,"","$T["&$i&"]["&$j&"] - ($T["&$i&"]["&$c&"]/$T["&$c&"]["&$c&"]) * $T["&$c&"]["&$j&"] = "&$temp);FOR DEBUG ONLY
    Return $temp
EndFunc

Edited by KaFu

Share this post


Link to post
Share on other sites
sharkos

Thx KaFu

So problem was

$A3=$A2
it should be after Step 1

Share this post


Link to post
Share on other sites
KaFu

Thx KaFu

So problem was

$A3=$A2
it should be after Step 1
De Nada :lmao:... maybe I should've said so :) , will work on my replying behavior...

Share this post


Link to post
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
Sign in to follow this  

×