Hey guys,

I am working on this square deal problem that I cant figure out.

This is the problem:

Input for this program will be two positive integers entered from the keyboard via scanf. The first will be an odd integer N in the range of 3 to 15. N will be used as the size of a square array. The second integer will be an initial value I. Both N and I will be small enough so the I + N squared is less than 1000.

Begin in the center of an N x N array, with the integer I. If I is prime, then print the number I in that position of the square. Otherwise, print *** in that position. Move to the right one square, and test the integer I+1 for primality. Print I+1 if it is prime and *** if it is not. Proceed in a counterclockwise spiral through the square until the square is full of numbers and ***. Then print the array.

Example: If N=5 and I = 16 then your output should be:

*** 31 *** 29 ***
*** *** 19 *** ***
*** *** *** 17 ***
*** *** 23 *** ***
*** 37 *** *** ***

This is what I have done so far, but I cant figure out how to set up my for loop in main function:

``````#include <stdio.h>

int main( ) {

int n, i;

printf( "Enter an odd integer n between 3 and 15: " ) ;
scanf( "%d", &n ) ;

printf( "Enter an initial value i: " ) ;
scanf( "%d", &i ) ;

int arr[n][n] ;

for( j = 0 ; j < n ; j++ ) {
for( k = 0 ; k < n ; k++ ) {
if( IsPrime( i ) == 1 ) return i ;
else
printf( "***" ) ;
}
arr[j][k] = arr[j+1][k+1] ;
}

return 0;
}

int IsPrime( int n ) {

int i, count = 0 ;

for( i = 1 ; i <= n ; i++ ) {
if( ( n % i ) == 0 ) count++ ;
}

return ( count == 2 ) ;

}``````

Any help would be very much appreciated.

Thank you...

## All 5 Replies

The main loop should fill the array (not print it, but just fill) in the following manner:

``````K steps to the right
K steps up
increment K
K steps left
K steps down
increment K``````

Break the loop as soon as K reaches N.
Of course, each step increments I as well.
A step to the left changes current coordinates (i, j) to (i - 1, j), a step down - to (i, j + 1) etc.
Once the array is filled up, print it in another loop.

The main loop should fill the array (not print it, but just fill) in the following manner:

``````K steps to the right
K steps up
increment K
K steps left
K steps down
increment K``````

Break the loop as soon as K reaches N.
Of course, each step increments I as well.
A step to the left changes current coordinates (i, j) to (i - 1, j), a step down - to (i, j + 1) etc.
Once the array is filled up, print it in another loop.

thanks for your help but i guess i am not understanding it clearly..
i would really appreciate if you could show me in code...

thanks

thanks for your help but i guess i am not understanding it clearly..
i would really appreciate if you could show me in code...

thanks

Something along the lines of

``````int k = 1;
int i = N/2 + 1;
int j = N/2 + 1;
while (k < N) {
int s;
// going right:
for (s = 0; s < k; s++, i++, I++)
arr[i][j] = isPrime(I)? I: -1;
// going up:
for (s = 0; s < k; s++, j--, I++)
arr[i][j] = isPrime(I)? I: -1;
k++;
// going left:
for (s = 0; s < k; s++, i--, I++)
arr[i][j] = isPrime(I)? I: -1;
// going down:
for (s = 0; s < k; s++, j++, I++)
arr[i][j] = isPrime(I)? I: -1;
k++;
}``````

Something along the lines of

``````int k = 1;
int i = N/2 + 1;
int j = N/2 + 1;
while (k < N) {
int s;
// going right:
for (s = 0; s < k; s++, i++, I++)
arr[i][j] = isPrime(I)? I: -1;
// going up:
for (s = 0; s < k; s++, j--, I++)
arr[i][j] = isPrime(I)? I: -1;
k++;
// going left:
for (s = 0; s < k; s++, i--, I++)
arr[i][j] = isPrime(I)? I: -1;
// going down:
for (s = 0; s < k; s++, j++, I++)
arr[i][j] = isPrime(I)? I: -1;
k++;
}``````

thanks again...so how do I print out the values in a different for loop? (as you mentioned in your previous post )

``````/*********************************************/
/* Programer:                                */
/*                                           */
/* Program 64: Square Deal                   */
/*                                           */
/* Approximate completion time:  1 hour      */
/*********************************************/
#include <stdio.h>
/* initializes functions and array */
int A;
int prime(int n);
int square(int n, int i, int o);
int main( int argc, char *argv[] ) {
/* initalizes variables */
int N;
int I;
int i;
int m;
int j;
int ans;
/* prompt user and check entry */
printf("Please enter an odd dimension between 3 and 15 for the array :\n");
scanf("%d",&N);
if(N%2==0 || N > 15 || N < 3){
printf("invalid entry\n");
return 0;
}
printf("Please enter a starting number beween 1 and 100 for the spiral :\n");
scanf("%d",&I);
if(I<1 || I>100){
printf("invalid entry");
return 0;
}
/* fill center space and call function */
A[(N-1)/2][(N-1)/2]=I;
m = square(N,I,0);

/* print box */
for(i=0;i<N;i++){
for(j=0;j<N;j++){
ans=prime(A[j][i]);
if (ans == 0)
printf(" *** ");
else if(A[j][i]<10)
printf("  %d  ",A[j][i]);
else if(A[j][i]<100)
printf("  %d ",A[j][i]);
else
printf(" %d ",A[j][i]);
}
printf("\n");
}

return 0;

}
/* identifies prime numbers */
int prime(int n) {

int i;
int count = 0;
for(i=1;i<n;i++) {
if(n%i==0)
count++;
}
if (count==1)
return n;
else
return 0;
}

/* populates array */
int square(int n, int i, int o) {

int h;
int v;
int c = i + (n * n) - 1;
if(n == 1){
return 0;
}
for(h=1;h<n;h++){
A[n-h+o][n-1+o] = c;
c--;
}
for(v=1;v<n;v++){
A[n-h+o][n-v+o] = c;
c--;
}
for(h=h;h>1;h--){
A[n-h+o][n-v+o] = c;
c--;
}
for(v=v;v>1;v--){
A[n-1+o][n-v+o] = c;
c--;
}

return square(n-2,i,++o);
}
``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.