Zig-zag matrix: Difference between revisions

Content added Content deleted
(Add entry for Beads language)
Line 906: Line 906:
10 18 19 23 24
10 18 19 23 24
</pre>
</pre>

=={{header|Beads}}==
This is a translation of the [[Zig-zag_matrix#C++|C++]] example.
<lang Beads>beads 1 program 'Zig-zag Matrix'

calc main_init
var test : array^2 of num = create_array(5)
printMatrix(test)
calc create_array(
dimension:num
):array^2 of num
var result : array^2 of num
var lastValue = dimension^2 - 1
var currNum = 0
var currDiag = 0
var loopFrom
var loopTo
var row
var col
loop
if (currDiag < dimension) // if doing the upper-left triangular half
loopFrom = 1
loopTo = currDiag + 1
else // doing the bottom-right triangular half
loopFrom = currDiag - dimension + 2
loopTo = dimension
loop count:c from:loopFrom to:loopTo
var i = loopFrom + c - 1
if (rem(currDiag, 2) == 0) // want to fill upwards
row = loopTo - i + loopFrom
col = i
else // want to fill downwards
row = i
col = loopTo - i + loopFrom
result[row][col] = currNum
inc currNum
inc currDiag
if (currNum > lastValue)
exit
return result
calc printMatrix(
matrix:array^2 of num
)
var dimension = tree_count(matrix)
var maxDigits = 1 + lg((dimension^2-1), base:10)
loop across:matrix ptr:rowp index:row
var tempstr : str
loop across:rowp index:col
tempstr = tempstr & " " & to_str(matrix[row][col], min:maxDigits)
log(tempstr)</lang>
{{out}}
<pre> 0 1 5 6 14
2 4 7 13 15
3 8 12 16 21
9 11 17 20 22
10 18 19 23 24</pre>


=={{header|Befunge}}==
=={{header|Befunge}}==