# Whats wrong in here? (arrays)

## Recommended Posts

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 on other sites

```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 on other sites

Input:

Step 1 result:

Step 2 result:

Step 2 desired result:

Whats going on?

##### Share on other sites

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

OS: Win10-1803 - 64bit - German, AutoIt Version: 3.3.14.5, AutoIt Editor: SciTE, Website: http://www.funk.eu, My unsolved Questions: Get default Window Size and Position, Rotate display by 270 degrees, Get Systray Icons, ...

##### Share on other sites

Thx KaFu

So problem was

`\$A3=\$A2`
it should be after Step 1

##### Share on other sites

Thx KaFu

So problem was

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

OS: Win10-1803 - 64bit - German, AutoIt Version: 3.3.14.5, AutoIt Editor: SciTE, Website: http://www.funk.eu, My unsolved Questions: Get default Window Size and Position, Rotate display by 270 degrees, Get Systray Icons, ...

## Create an account

Register a new account