CS 241 Frequently Asked Questions (FAQs)


HW 5 Related Questions:

Could you go over the specification for the four methods required for Problem 3?
Here is the specification for the four methods that you must support:

Lab 4 Related Questions:

While parsing flight-data.txt, I get a java.util.NoSuchElementException at java.util.StringTokenizer.nextToken. Do you have any idea what is causing this?
A few students have had this problem which appears to be caused by some extra carriage returns (or line feeds) being added at the end of flight-data.txt probably when it was opened in an editor to read it. Go to the end of flight-data.txt and be sure that there are no extra blank lines at the bottom. I also put flight-data.txt directly on the web page (versus just included in a zip file) so that a fresh copy can be saved in your directory. This should fix the problem -- If you open flight-data.txt in an edtior to look at it just don't save any changes made.

Could you provide the correct output for HeapTester.java?
No. It is important that you understand how the binary heap methods should work. There are never more than 5 items in the heap. Draw the heap after each operation by hand (in tree form) and then check that the output from HeapTester (which is shown in array form) is correct. You should be able to quickly do this and will lose an extra 5 points if your output is wrong and not marked as such.

GraphTester.java does not seem to be working.
The problem is that some students are using a version of Terminal.java from CS 101G that has a bug in readInputFromFile. In the first line it opens the FileInputStream with parameter fileName versus fn and so none of the input data is read.

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.


HW 4 Related Questions:

In problem 2c it talks about the B-tree having 3 levels but it only appears to have 2 levels?
As announced in class on Thursday this is a typo. The B-tree has 2 levels and thus the red-black tree should have 2 blacks on every path from the root to the leaf.

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.

In the solution provided to practice problem 5, there are red leaves but the book says that all leaves are black. Is there a problem with the solution?
The confusion here is caused by the fact that the text book would have another level of leaves of NILs that are black. That is A,C,E,J,M,R,V would have two black children that are NIL. K's right child would also be NIL. It might help to look at Figure 13.1 in the text. I'm using the third representation. Either way is fine in your homework.


Lab 3 Related Questions:

I believe there is a typo in the Lab 3 assignment that appears on the top of page 2 in the description of containsKey. It says that containsKey(-134) should return false but an event with the date of -134 exists.
Yes, that is a typo. For the 20events data set the call of containsKey(-134) should return true. The sample output correctly indicates that the key of -134 is in use.

Where is the variable randseq needed by the provided coinflip procedure declared?
At the top of your skiplist class you need
   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();

Homework 3 Related Questions:

For problem 1b, are x and y guaranteed to be in the array A?
No. x and/or y might or might not be in the array A.

For problem 1b, when you say between x and y, is that inclusive of x and y?
The intention was that it is inclusive. That is, an element A[i] is between x and y if x <= A[i] <= y. If you assumed the intention was x < A[i] < y, that is fine -- just clearly say so. (Of course, it should be clear from your solution which intepretation you picked.)

Could you give an example for Problem 4 so I can be sure I understand what the algorithm should compute?
So for example for r=3, k=14, and n=20, a possible input would be:
L1: 13, 2, 4, 0
L2:  5, 11, 1, 10, 2, 3, 7, 4, 5, 13
L3:  8, 10, 2, 5, 11, 9
The 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
For problem 4, I can't seem to create an O(n+r+k) algorithm, what should I do?
As stated in the homework, for partial credit describe the best algorithm you could create and analyze it's time complexity. For any more specific advice, you would have to describe to us (at office hours or via email) how you are currently trying to approach the problem.

Lab 2 Related Questions:

What should I send as the arguments for baseHash and stepHash?
You need to send them the key once it is converted to an integer. That is you want to send the value of toHashKey(key) to both. Thus the starting index is baseHash(toHashKey(key)), and the stepsize (which you should really only compute once) is stepHash(toHashKey(key)). A few students are sending the current index into the argument of stepHash but by doing this you no longer get the guarantee that all slots of the hash table will be visited before returning to where you started. Again, the reason that property does hold when done correctly is that the table size is a power of two and stepHash(toHashKey(key)) is an odd number that does not change for a given key. Thus when you start at some slot and keep adding the same number that is relatively prime to the hash table size you are guaranteed to visit every slot once before returning to your starting point.

In computing the min, max and average probes, I am having trouble varying the load factor with enough precision while still keeping the hash table size as a power of 2. What can I do?
Go ahead and allow the hash table size to no longer be a power of 2. Feel free to change the contructor for StringDictionary if this makes you task easier. If in doing so you end up with an infinite loop then you got unlucky and will most likely resolve the problem by picking a slightly different value for the table size. Remember that the purpose here is to empirically look at the number of probes across a lot of containKey method calls and this will achieve that goal.

Lab 1 Related Questions:

I found the crossover point for my naive algorithm and the divide-and-conquer algorithm. But when I use the naive algorithm for values of n less than the crossover, the hybrid algorithm is slower than the original divide-and-conquer algorithm. What is going on?
Most likely what has happened is that the crossover found for the distribution of points at the top-level call is larger than the crossover point for the distribution that occurs at the recursive calls. For the purpose of the lab, repeat the test for your hybrid algorithm where you switch to the naive algorithm at a much smaller value of n. Then report on your original findings as well as these, and then briefly discuss what you think is happening. That will satisfy the requirements of the lab.

What is the constant built into java for the largest integer?
Use either java.lang.double.MAX_INTEGER or java.lang.double.POSITIVE_INFINITY

Exactly what should I submit for Lab 1?
All of the below information is in the Lab 1 assignment but it may be easier for some for it to be stated separately from the rest of the assignment.Here is a detailed list of what must be submitted for each portion.

Where should the naive closest pair algorithm be put?
The easiest place would also be to put in ClosestPair.java (or ClosestPair.cc for those using C++). Just have two different closest pair procedures and then call the appropriate one from main.

I can't figure out how to set the command line arguments to get a transcript file from my Java program. Is there any other way to do it?
Add the line
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.

I'm using C++ and am having trouble with my makefile. What should I do?

If you need help using makefiles don't forget about the provided material on Using Simple Makefiles . Don't forget that the second line of each pair MUST begin with a TAB. Also note that you are NOT required to use a makefile. You can just directly compile and link your code.

For my C++ implementation of Lab1, I get a bus error. What does this mean?
For my C++ implementation of Lab1, I get a segmentation fault. What does this mean?
In C++ if you try to access a memory cell that is not allocated to your process but is rather allocated to the system (e.g. the operating system) then you get a bus error. If you try to access a memory cell that is not allocated to your process but is rather allocated to another user, then you get a segmentation fault.

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.

For my C++ implementation of Lab1, I get a segmentation fault but only for large values of n. What could this be?
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.

How do I allocate an array of a size given by a variable?
Look at the driver where X, Y, and P are allocated.

My program works correctly for some values of n, but not all of them. Any ideas why?
Often this problem is related to forgetting how for loops work. Suppose you have a loop of the form:
         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).

In my Java program, how do I call my fastest pair procedure?
Recall that in Java everything must be in a class. Thus your divide and conquer procedure (using the provided file) is in a class that I called FastClosestPair. Thus your procedure is then a static method for the class. Thus their call from main will look like
        varname = FastClosestPair.procedurename(...);

How do I compile and run my C++ procedures for Lab 1?
Make the changes required in lab1.cc (see the top of that file). A makefile is included with the provided code. Copy that into the directory with the rest of the code. Then just type "make". After successfull compilation and linking there will be a file called "lab1.out". Just type "lab1.out" to run it.


Homework Related Questions:


Old Lab Related Questions:


Return to the CS 241 Home Page