Question
JAVA Help Please. PROBLEM 3: merge together two sorted arrays of ints into a new array. * * Example1 merge: [-1 3 5 7 ]
JAVA Help Please.
PROBLEM 3: merge together two sorted arrays of ints into a new array. * * Example1 merge: [-1 3 5 7 ] with [ 2 4 6 8] would yield [-1 2 3 4 5 6 7 8] * Example2 merge: [1 6 ] with [ 2 3 8 9] would yield [1 2 3 6 8 9] * There is no guarantee about the size of either array. When/if you run out of elements in * either array, copy all the remaining elements from the nonempty array to the the new array * preconditions: * both arrays are sorted low to high * there are no duplicate values among the two arrays * either array may be empty * postcondition: return an array with all elements from both arrays sorted from low to high * * You may not use any additional methods, sorting routines etc * For full credit, your solution may only go through each array one time ( so in particular - no nested loops) * * You will need to create a new array inside the function * You do not have to write this recursively. */
public static int[] mergeArrays( int[] a, int[] b) {
int[] answer = new int[0]; // an empty array to have something to return return answer; // ToDo 3 . Fix this. }
/* * exercising functions and main. * There are no Todo's for you in the code below. */ public static void mergeArrayTests() {
int a[] = new int[] {1,3,5,7,9,11}; int b[] = new int[] {2,4,6}; int[] combinedArray = mergeArrays( a,b); StdOut.println("merging: "+ Arrays.toString(a) + " " + Arrays.toString(b)); StdOut.println(" --> " + Arrays.toString(combinedArray));
int c[] = new int[] {1,3,5,7,9,11}; int d[] = new int[] {2,4}; combinedArray = mergeArrays( c,d); StdOut.println("merging: "+ Arrays.toString(c) + " " + Arrays.toString(d)); StdOut.println(" --> " + Arrays.toString(combinedArray));
int e[] = new int[] {1,3,5,7,9,11}; int f[] = new int[] {}; combinedArray = mergeArrays( e,f); StdOut.println("merging: "+ Arrays.toString(e) + " " + Arrays.toString(f)); StdOut.println(" --> " + Arrays.toString(combinedArray));
int g[] = new int[] {3,11}; int h[] = new int[] {2,4,6,8,10}; combinedArray = mergeArrays( g,h); StdOut.println("merging: "+ Arrays.toString(g) + " " + Arrays.toString(h)); StdOut.println(" --> " + Arrays.toString(combinedArray)); }
public static void main (String[] args) {
StdOut.println(" This main function calls your functions for a variety of inputs"); StdOut.println(" and simply prints the results"); StdOut.println(" It does NOT check to see if your functions return the correct values"); StdOut.println(" It is up to you to review the output and verify the results"); StdOut.println("***************************************************************"); int[] list0 = new int[] {}; int[] list1 = new int[] { 5 }; int[] list2 = new int[] { 3, 4 }; int[] list3 = new int[] { -2, 3, -4 }; int[] list4 = new int[] { -1, -2, -4, -5 }; int[] list5 = new int[] { 6, 1, 2, -3, 8 };
StdOut.format(" list: %s sum of positives: %d ",Arrays.toString(list0), sumOfPositivesRecursive (list0)); StdOut.format(" list: %s sum of positives: %d ",Arrays.toString(list1), sumOfPositivesRecursive (list1)); StdOut.format(" list: %s sum of positives: %d ",Arrays.toString(list2), sumOfPositivesRecursive (list2)); StdOut.format(" list: %s sum of positives: %d ",Arrays.toString(list3), sumOfPositivesRecursive (list3)); StdOut.format(" list: %s sum of positives: %d ",Arrays.toString(list4), sumOfPositivesRecursive (list4)); StdOut.format(" list: %s sum of positives: %d ",Arrays.toString(list5), sumOfPositivesRecursive (list5)); StdOut.println();
StdOut.println ("Reverse: Before: " + Arrays.toString(list1 ) ); reverseArray (list1); StdOut.println (" After: " + Arrays.toString (list1) + " " );
StdOut.println ("Reverse: Before: " + Arrays.toString(list2 ) ); reverseArray (list2); StdOut.println (" After: " + Arrays.toString (list2) + " ");
StdOut.println ("Reverse: Before: " + Arrays.toString(list3 ) ); reverseArray (list3); StdOut.println (" After: " + Arrays.toString (list3) + " ");
StdOut.println ("Reverse: Before: " + Arrays.toString(list4 ) ); reverseArray (list4); StdOut.println (" After: " + Arrays.toString (list4) + " ");
StdOut.println ("Reverse: Before: " + Arrays.toString(list5 ) ); reverseArray (list5); StdOut.println (" After: " + Arrays.toString (list5) + " ");
mergeArrayTests(); StdOut.println("***************************************************************"); StdOut.println(" Output was not verified to be correct by this program. "); StdOut.println(" It is up to you to review the output and verify the results"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored 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