Question
The main objective of this assignment is to demonstrate your understanding file input and output, and to implement an algorithm to merge two unsorted data
The main objective of this assignment is to demonstrate your understanding file input and output, and to
implement an algorithm to merge two unsorted data
files into one sorted data file.
Objectives to be met
1
Demonstrate how to merge two sorted lists into one sorted output file.
2
Demonstrate how to implement the given algorithm to solve the merge problem.
Use the
start-
up source code file MergeSort.cpp provided to complete this assignment. Start by creating a new
empty project and the
n
copy the source code (.cpp) file and data .txt files into the project folder. Then right
-
click the Source node in Solution Explorer to Add Existing item into your projects solution. Be sure you can
compile, load and run the program to make sure that much works, which should print some messages to the
screen and create an output file that initially will be empty
you will be merging two sorted lists into one sorted
output file. Complete the TODO comments where indicated in the source code file
and as outlined in the
required merge algorithm
.
Given input files A and B read into two arrays and then sorted separately, the program
will use those two lists
and the algorithm that follows to write the merged output file AB (
Note: TestScoresMergeAB file should
contain just one column
):
TestScoresA
88
87
88
66
55
56
75
75
78
80
80
90
99
87
44
34
0
100
TestScoresB
80
91
55
67
75
90
80
100
35
98
0
66
99
87
75
34
15
90
88
100
44
83
TestScoresMergeAB
0
0
15
34
34
35
44
44
55
55
56
66
66
67
75
75
75
75
78
80
.
.
.
.
80
80
80
83
87
87
87
88
88
88
90
90
90
91
98
99
99
100
100
100
Requirements
1
Do not modify the input data files.
2
Do not define additional arrays in your program solution.
3
The two existing arrays, already sorted, must be merged externally into the required output file.
4
Use the algorithm that
follows as the basis of your solution.
5
After the merged output file is closed, reset the counters and write a loop to print both lists side
-
by
-
side to the standard output screen; it should similar to the first two columns on the first page
of these instructions.
Hint
: you will need a decision about how to use
setw()
to finish up.
This will not be an interactive program, but console output should include at least the following messages about
program activity:
Opening files....
Reading files....
Merging files....
Finishing up....
End program.
Use the algorithm shown here to implement your solution.
Merge list
-
A and list
-
B into output file
-
AB
// implement first part of merge
until (end
-
of
-
list
-
A) or (end
-
of
-
list
-
B)
if next
-
A <= next
-
B then
write next
-
A to file
-
AB
increment
list
-
A counter
else
write next
-
B to file
-
AB
increment list
-
B counter
// finish writing other list
if (end
-
of
-
list
-
A) then
until (end
-
of
-
list
-
B)
write next
-
B to file
-
AB
increment list
-
B counter
else
until (end
-
of
-
list
-
A)
write next
-
A to file
-
AB
increment list
-
A counter
close output file
-
AB
Considerations:
The algorithm uses different counters for each of the arrays. You need to maintain those two counters, one
for each list as indicated in the solution algorithm.
The list counters need to be compared to the lengths of their corresponding lists in order to determine when
the end of list A or the end of list B has been reached. Notice also that one of the lists will end before the
other in the first part, so when that loop ends, a follow up IF
-
ELSE with nested loops is used to determine
which array still needs to be added to the output file.
Additional Requirements:
Include your name at the top of the main program files as a comment.
Compress and submit that one
assignment project folder with all the source code, data files, and other project files.
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