#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;
}
}
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("");
}
}
}
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.
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
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++;
}
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).
varname = FastClosestPair.procedurename(...);
Terminal.startTranscript("transcript.txt");
at the beginning of your main procedure.
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