1. ## Sudoku Code

I am a beginner at C++ and I would be so kind is someone can help me fix my code, especially with the count integers in part 1. Here's my code. Thanks!

Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define size 9

//part 1 = row, column, grid loops
int obeysSudokuRules(int a[9][9])
{
int count[10][10];
//count[r][i] = 9;
//count[c][i] = 9;
//int count[];
int i,r,c,z;//r = row, c = column, z = # of grids
int k = 0;//k = # of rows in a grid
int j = 0;//j = # of columns in a grid

for (r = 0; r < size; r++)//start from 1st row to check columns
{
for(c = 0; c < size; c++)
{
count[r][c]++;
if ( (count[r][c]) > 1)
return 0;
}
for (i = 0; i < size; i++)
{
count[i][r] = 0;
}// to clear
}//end row loop
for (i = 0; i < size; i++)
{
count[i] = 0;
}// to clear
for (c = 0; c < size; c++)//start from 1st column to check rows
{
for(r = 0; r < size; r++)
{
count[r][c]++;
if ( (count[r][c]) > 1)
return 0;
}
for (i = 0; i < size; i++)
{
count[i][c] = 0;
}//to clear
}//end column loop
for (i = 0; i < size; i++)
{
count[i] = 0;
}//to clear
r = 0;
c = 0;
for (z = 0; z < size; z++)
{
while (k < 3)
{
while(j < 3)
{
count[r][c]++;
if ( (count[r][c]) > 1)
return 0;
c++;
j++;
}//end of j loop
r++;
k++;
}//end of k loop
if(z < 2)
{
r = 0;
}
if(z == 2)
{
c = 0;
r = 3;
}
if((z > 2) && (z < 5))
{
r = 3;
}
if(z == 5)
{
c = 0;
r = 6;
}
if(z > 5)
{
r = 6;
}
for (i = 0; i < size; i++)
{
count[i] = 0;
} //to clear //end z loop
return 0;
}
}//end grid loop

//part 2 = print grid
void printGrid(int a[9][9])
{
int r, c;

for(r = 0; r < size; r++)
{
for(c = 0; c < size; c++)
{
if ((a[r][c]) == 0)
{
printf("  ");
}
else
{
printf("%d", a[r][c]);
}
printf("\n");
}
}
}

//part 3 = generate grid
int genGrid(int a[9][9], int row, int column)
{
int i, r;
int swap;
int currentRow[size] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int obeysSudokuRules;

if (row == size)
return 1;

for(i = 0; i < size -1; i++)
{
r = i+ (rand()%8);
swap = currentRow[i];
currentRow[i] = currentRow[r];
currentRow[r] = swap;
}

for(row = 0; row < size; row++)
{
for(column = 0; column < size; column++)
a[row][column] = currentRow[i];
if (obeysSudokuRules == '1')
{
if (column == size -1)
{
obeysSudokuRules = genGrid(a, row + 1, 0);
}
else
{
obeysSudokuRules = genGrid(a, row, column + 1);
}
//if(obeysSudokuRules)
return 1;
}
}

a[row][column] = 0;
return 0;
}

//part 4 = filling zeroes
void fillZeros (int a[9][9])
{
int counter = 0;
int r, random;

for(r = 0; r < size; r++)
{
while (counter <= 4)
{
random = (rand()%8);
a[r][random] = 0;
counter++;
}
}
}

//part 5 = generating partial grid
void genPartialGrid(int a[9][9])
{
int obeysSudokuRules;
int r, c;

do
{
for(r = 0; r < size; r++)
{
for(c = 0; c < size; c++)
{
a[r][c]= 0;
}//fill array with zeros
}
obeysSudokuRules = genGrid(a, 0, 0);

}
while (obeysSudokuRules == 0);

fillZeros(a);
}

//part 6 = determining row
int getRowNum(void)
{
int row;

printf("Please enter the row number that you want to enter the value\n");
scanf("%d", &row);
while ((row < 0) || (row > 8))
{
printf("Please enter the row number that you want to enter the value\n");
scanf("%d", &row);
}
return row;
}

//part 7 = determining column
int getColumnNum(void)
{
int column;

printf("Please enter the column number that you want to enter the value\n");
scanf("%d", &column);
while ((column < 0) || (column > 8))
{
printf("Please enter the column number that you want to enter the value\n");
scanf("%d", &column);
}
return column;
}

//part 8 = receiving value
int getValue(void)
{
int value;

scanf("%d", &value);
return value;
}

int compSolve(int a[][size])
{
int r = 0;
int c = 0;
int checkZero;
while(r < 9)
{
while(c < 9)
{
if((a[r][c]) == 0)
checkZero = 0;
else checkZero = 1;
c++;
}
r++;
}
return checkZero;
}

int main(void)
{
int a[9][9];
int r,c, value, obeysSudokuRules, compSolve;

srand(time(NULL));//randomize random number generation
printf("Welcome to Sudoku++! All numbers in rows and columns must not be repeated");
genPartialGrid(a);//call function genPartialGrid to generate a partially filled grid that does not violate the rules
printGrid(a);//call function printGrid
do
{
r = getRowNum();//call function getRowNum to determine row number
c = getColumnNum();//call function getColumnNum to determine column number
value = getValue();
printGrid(a);//call function to get the value (between 1 & 9)
a[r][c] = value;//enter the value in the grid at the row and column positions entered
//call function printGrid
}
while ((obeysSudokuRules == 1) && (compSolve == 0));
if ((compSolve == 1) && (obeysSudokuRules == 1))
{
printf("Congratulations, you have solved Sudoku++!");
printGrid(a);
}
else//not all Sudoku rules are obeyed
{
}
printf("Thank you for playing!");

return 0;
}```

2. Is this supposed to be C or C++? You mentioned C++ and posted in the C++ programming forum, but your code appears to use C headers only and no C++ specific syntax.

Your code formatting is atrocious: you indent by way too much, so much so that your code becomes unreadable. Generally, the indent level is fixed at between two to eight spaces, or one tab (which may in turn be displayed as between two to eight spaces), as a matter of subjective style. If you want to use GNU style, then the brace's indentation would be half an indent level, e.g., if you are indenting by four spaces, you would indent the brace by two spaces:
Code:
```if (x)
{
foo();
}```
In the code that you posted, it is as if your indent level is a whopping 36 spaces!

Here's your code formatted according to Allman style with an indent level of four spaces:
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define size 9

//part 1 = row, column, grid loops
int obeysSudokuRules(int a[9][9])
{
int count[10][10];
//count[r][i] = 9;
//count[c][i] = 9;
//int count[];
int i,r,c,z;//r = row, c = column, z = # of grids
int k = 0;//k = # of rows in a grid
int j = 0;//j = # of columns in a grid

for (r = 0; r < size; r++)//start from 1st row to check columns
{
for(c = 0; c < size; c++)
{
count[r][c]++;
if ( (count[r][c]) > 1)
return 0;
}
for (i = 0; i < size; i++)
{
count[i][r] = 0;
}// to clear
}//end row loop

for (i = 0; i < size; i++)
{
count[i] = 0;
}// to clear
for (c = 0; c < size; c++)//start from 1st column to check rows
{
for(r = 0; r < size; r++)
{
count[r][c]++;
if ( (count[r][c]) > 1)
return 0;
}
for (i = 0; i < size; i++)
{
count[i][c] = 0;
}//to clear
}//end column loop

for (i = 0; i < size; i++)
{
count[i] = 0;
}//to clear

r = 0;
c = 0;
for (z = 0; z < size; z++)
{
while (k < 3)
{
while(j < 3)
{
count[r][c]++;
if ( (count[r][c]) > 1)
return 0;
c++;
j++;
}//end of j loop
r++;
k++;
}//end of k loop

if(z < 2)
{
r = 0;
}
if(z == 2)
{
c = 0;
r = 3;
}
if((z > 2) && (z < 5))
{
r = 3;
}
if(z == 5)
{
c = 0;
r = 6;
}
if(z > 5)
{
r = 6;
}
for (i = 0; i < size; i++)
{
count[i] = 0;
} //to clear //end z loop
return 0;
}
}//end grid loop

//part 2 = print grid
void printGrid(int a[9][9])
{
int r, c;

for(r = 0; r < size; r++)
{
for(c = 0; c < size; c++)
{
if ((a[r][c]) == 0)
{
printf("  ");
}
else
{
printf("%d", a[r][c]);
}
printf("\n");
}
}
}

//part 3 = generate grid
int genGrid(int a[9][9], int row, int column)
{
int i, r;
int swap;
int currentRow[size] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int obeysSudokuRules;

if (row == size)
return 1;

for(i = 0; i < size -1; i++)
{
r = i+ (rand() % 8);
swap = currentRow[i];
currentRow[i] = currentRow[r];
currentRow[r] = swap;
}

for(row = 0; row < size; row++)
{
for(column = 0; column < size; column++)
a[row][column] = currentRow[i];

if (obeysSudokuRules == '1')
{
if (column == size -1)
{
obeysSudokuRules = genGrid(a, row + 1, 0);
}
else
{
obeysSudokuRules = genGrid(a, row, column + 1);
}
//if(obeysSudokuRules)
return 1;
}
}

a[row][column] = 0;
return 0;
}

//part 4 = filling zeroes
void fillZeros (int a[9][9])
{
int counter = 0;
int r, random;

for(r = 0; r < size; r++)
{
while (counter <= 4)
{
random = (rand()%8);
a[r][random] = 0;
counter++;
}
}
}

//part 5 = generating partial grid
void genPartialGrid(int a[9][9])
{
int obeysSudokuRules;
int r, c;

do
{
for(r = 0; r < size; r++)
{
for(c = 0; c < size; c++)
{
a[r][c]= 0;
}//fill array with zeros
}
obeysSudokuRules = genGrid(a, 0, 0);
}
while (obeysSudokuRules == 0);

fillZeros(a);
}

//part 6 = determining row
int getRowNum(void)
{
int row;

printf("Please enter the row number that you want to enter the value\n");
scanf("%d", &row);
while ((row < 0) || (row > 8))
{
printf("Please enter the row number that you want to enter the value\n");
scanf("%d", &row);
}
return row;
}

//part 7 = determining column
int getColumnNum(void)
{
int column;

printf("Please enter the column number that you want to enter the value\n");
scanf("%d", &column);
while ((column < 0) || (column > 8))
{
printf("Please enter the column number that you want to enter the value\n");
scanf("%d", &column);
}
return column;
}

//part 8 = receiving value
int getValue(void)
{
int value;

scanf("%d", &value);
return value;
}

int compSolve(int a[][size])
{
int r = 0;
int c = 0;
int checkZero;
while(r < 9)
{
while(c < 9)
{
if((a[r][c]) == 0)
checkZero = 0;
else checkZero = 1;
c++;
}
r++;
}
return checkZero;
}

int main(void)
{
int a[9][9];
int r, c, value, obeysSudokuRules, compSolve;

srand(time(NULL));//randomize random number generation
printf("Welcome to Sudoku++! All numbers in rows and columns must not be repeated");
genPartialGrid(a);//call function genPartialGrid to generate a partially filled grid that does not violate the rules
printGrid(a);//call function printGrid
do
{
r = getRowNum();//call function getRowNum to determine row number
c = getColumnNum();//call function getColumnNum to determine column number
value = getValue();
printGrid(a);//call function to get the value (between 1 & 9)
a[r][c] = value;//enter the value in the grid at the row and column positions entered
//call function printGrid
}
while ((obeysSudokuRules == 1) && (compSolve == 0));

if ((compSolve == 1) && (obeysSudokuRules == 1))
{
printf("Congratulations, you have solved Sudoku++!");
printGrid(a);
}
else//not all Sudoku rules are obeyed
{
}
printf("Thank you for playing!");

return 0;
}```

3. Originally Posted by laserlight
Is this supposed to be C or C++? You mentioned C++ and posted in the C++ programming forum, but your code appears to use C headers only and no C++ specific syntax.

Your code formatting is atrocious: you indent by way too much, so much so that your code becomes unreadable. Generally, the indent level is fixed at between two to eight spaces, or one tab (which may in turn be displayed as between two to eight spaces), as a matter of subjective style. If you want to use GNU style, then the brace's indentation would be half an indent level, e.g., if you are indenting by four spaces, you would indent the brace by two spaces:
Code:
```if (x)
{
foo();
}```
In the code that you posted, it is as if your indent level is a whopping 36 spaces!

Here's your code formatted according to Allman style with an indent level of four spaces:
Code:
```#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define size 9

//part 1 = row, column, grid loops
int obeysSudokuRules(int a[9][9])
{
int count[10][10];
//count[r][i] = 9;
//count[c][i] = 9;
//int count[];
int i,r,c,z;//r = row, c = column, z = # of grids
int k = 0;//k = # of rows in a grid
int j = 0;//j = # of columns in a grid

for (r = 0; r < size; r++)//start from 1st row to check columns
{
for(c = 0; c < size; c++)
{
count[r][c]++;
if ( (count[r][c]) > 1)
return 0;
}
for (i = 0; i < size; i++)
{
count[i][r] = 0;
}// to clear
}//end row loop

for (i = 0; i < size; i++)
{
count[i] = 0;
}// to clear
for (c = 0; c < size; c++)//start from 1st column to check rows
{
for(r = 0; r < size; r++)
{
count[r][c]++;
if ( (count[r][c]) > 1)
return 0;
}
for (i = 0; i < size; i++)
{
count[i][c] = 0;
}//to clear
}//end column loop

for (i = 0; i < size; i++)
{
count[i] = 0;
}//to clear

r = 0;
c = 0;
for (z = 0; z < size; z++)
{
while (k < 3)
{
while(j < 3)
{
count[r][c]++;
if ( (count[r][c]) > 1)
return 0;
c++;
j++;
}//end of j loop
r++;
k++;
}//end of k loop

if(z < 2)
{
r = 0;
}
if(z == 2)
{
c = 0;
r = 3;
}
if((z > 2) && (z < 5))
{
r = 3;
}
if(z == 5)
{
c = 0;
r = 6;
}
if(z > 5)
{
r = 6;
}
for (i = 0; i < size; i++)
{
count[i] = 0;
} //to clear //end z loop
return 0;
}
}//end grid loop

//part 2 = print grid
void printGrid(int a[9][9])
{
int r, c;

for(r = 0; r < size; r++)
{
for(c = 0; c < size; c++)
{
if ((a[r][c]) == 0)
{
printf("  ");
}
else
{
printf("%d", a[r][c]);
}
printf("\n");
}
}
}

//part 3 = generate grid
int genGrid(int a[9][9], int row, int column)
{
int i, r;
int swap;
int currentRow[size] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int obeysSudokuRules;

if (row == size)
return 1;

for(i = 0; i < size -1; i++)
{
r = i+ (rand() % 8);
swap = currentRow[i];
currentRow[i] = currentRow[r];
currentRow[r] = swap;
}

for(row = 0; row < size; row++)
{
for(column = 0; column < size; column++)
a[row][column] = currentRow[i];

if (obeysSudokuRules == '1')
{
if (column == size -1)
{
obeysSudokuRules = genGrid(a, row + 1, 0);
}
else
{
obeysSudokuRules = genGrid(a, row, column + 1);
}
//if(obeysSudokuRules)
return 1;
}
}

a[row][column] = 0;
return 0;
}

//part 4 = filling zeroes
void fillZeros (int a[9][9])
{
int counter = 0;
int r, random;

for(r = 0; r < size; r++)
{
while (counter <= 4)
{
random = (rand()%8);
a[r][random] = 0;
counter++;
}
}
}

//part 5 = generating partial grid
void genPartialGrid(int a[9][9])
{
int obeysSudokuRules;
int r, c;

do
{
for(r = 0; r < size; r++)
{
for(c = 0; c < size; c++)
{
a[r][c]= 0;
}//fill array with zeros
}
obeysSudokuRules = genGrid(a, 0, 0);
}
while (obeysSudokuRules == 0);

fillZeros(a);
}

//part 6 = determining row
int getRowNum(void)
{
int row;

printf("Please enter the row number that you want to enter the value\n");
scanf("%d", &row);
while ((row < 0) || (row > 8))
{
printf("Please enter the row number that you want to enter the value\n");
scanf("%d", &row);
}
return row;
}

//part 7 = determining column
int getColumnNum(void)
{
int column;

printf("Please enter the column number that you want to enter the value\n");
scanf("%d", &column);
while ((column < 0) || (column > 8))
{
printf("Please enter the column number that you want to enter the value\n");
scanf("%d", &column);
}
return column;
}

//part 8 = receiving value
int getValue(void)
{
int value;

scanf("%d", &value);
return value;
}

int compSolve(int a[][size])
{
int r = 0;
int c = 0;
int checkZero;
while(r < 9)
{
while(c < 9)
{
if((a[r][c]) == 0)
checkZero = 0;
else checkZero = 1;
c++;
}
r++;
}
return checkZero;
}

int main(void)
{
int a[9][9];
int r, c, value, obeysSudokuRules, compSolve;

srand(time(NULL));//randomize random number generation
printf("Welcome to Sudoku++! All numbers in rows and columns must not be repeated");
genPartialGrid(a);//call function genPartialGrid to generate a partially filled grid that does not violate the rules
printGrid(a);//call function printGrid
do
{
r = getRowNum();//call function getRowNum to determine row number
c = getColumnNum();//call function getColumnNum to determine column number
value = getValue();
printGrid(a);//call function to get the value (between 1 & 9)
a[r][c] = value;//enter the value in the grid at the row and column positions entered
//call function printGrid
}
while ((obeysSudokuRules == 1) && (compSolve == 0));

if ((compSolve == 1) && (obeysSudokuRules == 1))
{
printf("Congratulations, you have solved Sudoku++!");
printGrid(a);
}
else//not all Sudoku rules are obeyed
{
}
printf("Thank you for playing!");

return 0;
}```
Thank you for that, I've reduced the number of indentations. However, my main concern with this code is I keep coming up with "Segmentation fault (core dumped)" and I am completely unsure where and what exactly is the problem. This is code compatible for C++.

4. I will preface this with it has been a while since I've programmed C++ so I may make a few mistakes, but I have programmed two sudoku generators one in C++ and one in JavaScript. Hopefully anything I miss-state due to lack of practice one of the more experienced members here will correct.

Firstly I'll start with segmentation fault. Here is a great link to explain what a segmentation fault is. Debugging Segmentation Faults and Pointer Problems - Cprogramming.com.
I know your first response may be annoyed that I posted a link rather than told you what was wrong, but in the long run learning to read errors efficiently is probably one of the greatest skills you can have as a coder.

If you read that link you will realize that this is caused by accessing memory outside of the scope of the program. Since I don't see any pointers it would be logical to assume that you are trying to access your arrays outside of their declared sizes. I didn't immediately spot where, but hopefully this can put you on the right track.

Firstly I like that you have commented your code, keep that up.

Code:
```for (r = 0; r < size; r++)//start from 1st row to check columns
{
for(c = 0; c < size; c++)
{
count[r][c]++;
if ( (count[r][c]) > 1)
return 0;
}
for (i = 0; i < size; i++)
{
count[i][r] = 0;
}// to clear
}//end row loop```
This code looks a bit suspect. Your count[r][c]++ I can only assume you are trying to add 1 to the variable stored in this position in the array. What immediately strikes me is you haven't set any initial value for these array indices. This is inviting undefined behavior.

Next you iterate through the array again, but through the rows as i and using the column as r, and then set those values to 0. You you first added 1 to every column then made every row at the column (value of row) 0. I'm not even sure what you are attempting to do here, but that second for loop with overwrite your changes you are attempting with the first loop.

As I roll through the code I see many similar issues. If I were you I'd start small and create segments of compilable testable code that works and then add to it. Once you have a larger codebase with a bunch of untested functions doing random things it gets really hard to track down bugs as you are seeing.

5. I forgot to update Part 1 so now it is:

Code:
```for (r = 0; r < size; r++)//start from 1st row to check columns
{
for(c = 0; c < size; c++)
{
count[r][c]++;
if ( (count[r][c]) > 1)
return 0;
}
for (i = 0; i < size; i++)
{
count[i][r] = 0;
}// to clear
}//end row loop

for (c = 0; c < size; c++)//start from 1st column to check rows
{
for(r = 0; r < size; r++)
{
count[r][c]++;
if ( (count[r][c]) > 1)
return 0;
}
}//end column loop

r = 0;
c = 0;
for (z = 0; z < size; z++)
{
while (k < 3)
{
while(j < 3)
{
count[r][c]++;
if ( (count[r][c]) > 1)
return 0;
c++;
j++;
}//end of j loop
r++;
k++;
}//end of k loop

if(z < 2)
{
r = 0;
}
if(z == 2)
{
c = 0;
r = 3;
}
if((z > 2) && (z < 5))
{
r = 3;
}
if(z == 5)
{
c = 0;
r = 6;
}
if(z > 5)
{
r = 6;
}
end z loop
return 0;
}
}//end grid loop
```
Also, I sourced the problem of my "Segmentation Fault" error to:
Code:
```for(i = 0; i < size -1; i++)
{
r = i+ (rand() % 8);
swap = currentRow[i];
currentRow[i] = currentRow[r];
currentRow[r] = swap;
}

for(row = 0; row < size; row++)
{
for(column = 0; column < size; column++)
a[row][column] = currentRow[i];

if (obeysSudokuRules == '1')
{
if (column == size -1)
{
obeysSudokuRules = genGrid(a, row + 1, 0);
}
else
{
obeysSudokuRules = genGrid(a, row, column + 1);
}
//if(obeysSudokuRules)
return 1;
}```
What do you think is the exactly problem with these parts, specifically?

6. I'd say you left out a few important pieces of code.

Code:
```int currentRow[size] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int obeysSudokuRules;

if (row == size)
return 1;

for(i = 0; i < size -1; i++)
{
r = i+ (rand() % 8);
swap = currentRow[i];
currentRow[i] = currentRow[r];
currentRow[r] = swap;
}```
What is the length of current row?
I see 9.

How large can r end up being?
I think 14.

Honestly though if you are trying to generate a sudoku puzzle its alot easier to just shuffle an existing puzzle you can actually get alot of variations from this alone.

7. Originally Posted by Lesshardtofind
I'd say you left out a few important pieces of code.

Code:
```int currentRow[size] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int obeysSudokuRules;

if (row == size)
return 1;

for(i = 0; i < size -1; i++)
{
r = i+ (rand() % 8);
swap = currentRow[i];
currentRow[i] = currentRow[r];
currentRow[r] = swap;
}```
What is the length of current row?
I see 9.

How large can r end up being?
I think 14.

Honestly though if you are trying to generate a sudoku puzzle its alot easier to just shuffle an existing puzzle you can actually get alot of variations from this alone.

The row size, as well as column size, is 9, you are correct. I would use an existing puzzle but the difficult aspect is that the grid has to randomize and create a brand new different grid for every new game. I know my problem is with row, column, and size, but idk how best to go at it.

8. The row size, as well as column size, is 9, you are correct. I would use an existing puzzle but the difficult aspect is that the grid has to randomize and create a brand new different grid for every new game. I know my problem is with row, column, and size, but idk how best to go at it.
Well since the size is 9 and i can end up being 7 by the end of the loop and you randomly generate a number between 0-7 and then add to it. Well worst case scenario you just tried to access the 14th index of an array with only 9 spaces in memory. This ends up being the seg fault.

Think about what you are trying to do here. Maybe the size of something that you need to generate numbers off of can change as you remove entries from it. Then you can randomly generate your range based on the length of the shrinking array as you build out a row.