954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Square deal in C

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...

coolfriends
Newbie Poster
5 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

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.

nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 

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

coolfriends
Newbie Poster
5 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

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++;
}
nezachem
Posting Shark
903 posts since Dec 2009
Reputation Points: 719
Solved Threads: 194
 

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 )

coolfriends
Newbie Poster
5 posts since Dec 2009
Reputation Points: 10
Solved Threads: 0
 

/*********************************************/
/* Programer: */
/* */
/* Program 64: Square Deal */
/* */
/* Approximate completion time: 1 hour */
/*********************************************/
#include
/* initializes functions and array */
int A[15][15];
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;i1;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);
}

1qazxsw2
Newbie Poster
1 post since Dec 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You