Q:

Scala program to merge two integer arrays into a third array

belongs to collection: Scala Array Programs

0

Here, we will create two integer arrays and then we will merge elements of both arrays into a third array.

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 merge two integer arrays into a third array is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to merge two integer arrays 
// into third array

object Sample {  
    def main(args: Array[String]) {  
        var IntArray1 = Array(10,11,12,13,14,15)
        var IntArray2 = Array(20,21,22,23,24,25)
        var IntArray3 = new Array[Int](12)
        var count:Int=0
        var count1:Int=0
        
        // Merge IntArray1 and IntArray2 into IntArray3.
        while(count<12)
        {
            if(count<6)
            IntArray3(count)=IntArray1(count)
            else
            {
                IntArray3(count)=IntArray2(count1)
                count1=count1+1
            }
            count=count+1
        }
        
        println("Elements of merged array:")
        count=0
        while(count<12)
        {
            printf("%d ",IntArray3(count))
            count=count+1
        }
        
        println()
    }
} 

Output:

Elements of merged array:
10 11 12 13 14 15 20 21 22 23 24 25

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 two integer arrays IntArray1IntArray2. Both arrays contain 6-6 elements. Then we merged elements of both arrays into a third array IntArray3. After that, we printed the elements of the merged array 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 create an array of strings... >>
<< Scala program to concatenate two integer arrays...