Q:

Scala program to Cyclically Permutes the Elements of an Array

belongs to collection: Scala Array Programs

0

Here, we will create an array of integers and then we will permute the elements of the array cyclically.

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 Cyclically Permute the Elements of an Array is given below. The given program is compiled and executed on the ubuntu 18.04 operating system successfully.

// Scala program to Cyclically Permute 
// the Elements of an Array

object Sample {  
    def main(args: Array[String]) {  
        var IntArray = Array(31,15,42,14,23)
        var i:Int=0
        var t:Int=0
        
        println("Array elements before Cyclically Permutation: ");
        i=0;
        while(i<5)
        {
            printf("%d ",IntArray(i));
            i=i+1;
        }
        
        i=0;
        t = IntArray(0);
        while(i<5)
        {
            if(i==4)
                IntArray(i)=t;
            else
                IntArray(i)=IntArray(i+1);
            
            i=i+1;
        }
        
        println("\nArray elements after Cyclically Permutation: ");
        i=0;
        while(i<5)
        {
            printf("%d ",IntArray(i));
            i=i+1;
        }
        println()
    }
}

Output:

Array elements before Cyclically Permutation: 
31 15 42 14 23 
Array elements after Cyclically Permutation: 
15 42 14 23 31 

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 an array IntArray that contains 5 integer items. Then we cyclically permute the elements of the array and then print them 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 delete an item from the array... >>
<< Scala program to sort an array in descending order...