Im writing a code that ask for two positive integers entered from the keyboard. The first integer N is odd in range of 3-15 and the second with be an initial value I. N will be the size of the array. Beginning the center of the NxN array with the integer I. If I is prime then print the number I in that position of the square. Otherwise print three asterisks 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 three asterisks if it is not. Continue going counter clockwise through the square until the square is full of numbers and three asterisks then print the array.

Thats the problem i have done most of it and im confused as to how to make the array and if any one could take a look at my code to let me know if i have my for loop set up correctly. Thank you kindly for your help! I dont know how to implement the spiral correctly if anyone could help please!!!

Heres my code:

#include <stdio.h>
#include <math.h>
#include <stdlib.h>

int IsPrime(int Nsize);

int main(int argc,char* argv[]) {

        int Nsize, Initial;
        int i;
        int j;
        int k;
        int **array;
        int totalsquares=Nsize*Nsize;
        int currentsquare=0;
        int currentsidelength=1;
        int currentpositiononside=0;
        int increasesidelength=0;
        int direction=0;

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

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


                ++currentsquare, ++Initial, ++currentpositiononside;
                if (currentpositiononside == currentsidelength) {
                        currentpositiononside = 0;
                        direction = (direction + 1) % 4;
                        if ((increasesidelength % 2) == 0) {
                                increasesidelength = 0;
                                printf("%d", array[i][j]);
        return 0;

int IsPrime(int Nsize){
        int i, sqrtNsize;

        if(Nsize<2)return 0;
        if(Nsize==2)return 2;
        if((Nsize%2)==0)return 0;
                if(Nsize%i==0)return 0;
        return Nsize;

Line 14:
totalsquares is being calculated from an uninitialized variable. Declare totalsquares, and don't give it a value until AFTER the user has entered the Nsize value.

Line 20:
Same thing with calculating i and j - do that AFTER Nsize has been entered.

The malloc just seems wrong:

array = malloc(Nsize * sizeof(int*));
for(i = 0;i<Nsize;i++) 
   array[i]=malloc(Nsize * sizeof(int));

array[totalsquares] is never a part of the array. In C, array indices go from 0 to totalsquares - 1 only.

And I have no idea why you'd malloc an address to an int array[index], anyway.

It's always a good idea to free these malloc'd memory in their reverse order, just before your program ends.

Be a part of the DaniWeb community

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