Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Programming Assignment 7 CSCI 251 - Spring 2018 Instructions: For program 7, you will merge two arrays into one array that is in ascending order.
Programming Assignment 7 CSCI 251 - Spring 2018 Instructions: For program 7, you will merge two arrays into one array that is in ascending order. You may assume that both individual arrays are in ascending order. Consider the two following examples: Example 1: Array 1 Array 2 2 | 26 | 39 | 44 58 | 59 72 3 4 5 27 | 42 | 43 | 60 | 61 | 62 Merged Array [2 3 4 5 | 26 | 27 42 | 43 | 44 | 58 | 59 | 60 | 61 | 62 | 72 Example 2: Array 1 | 2 | 3 | 4 | 5 | 6 | 7 Array 2 8 9 10 11 12 Merged Array 2 3 4 5 6 7 8 9 10 11 12 Algorithm: Download the starting file mergeArrays.m from Blackboard, as well as the 4 input files: array1a.txt, array2a.txt, array1b.txt, and array2b.txt. Analyze the code in mergeArrays. You are being asked to add code to the commented sections as follows: 1. Populate the second array, similar to how the first array is populated 2. Traverse both arrays using a while loop until either one of the arrays has been entirely traversed (i.e., consider the length of each of the arrays). In the above examples, Example 1 will completely traverse array 2 and only partially traverse array 1. In example 2, array 1 will be completely traversed and array 2 will only partially be traversed. You must consider the following: a. Use three variables to traverse the indexes in each of the arrays - these will keep track of the current index of the current element to populate/visit in each. countFirst will keep track of the current index in first to visit, countSecond the current index in second to visit, and count Merge the current index in merge to populate. b. Within this while-loop, you will need to add the code to populate merge (either with first's element or second's element). Increment either countFirst or countSecond depending on which array you used. In either case, also increment count Merge. C. Note that as long as countFirst is less than first's length and countSecond is less than second's length you need to keep traversing within the while-loop. 3. Once one of the two arrays is completely traversed, you must still finish traversing the array that was only partially traversed. The code has been given to identify which one was partially traversed. You must add code that finishes traversing this array, assigning values to the merge array. 4. Traverse and output the merge array. Submit your updated mergeArrays.m. Include header comments (at the beginning of your m-file), and be sure to include the Honor Code statement and program description
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