Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Overview: STL vectors can grow and shrink dynamically and the member functions of the STL vector class can help us more conveniently handle the dynamic

image text in transcribedimage text in transcribedimage text in transcribed

Overview: STL vectors can grow and shrink dynamically and the member functions of the STL vector class can help us more conveniently handle the dynamic nature ofmemory allocation. A STL vector also maintains the information of its size (the number of elements currently in it) dynamically and so we don't need separate variables or parameters to record the sizes of vectors. You can just call the size() member function from a vector to know the current size of the vector The program should work in a way which allows the user to (i) provide and merge numbers in two sorted vectors into a single sorted vector and (ii) provide and sort the numbers in a vector Structure of your program: The Structure of the code should look a little like the following #include #include using namespace sta, bool mergeTwoSortedVectors (vector & vecA, vector & vecB, vector & vecc) //Your code void mergeSort(vector & vecToSort) //Your code int main(, //Your code Step 1: Implement the mergeTwoSortedVectors function Given (references to) two vectorcdouble> vectors vecA and vecB, each of which contains values already sorted in ascending order, implement the following function that can merge the values stored in these two separate sorted vector into a single vector vecC with all the values in it sorted in ascending order bool mergeTwoSortedVectors vector & vecA, vector & vecB, vector & vecC) //Your code Checking the precondition Overview: STL vectors can grow and shrink dynamically and the member functions of the STL vector class can help us more conveniently handle the dynamic nature of memory allocation. A STL vector also maintains the information of its size (the number of elements currently in it) dynamically and so we don't need separate variables or parameters to record the sizes of vectors. You can just call the size() member function from a vector to know the current size of the vector The program should work in a way which allows the user to (i) provide and merge numbers in two sorted vectors into a single sorted vector and (ii) provide and sort the numbers in a vector Structure of your program: You only need a single cpp file for this assignment and the structure should looks like the following #include #include using namespace std, bool mergeTwoSortedVectors vector & vecA, vector & vecB, vector & vecC) /Your code void mergeSort(vector & vecToSort) //Your code int main( //Your code Step 1: Implement the mergeTwoSortedVectors function Given (references to) two vector vectors vecA and vecB, each of which contains values already sorted in ascending order, implement the following function that can merge the values stored in these two separate sorted vector into a single vector vecC with all the values in it sorted in ascending order bool mergeTwoSortedVectors (vector & vecA, vector & vecB, vector & vecC) //Your code Checking the precondition the two sorted series stored in the first two vectors have been correctly merged into a sorted series stored in the third vector You have to make sure that mergeTwoSortedVectors is working perfectly (by doing extensive testing in Step 2) before you proceed to Step 3 below If you are 100% sure your mergeTwoSortedVectors is working, you can comment out the testing code in the main function when you proceed to Step 3 below Step 3: Implement the mergeSort function Implement the following recursive merge sort function by using the mergeTwoSortedSeries function implemented in Step 1 void mergeSort vector & vecToSort) //Your code . If the size of vecToSort is 1 or less, it is already sorted just return. .If the size of vecToSort is 2, compare the two elements in it and swap them if necessary, and then return. Otherwise, the size of vecToSort is at least 3. In this case, we should use two separate vectors vecl and vec2 (as local objects), resize them to the sizes vecToSort.size( )/2 and vecToSort.size) vecToSort.size( )/2 respectively, and copy the first vecToSort.size( )/2 elements in vecToSort into the first vector and the remaining vecToSort.size) - vecToSort.size()/2 into the second vector . Call mergeSortvecl) to sort the first vector . Call mergeSort(vec2) to sort the second vector . Call mergeIwoSortedVectors(vec vec2, vecToSort) to merge the twosorted vectors vecl and vec2 into vecToSort to make it a sorted vector Step 4: Test the implementation of the mergeSort functioin Create a loop in your main function to repeatedly do the following test on the mergeSort function until the user enters a negative value for n in the input: . Declare a vector vector. Ask the user to enter one non-negative integer n. Resize the vector to the size of n. Use a loop to ask the user to enter a series of n sorted values and store them in the vector of size n. Then call the mergeSort function appropriately to sort the vector. Output the contents of this final sorted series to verify the result. When you are sure mergeSort is working perfectly, the problem is finished Overview: STL vectors can grow and shrink dynamically and the member functions of the STL vector class can help us more conveniently handle the dynamic nature ofmemory allocation. A STL vector also maintains the information of its size (the number of elements currently in it) dynamically and so we don't need separate variables or parameters to record the sizes of vectors. You can just call the size() member function from a vector to know the current size of the vector The program should work in a way which allows the user to (i) provide and merge numbers in two sorted vectors into a single sorted vector and (ii) provide and sort the numbers in a vector Structure of your program: The Structure of the code should look a little like the following #include #include using namespace sta, bool mergeTwoSortedVectors (vector & vecA, vector & vecB, vector & vecc) //Your code void mergeSort(vector & vecToSort) //Your code int main(, //Your code Step 1: Implement the mergeTwoSortedVectors function Given (references to) two vectorcdouble> vectors vecA and vecB, each of which contains values already sorted in ascending order, implement the following function that can merge the values stored in these two separate sorted vector into a single vector vecC with all the values in it sorted in ascending order bool mergeTwoSortedVectors vector & vecA, vector & vecB, vector & vecC) //Your code Checking the precondition Overview: STL vectors can grow and shrink dynamically and the member functions of the STL vector class can help us more conveniently handle the dynamic nature of memory allocation. A STL vector also maintains the information of its size (the number of elements currently in it) dynamically and so we don't need separate variables or parameters to record the sizes of vectors. You can just call the size() member function from a vector to know the current size of the vector The program should work in a way which allows the user to (i) provide and merge numbers in two sorted vectors into a single sorted vector and (ii) provide and sort the numbers in a vector Structure of your program: You only need a single cpp file for this assignment and the structure should looks like the following #include #include using namespace std, bool mergeTwoSortedVectors vector & vecA, vector & vecB, vector & vecC) /Your code void mergeSort(vector & vecToSort) //Your code int main( //Your code Step 1: Implement the mergeTwoSortedVectors function Given (references to) two vector vectors vecA and vecB, each of which contains values already sorted in ascending order, implement the following function that can merge the values stored in these two separate sorted vector into a single vector vecC with all the values in it sorted in ascending order bool mergeTwoSortedVectors (vector & vecA, vector & vecB, vector & vecC) //Your code Checking the precondition the two sorted series stored in the first two vectors have been correctly merged into a sorted series stored in the third vector You have to make sure that mergeTwoSortedVectors is working perfectly (by doing extensive testing in Step 2) before you proceed to Step 3 below If you are 100% sure your mergeTwoSortedVectors is working, you can comment out the testing code in the main function when you proceed to Step 3 below Step 3: Implement the mergeSort function Implement the following recursive merge sort function by using the mergeTwoSortedSeries function implemented in Step 1 void mergeSort vector & vecToSort) //Your code . If the size of vecToSort is 1 or less, it is already sorted just return. .If the size of vecToSort is 2, compare the two elements in it and swap them if necessary, and then return. Otherwise, the size of vecToSort is at least 3. In this case, we should use two separate vectors vecl and vec2 (as local objects), resize them to the sizes vecToSort.size( )/2 and vecToSort.size) vecToSort.size( )/2 respectively, and copy the first vecToSort.size( )/2 elements in vecToSort into the first vector and the remaining vecToSort.size) - vecToSort.size()/2 into the second vector . Call mergeSortvecl) to sort the first vector . Call mergeSort(vec2) to sort the second vector . Call mergeIwoSortedVectors(vec vec2, vecToSort) to merge the twosorted vectors vecl and vec2 into vecToSort to make it a sorted vector Step 4: Test the implementation of the mergeSort functioin Create a loop in your main function to repeatedly do the following test on the mergeSort function until the user enters a negative value for n in the input: . Declare a vector vector. Ask the user to enter one non-negative integer n. Resize the vector to the size of n. Use a loop to ask the user to enter a series of n sorted values and store them in the vector of size n. Then call the mergeSort function appropriately to sort the vector. Output the contents of this final sorted series to verify the result. When you are sure mergeSort is working perfectly, the problem is finished

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Hands-On Database

Authors: Steve Conger

2nd Edition

0133024415, 978-0133024418

More Books

Students also viewed these Databases questions