Kotlin Program to Multiply to Matrix Using Multi-dimensional Arrays

Kotlin Program to Multiply to Matrix Using Multi-dimensional Arrays: In this program, you’ll learn to multiply two matrices using multi-dimensional arrays in Kotlin.

Kotlin Program to Multiply to Matrix Using Multi-dimensional Arrays

Source Code

fun main(args: Array<String>) {
    val r1 = 2
    val c1 = 3
    val r2 = 3
    val c2 = 2
    val firstMatrix = arrayOf(intArrayOf(6, -1, 5), intArrayOf(2, 0, 6))
    val secondMatrix = arrayOf(intArrayOf(7, 4), intArrayOf(-2, 0), intArrayOf(0, 4))

    // Mutliplying Two matrices
    val product = Array(r1) { IntArray(c2) }
    for (i in 0..r1 - 1) {
        for (j in 0..c2 - 1) {
            for (k in 0..c1 - 1) {
                product[i][j] += firstMatrix[i][k] * secondMatrix[k][j]
            }
        }
    }

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

Output