CS 241 Frequently Asked Questions (FAQs)

Lab Questions
Homework Questions
Lab 1 Questions
Lab 2 Questions
Lab 3 Questions

Lab Related Questions:

Do I need to use file I/0 and tokenizers to read the input for Lab 4?

NO! The Terminal class (that provided with Lab 4 and the other variations provided with earlier labs) has a method readInputFromFile. So an example call would be
        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.


Homework Related Questions:


Don't forget about the practice problems (both on the web page and in the B-tree handout).


Lab 1 Questions:


Approximately what run times should I be seeing for my correct implementation of Lab 1?

Of course this will vary based on the speed of the processor you have. To give you a ballpark on a UltraSparc 30 using C++ with 6525 points it took approximately 0.4 seconds (versus 20 seconds for the naive algorithm) and using Java with 6525 points it took approximately 1 second (versus 1 minute for the naive algorithm). Of course, the naive algorithm gets substantially worse as the number of points increases! By 50,000 points there's a difference of 12.76 secs for the divide-and-conquer algorithmv ersus about 55 minutes for the naive algorithm.

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 my Java implementation of Lab 1, do I have to implement the closest pair procedure as a static method?

No, you do not have to. Feel free to modify any of the provided code. However, there is no probem with having this as a static method. If you are having trouble with how to call a static method, see the next question.

In my Java implementation of Lab 1, is it possible for me to call the closestPair method without instantiating a FastClosestPair object?

Yes. Suppose that your closestPair method returns a double. Then you can make it a public static method by using:
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);

For my Java implementation of Lab 1, do I have to use the provided Plotter class?

No, it is just there to help you in debugging.

I'm using C++ but would prefer to use a different compiler than the one on CEC. Is this possible?

Sure. However, you will be responsible for making any needed modifications to the provided code. The provided IntervalTimer will not work on other platforms since it makes calls into the Unix operating system. You can replace this by whatever system utility you have to get the time. Then by getting the time before and after the algorithm runs you can take their difference to determine the elapsed time. Just be sure that your procedure is the only process running. Only small changes (if any) should be required for the rest. If you have further questions, come see us.

In the provided Lab1.java it used the variables sortedX and sortedY. Shouldn't those be ptsByX and ptsByY, respectively?

Yes. I decided to pick better variables names than SortedX and SortedY just before putting it on the web page and neglected to change those 5 occurences. This has been corrected on the web page if you want to get new copies. However, it may be just as easy to edit the 5 occurences.

What do I use instead of infinity for my Lab 1 implementation?

You may take advantage of the fact that the x and y coordinates of each point are integers between 1 and 100,000. Thus any distance greater than the distance between two points would suffice. So for example, in Java you could put the following in your FastClosestPair class:
public final static double MAXDOUBLE = 200000.0;
Then you can use MAXDOUBLE in place of infinity.

For those using C++, I've already defined maxfloat as 200000.0 in the provided stdinc.h.

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++;
        }
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).

How do I compile and run my C++ procedures for Lab 1?

For the "B" part (including the version in which you also return the pair of points along with the distance), you are expected to put your divide-and-conquer algorithm in a file called "closest-pair.cc" You will also need to modify lab1b.cc as indicated. 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 "lab1b.out". Just type "lab1b.out" to run it.

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.


Lab 2 Questions:


Please clarify the semantics for the copyInto method of the DataRecord class.

Let me give an example call. First in Java:

    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.

In my Java Lab2 implementation, it is ignoring everything in the name after the underscore. Is there something I can do to fix this?

Change the underscore to a dash (-). There appears to be a bug in the Java StreamTokenizer.

When I have the output go to a file in Java (using the Terminal class), nothing goes to the screen.

This is the change made in Terminal.java. If you want the output to also go to the screen then you can use the Terminal class provided with lab 1. HOWEVER, you will notice that when your input also comes from a file then the output to the terminal will look terrible because it's missing the responses (and carriage returns) that the user would type. This is why I made the change. For debugging purposes you can make your own input file.

In Lab 2, the inventory program calls a remove procedure. Are we suppose to write this?

In the Lab 2 assignment, I intended to use the method name of remove instead of delete. The reason for this is that delete is a keyword in C++ and thus it couldn't be used as a method name. If you have already gotten started (which I hope many of you have), all you need to do is to change the CS241Dictionary delete method's name to remove. The rest is exactly as described in the Lab 2 assignment.

In hash-test-part2, there is a 0 at the beginning. Is this a typo?

Yes. Just remove that 0. I have changed it on the course web page if you prefer to get a new copy.

In the provided C++ inventory program (inventory.cc) when the user tries to remove a bar code that is not there it prints that the bar code is already in use versus that it is not in use.

inventory.cc has since been corrected. Please just get a new copy and recompile.

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.

How do I find where an array out of bounds error is in my Java program (when using Symantec Cafe)?

First rewrite main so that n is not taken as input. Instead just set n to whatever value you want to in main itself. Go into the debug menu. Then pick start debugging and select the green flag.

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.

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.

How do I open the transcript file for printing (in Symantec Cafe)?

When you select the "open" option in the File menu in Cafe, you may only see the java files. At the bottom left, where it says "List Files of Type", click and hold the mouse button on the arrow and drag down to the line that says "all files." When you release the mouse button, all the files in the folder will be shown. Scroll down to the one that says "transcript.txt" and select it for opening. A new window containing the transcript file will be created, and you can print it by selecting the "Print" option from its file menu.


Lab 2 Questions:


What should I use for infinity, negative infinity and NONE?

Here's a good solution (assuming that -1 is not a valid key) for those using Java. By assigning constants like this you can easily change them and the code is easier to read. For those using C++ define similar constants in your ".h" file.

  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;
Return to the
CS 241 Home Page