i'm a beginner in c++ i want to programming sudoku i start with a
9x9 table but i had difficulties to continue
1)how to make a function to check numb in the row,colones,and 3x3
2)a counter to know when the game end or repeat until there is no values

``````#include "suduko.h"
#include <iostream.h>

void main()
{
int i,j ;
int t[9][9] ;
for(i=0;i<9;i++)
for(j=0;j<9;j++)
{
t[i][j]=0;
t[0][0]=5;
t[2][0]=1;
t[4][0]=4;
t[5][0]=2;
t[3][1]=9;
t[5][1]=7;
t[1][2]=9;
t[2][2]=7;
t[8][2]=1;
t[1][3]=4;
t[5][3]=1;
t[6][3]=2;
t[7][3]=3;
t[2][4]=9;
t[6][4]=6;
t[1][5]=5;
t[2][5]=2;
t[3][5]=6;
t[7][5]=1;
t[0][6]=2;
t[6][6]=3;
t[7][6]=9;
t[3][7]=8;
t[5][7]=3;
t[3][8]=4;
t[4][8]=5;
t[6][8]=8;
t[8][8]=2;
}
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
cout<<t[i][j]<<" ";
cout<<endl;

}

}
and this is the function that i tried
bool val (int t [][9])
{
int i,j;
bool comp;
comp= true;
for (int a=0;a<9;a++)
if(a!=j && t[i][a]=t[i][j])
comp false;
for(int b=0;b<9;b++)
if(b!=i && t[b][j]==t[i][j])
comp false;
for( b=(i/3)*3;b<(i/3)*3+3;b++)
for(int col=(j/3)*3;col<(j/3)*3+3;col++)
if(b!=i&&col!=j && t[b][col]==t[i][j])
comp=false;
for( i=0;i<9;i++)
for( j=0;j<9;j++)
if(t[i][j]!=0)
comp=false;
for( i=0;i<9;i++)
for(int j=0;j<9;j++)
if ( t[i][j]<0 || t[i][j]>9 )
comp=false;
return comp;

}``````

lines 13-40 do the same thing over and over for 81 times!:icon_eek: What's the point of doing that?

delete line 12 because lines 13-40 initialize the array with other numbers.

lines 61 and 64: what is comp false; ? There is no such thing as comp

This code is unintelligible.

I can't quite tell with the current formatting, but is that supposed to be an 8 - levelled for loop? Rewrite your code, using `int main( void )` instead of `void main()` , and make sure it has a decent structure.

Initialize …

## All 5 Replies

lines 13-40 do the same thing over and over for 81 times!:icon_eek: What's the point of doing that?

delete line 12 because lines 13-40 initialize the array with other numbers.

lines 61 and 64: what is comp false; ? There is no such thing as comp

This code is unintelligible.

I can't quite tell with the current formatting, but is that supposed to be an 8 - levelled for loop? Rewrite your code, using `int main( void )` instead of `void main()` , and make sure it has a decent structure.

``````int sudoku[9][9] = {
{0,0,0, 0,0,0, 0,0,0},
{0,0,0, 0,0,0, 0,0,0},
{0,0,0, 0,0,0, 0,0,0},

{0,0,0, 0,0,0, 0,0,0},
{0,0,0, 0,0,0, 0,0,0},
{0,0,0, 0,0,0, 0,0,0},

{0,0,0, 0,0,0, 0,0,0},
{0,0,0, 0,0,0, 0,0,0},
{0,0,0, 0,0,0, 0,0,0}
};``````

Do that, then I will help more.

``````int sudoku[9][9] = {
{0,0,0, 0,0,0, 0,0,0},
{0,0,0, 0,0,0, 0,0,0},
{0,0,0, 0,0,0, 0,0,0},

{0,0,0, 0,0,0, 0,0,0},
{0,0,0, 0,0,0, 0,0,0},
{0,0,0, 0,0,0, 0,0,0},

{0,0,0, 0,0,0, 0,0,0},
{0,0,0, 0,0,0, 0,0,0},
{0,0,0, 0,0,0, 0,0,0}
};``````

Good grief! initialize the array like this: `int sudoku[9][9] = {0};`

commented: Haha :) +18

Good grief! initialize the array like this: `int sudoku[9][9] = {0};`

That's not what I meant :P
He could replace some of the 0s with numbers of his choice, instead of entering them in one by one on each line.

thnx:D
i tried another functions to check 3x3 grids ,rows and colones what you think about it

``````bool Valid_Set(int line[9])
{
bool found = true;
for (int t = 1; (t <= 9 && found); ++t)
{
found = false;
for (int i = 0; i < 9; ++i)
if (line[i] == t) found = true;
}
return found; // returns true if all value 1-9 are in array.
}

bool Valid_Matrix(int sud[9][9])
{
int i, j, check[9];
bool valid = true;

// check each row
for (j = 0; (j < 9) && valid; ++j)
{
for (i = 0; i < 9; ++i)
check[i] = sud[i][j];
valid = Valid_Set(check[i]);
}
// check each column
for (j = 0; (j < 9) && valid; ++j)
{
for (i = 0; i < 9; ++i)
check[i] = sud[j][i];
valid = Valid_Set(check[i]);
}
// check 3x3 area
for (i = 0; (i < 9) && valid; i += 3)
{
for (j = 0; (j < 9) && valid; j += 3)
{
int t = 0;
for (int x = 0; x < 3; ++x)
for (int y = 0; y < 3; ++y)
check[t++] = sud[x + i][y + j];
valid = Valid_Set(check);
}
}
return valid;
}``````
commented: Don't you learn from your mistakes? -2
Be a part of the DaniWeb community

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