/* this code attempts to generate a sudoku board in an non graphical envoirnment 
   the code compiles fine but however it throws segmentation fault everytime it's run*/

# include<stdio.h>
# include<stdlib.h>
# include<time.h>
static int baseArray[9][9];

static void initialise();
void display();
static int checkPosition(int row, int col, int value);

int main(void)
{
   srand(time(NULL));
   initialise();
   display();
    return 0;
}

static void initialise()
{ 
   int i, j, value, row = 0, col = 0, flag = 0;
   for(i=0;i < 30; )
      {
         printf("inside initialise\n");
         row = rand() % 8;          //randomising rows columns and value
	 col = rand() % 8;
	 value = rand() % 8 + 1;

         flag = checkPosition(row, col, value);   //check for repeat values
  
	 if(flag == 1)
	    initialise();       
	 else
	 {
	 printf("assingning values to baseArray\n");
	 baseArray[row][col] = value;
	 i++;
         }
      }
}


static int checkPosition(int row ,int col, int value)
{
  int counter = 0, counter_1 = 0;
  printf("inside checkPosition\n");
   for(counter = 0; counter < 9 ;counter++)        //check if value already exist in column
	 {
	    if(baseArray[row][counter] == value)
	    {
	         printf("values already there in matrix rows\n");
	         return 1;
	    } 
	 }
	  for(counter = 0 ; counter < 9;counter++)  // check if value alreasy exist in rows
	 {
	    if(baseArray[counter][col] == value)
	    {
	    printf("value already there in matrix column\n");
	     return 1;
	    }
	 }
    for(counter = row; counter < (row + 3); counter++)     //check if value already exist within the 3*3 matrix 						           //around block
    {
       for(counter_1 = col; counter_1 < (col + 3); counter_1++)
       {
           if(baseArray[counter][counter_1] == value)
           {
              printf("value there within three blocks of selected sqare\n");
              return 1;
           }
       }
    }
return 0;
}


void display()
{
int i = 0,j = 0;
for(i = 0; i < 9; i++)
   {
      for(j = 0; j < 9; j++)
      {
          printf(" %d ", baseArray[i][j]);  
      }
   printf("\n");
   }
}

I was hoping you guys might help...
anyway leave it ..
I've fixed the problem

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.