Q:

Scala program to reverse an integer array

belongs to collection: Scala Array Programs

0

Here, we will create an integer array and then we will copy the elements of the array in reverse order into another 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 reverse an integer array is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to reverse an integer array

object Sample {  
    def main(args: Array[String]) {  
        var IntArray = Array(11,12,13,14,15)
        var RevArray = new Array[Int](5)
        
        var count1:Int=0
        var count2:Int=0
        
        
        //Reverse an array
        count1=0
        count2=4
        while(count1<5)
        {
            RevArray(count1)=IntArray(count2)
            
            count1=count1+1
            count2=count2-1
        }
        
        println("Array:")
        count1=0
        while(count1<5)
        {
            printf("%d ",IntArray(count1))
            count1=count1+1
        }
        println()
        
        println("Reversed Array:")
        count1=0
        while(count1<5)
        {
            printf("%d ",RevArray(count1))
            count1=count1+1
        }
        println()
    }
} 

Output:

Array:
11 12 13 14 15 
Reversed Array:
15 14 13 12 11 

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 IntArrayRevArray.  The IntArray contains 5 integer elements. Then we copied the elements of IntArray into RevArray in revered order. After that, we printed the elements of both arrays 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 find the prime numbers from the a... >>
<< Scala program to calculate the sum of array elemen...