0

Ok basically I have a program that is doing a counter clockwise spiral and will print the value i or print out a 0 depending upon if the number is prime. n is equal the length of the spiral while i is the initial center value of the spiral which is incremented with each shift in the spiral. I need the spiral to insert the values counter clockwise starting at the center. The problem I'm having is a segmentation fault. When I put it through a debugger it's pointing to the second else statement:

`else a[k][l] = 0 ;`

but I'm not really sure whats wrong with the pointers. Anyone got any ideas? I tried to snip the code a little bit below. I cut out the parts I knew were working.

```
n = 5 ;
i = 16 ;
in = 2 ;
m = i + ( (n * n) - 1 ) ;
k = n / 2 + 1 ;
l = n / 2 + 1 ;
a = (int **) malloc( sizeof( int * ) * n ) ;
for ( x = 0 ; x < n ; x++ ) a[i] = (int *) malloc( sizeof(int) * n ) ;
while ( i < m + 1 ) {
for (j = 0 ; j < in ; j++, i++, l++) {
if ( IsPrime(i) ) a[k][l] = i ;
else a[k][l] = 0 ;
if ( j == in - 1 ) break ;
}
i++ ;
k++ ;
for (j = 0; j < (in-1); j++, i++, k++) {
if ( IsPrime(i) ) a[k][l] = i ;
else a[k][l] = 0 ;
if ( j == ( in - 1 ) - 1 ) break;
}
i++ ;
l-- ;
in++ ;
for (j = 0; j < in; j++, i++, l--) {
if ( IsPrime(i) ) a[k][l] = i ;
else a[k][l] = 0 ;
if ( j == in - 1 ) break;
}
i++ ;
k-- ;
for (j = 0; j < (in-1); j++, i++, k--) {
if ( IsPrime(i) ) a[k][l] = i ;
else a[k][l] = 0 ;
if ( j == ( in - 1 ) - 1 ) break;
}
i++ ;
l++ ;
}
```