OneCompiler

multiplication of two matrices

282

Q6 Write a program for multiplication of two matrices(Validate number of rows andcolumns before multiplication and give appropriate message)

object matrix {
def main(args: Array[String])
{
val a = Array.ofDimInt
val b = Array.ofDimInt
val c = Array.ofDimInt
var i = 0
var j = 0
var k = 0
println("Enter the elements of first matrix: ")
for(i <- 0 to 1; j <- 0 to 1)
{
a(i)(j) = scala.io.StdIn.readInt()
}
println("Enter the elements of second matrix: ")
for(i <- 0 to 1; j <- 0 to 1)
{
b(i)(j) = scala.io.StdIn.readInt()
}
if(a(0).length == b.length)
{
for(i <- 0 to 1; j <- 0 to 1; k <- 0 to 1)
{
c(i)(j) = c(i)(j) + a(i)(k) * b(k)(j)
}
println("Product of two matrix is: ")
for(i <- 0 to 1; j <- 0 to 1)
{
print(c(i)(j) + " ")
println()
}
}
}
}
}