Kotlin Program to Add Two Matrix Using Multi-dimensional Arrays

Kotlin Program to Add Two Matrix Using Multi-dimensional Arrays: In the below program, the two matrices are stored in 2d array, namely first matrix and second matrix. We’ve also defined the number of rows and columns and stored them in variables rows and columns respectively.

Kotlin Program to Add Two Matrix Using Multi-dimensional Arrays

Source Code

fun main(args: Array<String>) {
    val rows = 2
    val columns = 3
    val firstMatrix = arrayOf(intArrayOf(2, 3, 4), intArrayOf(5, 2, 3))
    val secondMatrix = arrayOf(intArrayOf(-4, 5, 3), intArrayOf(5, 6, 3))

    // Adding Two matrices
    val 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]
        }
    }

    // Displaying the result
    println("Sum of two matrices is: ")
    for (row in sum) {
        for (column in row) {
            print("$column    ")
        }
        println()
    }
}

Output