50 lines
1.1 KiB
Text
50 lines
1.1 KiB
Text
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
|
|
lastValue = dimension^2 - 1
|
|
loopFrom
|
|
loopTo
|
|
row
|
|
col
|
|
currDiag = 0
|
|
currNum = 0
|
|
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)
|