Write a method that is passed an array, x, of doubles and an integer rotation am
ID: 3765709 • 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
public static double[] rotate(double[] d, int i)
{
if(i==0)return d;
double[] x = new double[d.length];
System.arraycopy(d, 1, x, 0, d.length-1);
x[x.length-1] = d[0];
i--;
return rotate(x,i);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.