check if there are a frequency number in 2D array?

hi

i have a 2D array 9x9 that represent Sudoku
that hold onley a number from 1 to 9 in each cell

how to write one or more functions to check whether the 9x9 board configuration is a successful Sudoku configuration; i.e., each row, each column and each of the nine 3×3 sub-grids contains exactly one of the digits from 1 to 9?
Last edited on
Run a loop for 9*9 times, i.e. for each row/column and check if that row/column has all the digits, no repeats.

To check that each row has all the digits from 1-9, what you can do is take a temporary array,

short tempArr[9];

now for each digit in that row, put that digit in this array on the same index according to the value of the digit. so lets say you have this matrix:

short matrix[9][9];

and lets say you are running a loop which is on the first row and first column. you will do this:

tempArr[matrix[0][0]] = matrix[0][0]; //if matrix[0][0] has value 5, than tempArr's fifth index will have value 5

this is going to fill the tempArrperfectly with no empty. If this happens, you can be sure that that, that row has all the digits.

hope this will work for you. Although there can be more solutions.
Last edited on
thanks pro for benig with me..

i dont mean that is there any empty index in tha array
i mean is there any number that is occurs more then onse in the row or column?

for example lets take the first row
ande assume that we have like this :
5 3 4 6 7 8 9 1 1

the progrene should display an error message!! becouse we have 1 twice!

but if we had the fisrt row like this
5 3 4 6 7 8 9 1 2

it will be successful configuration !
I would suggest using an array of 9 booleans that will account for the values present in the column / row you're checking.

1
2
3
4
5
6
7
int matrix[9][9];
bool appeared[9]; // initialize to false
// and inside your loop...
if (appeared[matrix[x][y]])
	// (x,y) has a duplicated value.
else
	appeared[matrix[x][y]] = true;
Exactly this is what I tried to explain in my last post. Try to write some code on that logic and post it and we can help you to correct it.

dear writetonsharma..

maybe i didn't understand ur suggest solution clearly

but i was thinking of the same method that Zeillinger has wrote it

Zeillinger could you explain why you initialize tha array to false?

and why you didn't write anything in the true area of if statment ?
zz77z wrote:
Zeillinger could you explain why you initialize tha array to false?

Before going over the elements in the column / row they haven't actually appeared, only when they're spotted in the column / row you can mark them (by setting the value of appeared[number] to be true if you encountered number).

zz77z wrote:
and why you didn't write anything in the true area of if statment?

I left this for you since you haven't specified what you want to do if duplicated values are found.
Last edited on
i will back to complete our discussion tommorow because i have an exame in c++
zeilinger what do mean by dulicate value to x and y you mean we assign same value to x and y
e.g x=1,y=1.
good job Zeillinger

so i will need two loops one for rows and one for column is that right?
zeilinger what do mean by dulicate value to x and y you mean we assign same value to x and y
e.g x=1,y=1.


tahir89,, try to read my question to understand his meaning
i have a problem

if i tried to check this row :
5 3 4 6 7 8 9 1 2

and i've initilized all tha value of bool appeared[9]; to false

it will apear to true when read the digit 9 !!

why ??

this was my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/************ start of checks functions ************/

int rowCheck (const int matrix[][9],bool check[], int& ret) 
{
	for (int i=0;i<9;i++)
		check[i]= false; //initilizing tha value to false

	for (int col=0 ;col<9;col++)
	{

		for (int row =0; row<9 ; row++)
		{
		if (check[matrix[row][col]])
		{
			cout<<row<<col <<"Suducu configuration unsuccessful.!\n";
				ret=1;
				return ret;
		}
	
		else
			check[matrix[row][col]] = true;
		}
		for (int i=0;i<9;i++)
		check[i]= false; //reinitilizing tha value to false
	}

				return ret;
	
}
Topic archived. No new replies allowed.