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:
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;
printf("Please enter the 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("Sorry, your solution is incorrect.");
}
printf("Thank you for playing!");
return 0;
}