Answered step by step
Verified Expert Solution
Question
1 Approved Answer
In Java Add a function reverse to IntArrayutils which reverses the given array of sub-array. There should be two versions, one that works with the
In Java
Add a function reverse to IntArrayutils which reverses the given array of sub-array. There should be two versions, one that works with the entire array, and one that works with a sub-array defined by a half-open range. The signatures should be: public static void reverse (int[] a) public static void reverse (int[] a, int lo, int hi) The version that works with the entire array should call the sub-array version, just like we did in class with the other IntArrayutils functions. For example, assume that array a = {1, 1, 2, 3, 5, 8, 13, 21). Then reverse(a) results in a = { 21, 13, 8, 5, 3, 2, 1, 1} reverse(a, 0, 4) results in a = { 3, 2, 1, 1, 5, 8, 13, 21 } reverse (a, 4, 8) results in a = { 1, 1, 2, 3, 21, 13, 8, 5 } reverse (a, 2, 6) results in a = { 1, 1, 8, 5, 3, 2, 13, 21 } reverse(a, 3, 3) results in no change to a Note that reversing an empty sub-array does nothing. Your function may assume that o slo s his a.length. You'll notice that I provided an implementation of swap for you to use. Consider that a hint. BTW, there's a really elegant (IMO) solution to this. I'm curious to see if anyone finds it. I have supplied a main function that does some rudimentary testing; this way you can do some testing before you submit your code for grading. The real tests do not use this code, so feel free to modify the tests or add more of them. LUdu telduit lepidle... 1 public class IntArrayUtils { // Your implementation of reverse() goes here. // I've provided an implementation of swap() if you want to use it. // Take that as a hint. public static void swap(int[] a, int idx1, int idx2){ int t = a[idx1]; a[idx1] = a[idx2]; a[idx2] = t; // This is a test client that performs the tests described in the // assignment description. If all goes well, no output is produced. public static void main(String[] args) { int[] a = { 1, 1, 2, 3, 5, 8, 13, 21 }; int[] result = { 1, 1, 8, 5, 3, 2, 13, 21 }; reverse(a, 2, 6); for (int i = 0; iStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started