ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Scala program to merge two integer arrays into a third array

Here, we are going to learn how to merge two integer arrays into a third array in Scala programming language?
Submitted by Nidhi, on May 08, 2021

Problem Solution:

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

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 IntArray1, IntArray2. 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.

Scala Array Programs »

ADVERTISEMENT




Comments and Discussions

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.