My question is how to create a Dynamic 2D array in Kotlin. so that the user can input values when running the program. I have tried it with default values to add 2 matrices. but I need the program with a dynamic array and I NEED TO INPUT VALUES TO ARRAY EACH TIME WHEN I AM RUNNING THE PROGRAM. please help me to convert my code.
Below is my code.
fun main(args: Array<String>) {
var rows = 2
var columns = 2
var firstMatrix = arrayOf(intArrayOf(5,8), intArrayOf(3,8))
var secondMatrix = arrayOf(intArrayOf(3,8), intArrayOf(8,9))
// Adding Two matrices
var sum = Array(rows) { IntArray(columns) }
for (i in 0..rows - 1) {
for (j in 0..columns - 1) {
sum[i][j] = firstMatrix[i][j] + secondMatrix[i][j]
// println(sum[i][j])
}
}
/// Displaying the result
println("Sum of two matrices is: ")
for (row in sum) {
for (column in row) {
print("$column ")
}
println()
}
}
Need to input values to array each time when I am running the program? Do you want the array to persists across multiple program executions? Can you provide a sample input for your case?