Q:

Ruby program to cyclically permutes the elements of the array

0

In this program, we will create an array of integers and then we will cyclically permute the elements of the array and print the result.

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 the array is given below. The given program is compiled and executed successfully.

# Ruby program to Cyclically Permute the 
# elements of array

arr = [12,69,49,87,68];

print "Array elements before Cyclically Permutation: \n";
i=0;
while(i<5)
    print arr[i]," ";
    i=i+1;
end

i=0;
t = arr[0];
while(i<5)
    if(i==4)
        arr[i]=t;
    else
        arr[i]=arr[i + 1];
    end
    i = i + 1;
end
        
print "\nArray elements after Cyclically Permutation: \n";
i=0;
while(i<5)
    print arr[i]," ";
    i=i+1;
end

Output:

Array elements before Cyclically Permutation: 
12 69 49 87 68 
Array elements after Cyclically Permutation: 
69 49 87 68 12 

Explanation:

In the above program, we created an array of integer elements. Then we cyclically permutated the elements of the array. After that, we printed the result.

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

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now