ork 7: Merging two sorted arrays Due date: Saturday March 18. Objectives: Learn to implement algorithms that work with multiple arrays. Understand the Merging algorithm. IMPORTANT. Carefully study Lecture 1 from Week 8 on D2L before you start on this.   Sometimes we need to combine the values in two sorted sequences to produce a larger sorted sequence. This process is called Merging. As an example, if the first sequence (S1)contains the numbers 2 3 4 4 5 9 and the second sequence (S2) contains the numbers 1 4 5 8 9 11 12 13 17 the output sequence will contain 1 2 3 4 4 4 5 5 8 9 9 11 12 13 17 Each sequence can be stored in an array. The strategy is as follows: Start by comparing the first items in the two input arrays. Write the smaller number to the output array, and move to the the next item in that array. This is repeated until we reach the end of any one of the input arrays; thereafter, we simply copy all the numbers from the other input array to the output array. (In the above example, we look at 2 from S1 and 1 from S2; since 1 is smaller, we write 1 to the output and look at the next number (4) from S2, etc. After we examine 9, there are

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

Homework 7: Merging two sorted arrays Due date: Saturday March 18. Objectives: Learn to implement algorithms that work with multiple arrays. Understand the Merging algorithm. IMPORTANT. Carefully study Lecture 1 from Week 8 on D2L before you start on this.

 

Sometimes we need to combine the values in two sorted sequences to produce a larger sorted sequence. This process is called Merging. As an example, if the first sequence (S1)contains the numbers 2 3 4 4 5 9 and the second sequence (S2) contains the numbers 1 4 5 8 9 11 12 13 17 the output sequence will contain 1 2 3 4 4 4 5 5 8 9 9 11 12 13 17 Each sequence can be stored in an array. The strategy is as follows: Start by comparing the first items in the two input arrays. Write the smaller number to the output array, and move to the the next item in that array. This is repeated until we reach the end of any one of the input arrays; thereafter, we simply copy all the numbers from the other input array to the output array. (In the above example, we look at 2 from S1 and 1 from S2; since 1 is smaller, we write 1 to the output and look at the next number (4) from S2, etc. After we examine 9, there are are no numbers left in S1; we then copy the numbers 11, 12, 13 and 17 from S2 to the output.)

 

Question 1. Write psuedocode to implement this strategy. The psuedocode should match the structure of a while loop, and will have three loops: The first loop will run until one of the input arrays reaches its end. Following this we have an if statement that checks to see which array ended. If the first array ended, we enter a loop that copies all the remaining items from the second array to the output array. Otherwise, we enter a loop that copies all the remaining items from the first array to the output array. (Note that since the first loop runs until the end of a sequence is reached, only one of the last two loops will execute in a given instance.)

 

Question 2. Write a program that does the following: ˆ Declares two arrays of 20 items each, and one array of 40 items ˆ Asks for the names of the input files and the output file. ˆ Calls the function readSortedArray (twice) to read at most 20 numbers from the each of the input files, and stores them in the two arrays of size 20. ˆ If both files can be successfully read, the program merges the sequences stored in the two arrays of size 20, into the array of size 40. 1 Prints all the numbers in the array of size 40,to the output file, on one line with inter- leaving spaces.

 

CAN YOU PLEASE WRITE WHOLE CODE FOR QUESTION 2 IN C++ AND MAKE SURE IT WILL RUN . I HAVE PROBLEM RUNNING CODE .

Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Quicksort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education