#Array gives error when I cross the "limit"
1 messages · Page 1 of 1 (latest)
`
module main
import os {input}
fn main() {
i_max := 15
x_max := 20
mut tiles := [][]int{len:i_max, init: []int{len:x_max}}
for i:= 0; i < i_max; i+=1 {
println("")
for x:=0; x < x_max; x+=1 {
if i == 0 {
print("x")
tiles[0][x] = 1
}
else if i == i_max-1 {
print("x")
tiles[i_max][x] = 1
}
else {
if x == 0 || x == x_max-1 {
print("x")
tiles[i][x] = 1
}
else {
print(" ")
}
}
}
}
print(tiles)
}
`
this code returns error at line mut tiles := [][]int{len:i_max, init: []int{len:x_max}} cuz i cross the limit for the array
however when i edit the code to be mut tiles := [][]int{len:i_max+1, init: []int{len:x_max+1}} it doesn't give me error
what is weird; however, is the output
[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0]]
at the first array, it ends with a 0
so if it ends with a 0 then why can't i just remove the +1 from the len
here is what the code with +1 returns `
xxxxxxxxxxxxxxxxxxxx
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
x x
xxxxxxxxxxxxxxxxxxxx
discord f it up
bruh
here the error without +1
xV panic: array.get: index out of range (i == 15, a.len == 15) v hash: 2694624 C:/Users/AppData/Local/Temp/v_0/main.4957144200045912268.tmp.c:7984: at _v_panic: Backtrace C:/Users/AppData/Local/Temp/v_0/main.4957144200045912268.tmp.c:7236: by array_get C:/Users/AppData/Local/Temp/v_0/main.4957144200045912268.tmp.c:17895: by main__main C:/Users/AppData/Local/Temp/v_0/main.4957144200045912268.tmp.c:18288: by wmain 00494728 : by ??? 0049488b : by ??? 7ffbf86b26bd : by ???
Ye ik
Where does that happen tho
It looks like same error
for me, it shows that line 18 in your code is responsible
i.e. tiles[i_max][x] = 1
which indeed uses i_max as an index
not as a limit for the loop
not enough Vespene gas coffee
it is the spice that expands consciousness for a lot of people on Earth 🙂
oh u gotta use flag before run