This lab consists of two C++ programs:
Download
sort.cpp and
queue.cpp to your machine and
transfer them to your home directory on the UMD CS Department
server
janus. (See
Using C++ At UMD in the menu.)
You may want to keep them in separate folders.
See menu at left for source program listings.
Each of the functions you must write is defined in its file without a body.
You should look in the
main function of each file to see how the functions
are to behave. There also are comments describing the functions.
When you have written a function and are ready to test it (for either
of the programs), you must compile it:
- It helps to comment out any code in the main
function that calls functions you have not yet written and tested
- Compile the file following the procedure described in Using
C++ At UMD in the menu to the left.
-
Fix any syntax errors.
Run the executable file
a.out. For both programs, success
messages will be shown when a function passes a test.
You are finished when both your
sort.cpp and
queue.cpp files
compile and run without assertion errors.
More details for these programs are given through the menu at left.
In this exercise you will complete an implementation of the merge sort
algorithm that is started for you.
This implementation is done with
arrays, so it is completely different than
the
list implementation given in chapter 7 of our text.
However, you must still understand the basic algorithm described in
chapter 4 on pp. 78—79
of the text. A diagram of the tree recursion used is
shown to the right.
The
sort.cpp file will compile and run as it is given to you,
but the final
assert statement
will fail because the array will not be sorted.
Note that the only function you must write is
merge.
When
merge is correctly implemented, there will be no assertion errors
and the output will look like that shown from the menu.
The menu also shows a pictorial trace of the
merge
algorithm.
This section shows a trace of the desired action of the
merge algorithm.
The example shows the final merging of the two halves of the array,
but the action will be similar for smaller parts.
Start by initializing variables to hold the indexes of the
beginnings of the parts of the array to merge.
The results of the merge will be put in a temporary array.
When finished, copy from the temporary array back to the original
array:
Compare the values at the indexes, put the smaller one in a
temporary array, and advance the appropriate index.
Compare again, and put the smaller value in the next location
in the temporary array.
Advance the appropriate index.
Repeat the process, eventually advancing both indexes to the
end of their respective parts of the array.
383 886 777 915 793 335 386 492 649 421
362 27 690 59 763 926 540 426 172 736
211 368 567 429 782 530 862 123 67 135
929 802 22 58 69 167 393 456 11 42
229 373 421 919 784 537 198 324 315 370
413 526 91 980 956 873 862 170 996 281
305 925 84 327 336 505 846 729 313 857
124 895 582 545 814 367 434 364 43 750
87 808 276 178 788 584 403 651 754 399
932 60 676 368 739 12 226 586 94 539
11 12 22 27 42 43 58 59 60 67
69 84 87 91 94 123 124 135 167 170
172 178 198 211 226 229 276 281 305 313
315 324 327 335 336 362 364 367 368 368
370 373 383 386 393 399 403 413 421 421
426 429 434 456 492 505 526 530 537 539
540 545 567 582 584 586 649 651 676 690
729 736 739 750 754 763 777 782 784 788
793 802 808 814 846 857 862 862 873 886
895 915 919 925 926 929 932 956 980 996
Merge Sort Test Passed
RUN SUCCESSFUL (total time: 313ms)
This program is a C++ version of the textbook exercise 13.13 you did for
the
Stacks and Queues Lab.
In that exercise, you implemented a queue ADT using a Racket 2-element vector
and Racket conses:
In this program you will do the same using C++ dynamically allocated arrays
and structures.
The queue implementation in
queue.cpp
is complete except for the
enqueue
and
dequeue functions, which you must provide.
See
Queues in C++ in the menu at left for a breakdown of the
structure of
queue.cpp.
Note that the
main function goes about testing the queue
implementation in a similar way to the
test-queue procedure in
the Racket stacks and queues lab.
queue.cpp will compile as is, but it will not survive the
assertions in
main.
When you have correctly implemented
enqueue and
dequeue, there
will be no assertion errors and the output will consist of "
Queue Test
Passed".
When finished, submit your
sort.cpp and
queue.cpp
files to
Lab: Arrays and Pointers in
.
The grader will test your code by compiling and running it and checking
that all assertions succeed.