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