import scala.util.Random
object Main {
val ROWS = 5
val COLS = 4
def main(args: Array[String]): Unit = {
val matrix = Array.ofDim[Int](ROWS, COLS)
fillMatrixWithRandom0and1(matrix, ROWS, COLS)
printMatrix(matrix, ROWS, COLS)
}
def fillMatrixWithRandom0and1(matrix: Array[Array[Int]], rows: Int, cols: Int): Unit = {
val rand = new Random()
for (i <- 0 until rows) {
for (j <- 0 until cols) {
matrix(i)(j) = rand.nextInt(2) // Generates either 0 or 1
}
}
}
def printMatrix(matrix: Array[Array[Int]], rows: Int, cols: Int): Unit = {
val f = "%2d"
for (i <- 0 until rows) {
for (j <- 0 until cols) {
print(f.format(matrix(i)(j)))
}
println()
}
}
}
/*
run:
0 0 1 1
0 0 0 1
1 1 0 0
1 0 1 1
1 1 0 1
*/