Write a method that is passed an array, x, of doubles and an integer rotation am
ID: 3770147 • Letter: W
Question
Write a method that is passed an array, x, of doubles and an integer rotation amount, n. The method creates a new array with the items of x moved forward by n positions. Elements that are rotated off the array will appear at the end. For example, suppose x contains the following items in sequence:
1 2 3 4 5 6 7
After rotating by 3, the elements in the new array will appear in this sequence:
4 5 6 7 1 2 3
Array x should be left unchanged by this method. Use the following code to help you get started. Be sure to test your program with different rotation amounts.
Explanation / Answer
In a straightforward way, we can create a new array and then copy elements to the new array. Then change the original array by using System.arraycopy().
for example:Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4].
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.