Scala program to multiply two matrices

Here, we are going to learn how to multiply two matrices in Scala programming language?
Submitted by Nidhi, on May 20, 2021 [Last updated : March 10, 2023]

Scala – Multiply Two Matrices

Here, we will create three 2X2 matrices using a two-dimensional array and then we will read elements for two matrices. After that, we will multiply two matrices and assign the result to the third matrix.

Scala code to multiply two matrices

The source code to multiply two matrices is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to multiply two matrices

object Sample {
  def main(args: Array[String]) {
    var Matrix1 = Array.ofDim[Int](2, 2)
    var Matrix2 = Array.ofDim[Int](2, 2)
    var Matrix3 = Array.ofDim[Int](2, 2)

    var i: Int = 0
    var j: Int = 0
    var k: Int = 0

    var sum: Int = 0

    printf("Enter elements of MATRIX1:\n")
    i = 0;
    while (i < 2) {
      j = 0;
      while (j < 2) {
        printf("ELEMENT(%d)(%d): ", i, j);
        Matrix1(i)(j) = scala.io.StdIn.readInt();
        j = j + 1;
      }
      i = i + 1;
    }

    printf("Enter elements of MATRIX2:\n")
    i = 0;
    while (i < 2) {
      j = 0;
      while (j < 2) {
        printf("ELEMENT(%d)(%d): ", i, j);
        Matrix2(i)(j) = scala.io.StdIn.readInt();
        j = j + 1;
      }
      i = i + 1;
    }

    //Multiply Matrix1 and Matrix2
    i = 0;
    while (i < 2) {
      j = 0;
      while (j < 2) {
        sum = 0;
        k = 0;
        while (k < 2) {
          sum = sum + (Matrix1(i)(k) * Matrix2(k)(j));
          k = k + 1;
        }
        Matrix3(i)(j) = sum;
        j = j + 1;
      }
      i = i + 1;
    }

    printf("MATRIX1:\n")
    i = 0;
    while (i < 2) {
      j = 0;
      while (j < 2) {
        printf("%d ", Matrix1(i)(j));
        j = j + 1;
      }
      i = i + 1;
      println();
    }

    printf("MATRIX2:\n")
    i = 0;
    while (i < 2) {
      j = 0;
      while (j < 2) {
        printf("%d ", Matrix2(i)(j));
        j = j + 1;
      }
      i = i + 1;
      println();
    }

    printf("Multiplication of Matrix1 and Matrix2:\n")
    i = 0;
    while (i < 2) {
      j = 0;
      while (j < 2) {
        printf("%d ", Matrix3(i)(j));
        j = j + 1;
      }
      i = i + 1;
      println();
    }
  }
}

Output

Enter elements of MATRIX1:
ELEMENT(0)(0): 10
ELEMENT(0)(1): 20
ELEMENT(1)(0): 20
ELEMENT(1)(1): 10
Enter elements of MATRIX2:
ELEMENT(0)(0): 10
ELEMENT(0)(1): 20
ELEMENT(1)(0): 20
ELEMENT(1)(1): 10
MATRIX1:
10 20 
20 10 
MATRIX2:
10 20 
20 10 
Multiplication of Matrix1 and Matrix2:
500 400 
400 500

Explanation

In the above program, we used an object-oriented approach to create the program. We created an object Sample, and we defined main() function. The main() function is the entry point for the program.

In the main() function, we created three 2X2 matrices using a two-dimensional array, and then we read the elements for Matrix1 and Matrix2 from the user. Then we multiply Matrix1 and Matrix2 and assign the result into Matrix3. After that, we printed the multiplication of matrices on the console screen.

Scala Array Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.