Q:

Scala program to create a two-dimensional array by using an array of array

belongs to collection: Scala Array Programs

0

Here, we will create a two-dimensional array of integers by using an array of array, and then we will print the elements of the array on the console screen.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Program/Source Code:

The source code to create a two-dimensional array by using an array of an array is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to create a two-dimensional array 
// by using array of array.

import scala.util.control.Breaks._

object Sample {
  def main(args: Array[String]) {
    var TwoDArr = Array(Array(1, 2, 3, 4, 5), Array(6, 7, 8, 9, 10))
    var i: Int = 0
    var j: Int = 0

    printf("Two dimensional matrix.\n")
    i = 0;
    while (i < 2) {
      j = 0;
      while (j < 5) {
        printf("%d ", TwoDArr(i)(j));
        j = j + 1;
      }
      i = i + 1;
      println();
    }
  }
}

Output:

Two dimensional matrix.
1 2 3 4 5 
6 7 8 9 10

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 a two-dimensional array TwoDArr by using an array of the array that contains 2 rows each row contains 5 items. Then we printed the two-dimensional array by using the nested loop on the console screen.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Scala Array Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Scala program to read and print MATRIX using a two... >>
<< How to extract unique elements from sequences in S...