Write a method that accepts two sorted integer arrays, a and b, and returns a so
ID: 3568119 • Letter: W
Question
Write a method that accepts two sorted integer arrays, a and b, and returns a sorted array containing the values stored in both a and b. In other words, your method should merge arrays a and b into a third array. You can merge two sorted arrays as follows:
Declare a new array c that is large enough to hold the contents of both a and b; also declare two integer variables, i and j. Initialize i and j to 0.
Compare a[i] and b[j] and copy the smaller value into c.
Increment i if a contains the smaller value, otherwise increment j.
Repeat this procedure until either i or j exceeds the highest index of a or b respectively.
Copy the remainder of either a or b to c.
Return c.
Include this method in a program that has a method for each of the following items, plus the method main:
Interactively reads two lists of integers into two arrays. Each list ends with the sentinel value of -999. (See Exercise la.)
Sorts the arrays (use the bubble sort algorithm);
Merges the arrays; and
Displays the merged array.
The two lists might not be the same size. Make sure that your program keeps track of how many data are in each list.
Use a method for each task. Design, implement, and test one method before including the next.
Explanation / Answer
100% Fresh answer, guaranteed to get you best grades possible. Just check it out: http://programmingtutor3.tk/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.