Terminal.readInputFromFile("testdata.txt");
(See the inventory program from Lab2 as an example.) Now assuming
that your data was in testdata.txt then the
Terminal.readInt() and Terminal.readDoule() as well
as the other Terminal read methods will all read from
testdata.txt versus receiving the input from the keyboard.
In the Lab 2 driver you can also see how you can have the input
(and transcript) file names specified as command line arguments versus
having them hardcoded in your program. However, setting this lab
up so command line arguments are used to specify the file names is
NOT necesary. Also, if they prefer to type in the data from
the keyboard you can but there's really no reason to do this.If you also want the output to go the a transcript file they make a call such as:
Terminal.startTranscript("transcript.txt");
and the output will be saved in the file transcript.txt.
Don't forget about the practice problems (both on the web page and in the
B-tree handout).
While these times will vary based on the hardware you use, if you plot
a graph of time versus the number of points the only thing that really
differs is the scale for the y-axis.
For those using C++, I've already defined maxfloat as 200000.0 in
the provided stdinc.h.
Recall that in C++ when you access an array out of bounds, if the memory
address you are accessing is allocated to your process (although not in
the array you intended to access) then there will be no run-time error
generated. Nevertheless, you may still be accessing an array out of
bounds.
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 timing the naive routine, notice that the sort routine is no
longer needed and thus the calls to it (and the prototype at the top)
should be removed.
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.
Homework Related Questions:
Lab 1 Questions:
public static double closestPair(XYPoint[] ptsByX, XYPoint[] ptsByY){
Put your method here... Also, of course you can pick whatever
variable names you want for the two arrays.
}
Then, for example, your call from Lab1.java would look like:
double ans = FastClosestPair.closestPair(ptsByX,ptsByY);
public final static double MAXDOUBLE = 200000.0;
Then you can use MAXDOUBLE in place of infinity.
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++;
}
I personally discourage the use of less than above but rather recommend
the for loop is written like:
for (i=0; i <= n-1; i++){
loop body
}
The most common place where I'm seeing this error 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).
Lab 2 Questions:
data.CopyInto(result);
Now in C++:
data->CopyInto(result);
Both of the above copy the data from the DataRecord referenced by
data into the DataRecord referenced by result.
Terminal.startTranscript("transcript.txt");
at the beginning of your main procedure.
Lab 2 Questions:
Return to the CS 241 Home Page
public static final int INF = java.lang.Integer.MAX_VALUE;
public static final int NEGINF = java.lang.Integer.MIN_VALUE;
public static final int NOTFOUND = -1;
public static final int NONE = -2;