CS 241 Frequently Asked Questions (FAQs)


Lab 4 Related Questions:

In C++ how do I allocate a n by n two-dimensional array where n's value is not known until runtime?

Here is a sample program that allocates an two-dimensional array A and then sets A[i][j] to i+j (just to demonstrate the use). It uses the same stdinc.h as provided in Lab 1 or Lab 2.

#include "stdinc.h"

main(){
   int **A;
   int n;
   cin >> n;
   A = new int*[n];
   for (int i=0; i<=n-1; i++)
      A[i] = new int[n];

// You can now access an entry with A[i][j] as shown below
// Note that the above is all you need to do to allocate
// the array.  What is below is just to demonstrate the use.
// You shouldn't do anything like the below for your lab

   for (i=0; i<=n-1; i++)
      for (int j=0; j<=n-1; j++)
         A[i][j] = i+j;

   cout << "Here is the matrix " << endl;
   for (i=0; i<=n-1; i++){
      for (int j=0; j<=n-1; j++)
        cout << A[i][j] << " ";
      cout << endl;
   }
}   
In Java how do I allocate a n by n two-dimensional array where n's value is not known until runtime?

Here is a sample program that allocates an two-dimensional array A and then sets A[i][j] to i+j (just to demonstrate the use). It uses the same Terminal class as that provided in Labs 1 and 2.
public class Test {

   public static void main(String args[]){
      int n = Terminal.readInt();
      int A[][] = new int[n][n];

// You can now access an entry with A[i][j] as shown below.
// Note that the above is all you need to do to allocate
// the array.  What is below is just to demonstrate the use.
// You shouldn't do anything like the below for your lab

      for (int i=0; i<=n-1; i++)
         for (int j=0; j<=n-1; j++)
            A[i][j] = i+j;

     Terminal.println("Here is the matrix");
      for (int i=0; i<=n-1; i++){
         for (int j=0; j<=n-1; j++)
           Terminal.print(A[i][j] + " ");
         Terminal.println("");
      }
   }   
}
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.


Homework Related Questions:

For Homework 3, in all problems in which you are asked for a time complexity, give the worst-case time complexity.

For #4, unlike #3, you are to consider the standard implementation in which the lists are unsorted.

For #7 you can assume that there is at least one element in each of the k lists. That is, n > = k.


Old Lab Related Questions:

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.

For LAB 1, Please explain how the X and Y arrays work?
I'm still seeing some students confused about the X and Y arrays. I've found the best way to help students understand is to give an example. So here it is. Below are a few points (where these points were "randomly" selected).


            P[5]
              *

       P[0]
        *
                  P[3]
                   *
P[1] *
            * P[2]

  * P[4]
While I've shown the points graphically, they are really stored as an x-coord, y-coord, and the unique identifier num used by leftof and below. Here's the value for the X and Y arrays which are arrays of indices into the point array. So for example P[X[0]] is the leftmost point and P[X[5]] is the rightmost point. Likewise P[Y[0]] is the lowest point. So,for example, if you want to look at the x-coordinate of the lowest point you would use P[Y[0]].x()

X[0] = 4,  Y[0] = 4
X[1] = 1,  Y[1] = 2
X[2] = 0,  Y[2] = 1
X[3] = 2,  Y[3] = 3
X[4] = 5,  Y[4] = 0
X[5] = 3,  Y[5] = 5
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).

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(...);

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.

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.


Return to the CS 241 Home Page