If you are using the Terminal.java provided with Lab 1 from this course then things are fine. Otherwise, you should save a new copy of Terminal.java from the CS 241 home page. I've put a direct link to Terminal.java (from Lab 1) with the Lab 4 files. Also, I've added Terminal.java to the zip file with the provided Java classes.
Note: In the text, instead of the leaves being the lowest level of nodes, the leaves are the null children of the lowest level of nodes and they are black. If you would count these then there would be 3 levels but that was not the intention.
import java.util.*;As one of your skiplist class instance variables include
java.util.Random randseq;Then as part of your skiplist constructor include
randseq = new java.util.Random();
L1: 13, 2, 4, 0 L2: 5, 11, 1, 10, 2, 3, 7, 4, 5, 13 L3: 8, 10, 2, 5, 11, 9The output from the algorithm should be:
L1: 0, 2, 4, 13 L2, 1, 2, 3, 4, 5, 5, 7, 10, 11, 13 L3: 2, 5, 8, 9, 10, 11
Terminal.startTranscript("transcript.txt");
at the beginning of your main procedure. Similarly if
you would like the data to be read from the file
"sample-pointfile" then just add the line
points = PointReader.readXYPoints("sampel-pointfile");
Of course you want to then not run the code to do
the random generation. An option would be to have
the user either select between giving a file (and
then the above line can be called with the given
filename) or randomly generated (in which case
the user will just specify the number of points).
Any changes along these lines can be made to the driver
to avoid the need for command line arguments if they
are giving you trouble.
In either of these cases the most likely cause is that you are accessing an array out of bounds. If you are having trouble finding the error, then EVERY time you access an array, print out the name of the array, the size you allocated for it and the index you are accessing. The index should be between 0 and the size of the array-1.
In your C++ lab if you are getting a segmentation fault then I would be willing to bet (with a good stake) that you are accessing an array out of bounds. Furthermore, you are probably doing this for small values of n even if the segmentation fault isn't occuring. So how do you find this if you don't see it in the code? What I suggest is that EVERY time you access an array, print out the name of the array, the size you allocated for it and the index you are accessing. The index should be between 0 and the size of the array-1.
for (i=0; i < n; i++){
loop body
}
Remember that the value of i
in the last iteration of the loop
is n-1 (since it is the last integer
value less than n). Recall that the above loop is equivalent to:
i=0;
while (i < n){
loop body
i++;
}
The most common place where I'm seeing this error for Lab 1 is in the
final loop. In class I put:
for i = 0 to |Y'|-2
First, by |Y'| I mean the number of elements placed into the Y' array. In the above
for loop i should go from 0 (the lowest point) to |Y'|-2 (the second to highest
point).
varname = FastClosestPair.procedurename(...);
Return to the CS 241 Home Page