beatsspam - 8 months ago 47

Scala Question

`val matrix = Array.ofDim[Int](r,c);`

//matrix(0)(0) = 0;

for (i <- 0 to r) {

//println(i)

for (j <- 0 to c){

if (j == 0)

matrix(i)(j) = 1

}

}

Answer

Your Array has `r`

rows and `c`

cols, numbered from `0`

. You're trying to access an element out of those bounds when `i == r`

or `j == c`

.

```
for (i <- 0 until r) {
for (j <- 0 until c) {
...
}
}
```