I've the following code:
import random,strutils,rdstdin
type
Matrix[W,H:static[int]]=
array[1..W,array[1..H,int]]
proc RandMatrix(w,h,r1,r2:int):auto=
var
mat:Matrix[w,h]
for i in 1..w:
for j in 1..h:
mat[i][j]=rand(r1..r2)
return mat
var
w,h,r1,r2:int
w = parseInt(readLineFromStdin "W: ")
h = parseInt(readLineFromStdin "H: ")
r1 = parseInt(readLineFromStdin "R1: ")
r2 = parseInt(readLineFromStdin "R2: ")
var
mat=RandMatrix(w,h,r1,r2)
for i in 1..w:
for j in 1..h:
stdout.write mat[i][j]
echo ""
At the RandMatrix function, I declared:
var mat:Matrix[w,h]
But w,h are int, and the Matrix type just accept static[int], how can I solve that? I just tried to exchance static[int] for int, but without any success.I think your main problem is, that Nim's arrays have fixed compile time size, but you try to specify size at runtime by values read from keyboard. For a dynamic size container you can use seq data type, for a matrix a seq where each element is again a seq.
To get an idea you may compare
https://stackoverflow.com/questions/30298950/initialize-a-seq-of-seqs