0

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()
    }
}
6
  • What do you mean by 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? Commented Sep 27, 2022 at 10:31
  • hello yeah for example when the user running the program the user needs to give their own values to the array matrices. var firstMatrix = arrayOf(intArrayOf(5,8), intArrayOf(3,8)) var secondMatrix = arrayOf(intArrayOf(3,8), intArrayOf(8,9)) here I have given some default values with each arrays. Commented Sep 27, 2022 at 10:35
  • it means when I click the run button in the compiler. it should ask me to input values for the arrays. Commented Sep 27, 2022 at 10:40
  • in the above program, the output will be (5+3 = 8), (8+8=16), (3+8=11), (9+8=17) Commented Sep 27, 2022 at 10:46
  • Will the two arrays always be of size 2*2 or do you want the size also as user input? Commented Sep 27, 2022 at 11:53

1 Answer 1

1

You can use Kotlin's readln function to read user input from console. It gives you a string, which you can split with spaces to get all the numbers in one row which can then be mapped to Int from String.

val rows = 2
val columns = 2
println("Enter elements of first array:")
val firstMatrix = (1..rows).map {
    readln().split(' ').map { it.toInt() }
}
println("Enter elements of second array:")
val secondMatrix = (1..rows).map {
    readln().split(' ').map { it.toInt() }
}

After this you can use your existing code to find the sum matrix.

Sample execution:

Enter elements of first array:
1 2
3 4
Enter elements of second array:
3 5
2 5
Sum of two matrices is: 
4    7    
5    9
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.