checking rows & columns in 2D array!

May 5, 2011 at 3:03am


How to check each row, each column, and each of the nine 3*3 blocks that they contain one of the digits from 1 to 9 like the following board??

it will not be 1-9 , but this is an example :
123456789
123456789
123456789
123456789
123456789
123456789
123456789
123456789
123456789

do I have to use if-statement ??



May 5, 2011 at 3:10am
so, you want to write a sudoku game? you'll probably need an if statement at some point

why not sit down and work on an algorithm and come back when you've got some ideas? or even better, some code
May 5, 2011 at 3:33am
You will not use an if-statement to check values of a 2-D array, you 'll be using for-loops. However, if your array hasn't been initialized, you will see garbage when you run it.

Here is a snippet of code that will display the code (once you have something initialized):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main(void)
{
	auto	int	grid[3][3];
	auto	int	row;
	auto	int	col;

	for (row = 0; row < 3; ++row)
		{
		for (col = 0; col < 3; ++col)
			{
			cout << grid[row][col];
			}
		cout << endl;
		}
}


Now if you compile this code, you will see nothing but numbers until you initialize it yourself. Hope this helps.
Last edited on May 5, 2011 at 3:35am
May 5, 2011 at 3:44am

Yeah I've done with the initializing part and my numbers are stored in 2D array
(but checking is left),
........
What is auto ??
Can I use int only ?
May 5, 2011 at 3:47am
Is this code correct?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Check(int grid[][3],int s)
{
     
	int	grid[3][3];
	int	row;
	int	col;

	for (row = 0; row < 3; ++row)
		{
		for (col = 0; col < 3; ++col)
			{
			cout << grid[row][col] << "  ";
			}
		cout << endl;
		}
}


and Is it checking each row , each column, and each 3*3 block ?
Or Do I have to make another for loop for columns and rows ?
Last edited on May 5, 2011 at 3:49am
May 5, 2011 at 4:07am
Sorry, in my class we use auto. Its deprecated but we use it anyways. But anyways, yes the code you have is right. I'm assuming you have 'int s' initialized but your not using it in the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Check(int grid[][3],int s)
{
     
	int	row;
	int	col;
        int     s = 0;

	for (row = 0; row < 9; ++row)
		{
		for (col = 0; col < 9; ++col)
			{
			grid[row][col] = ++s
                        cout << grid[row][col] << "  ";
			}
		cout << endl;
		}
}


This code will display to your dimensions but you will need more to meet your requirements.
Last edited on May 5, 2011 at 4:15am
May 5, 2011 at 4:42am

sorry ,

s is the size which is 9.
May 5, 2011 at 4:52am

But, I don't want to display the array I've display it already, I want only to check !

is it the same way ?
May 5, 2011 at 7:29am
Ok so what exactly do you need to check it for? If you're checking for a value you can use an if statement inside the second for loop. If the criteria is not met, it will go to the next member. So yes, this code can be used to check each matrix.
May 5, 2011 at 8:49am

I want to make 4 checks :

1 - to check the whole 2D array that all values are between (1-9)no more no less.
2- check each row (contain one of the digits from 1 to 9 ) No repetition.
3- check each column (contain one of the digits from 1 to 9 ) No repetition.
4- check all 3*3 blocks which are 9 they should (contain one of the digits from 1 to 9 ) No repetition.

how to do it ? :!
May 6, 2011 at 5:37am

here is the code to check rows and columns , but It doesn't work it gives random numbers after the array and then the program stop working >> how to solve it ?/:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//a function to check the rows and columns 
void col_row(int sudok[][9],int siz)
{
    bool duplic = false;
	for(int i=0;i<siz;i++)
	{
		for(int j=0;j<siz;j++)
		{
			if(sudok[i]!= sudok[i+1])
				duplic = true;
			else
			{duplic = false;
			break;
			}
		}
		if(duplic == false)
			break;

	}
 if(duplic)
	 cout << "The Numbers are suitable for the game" << endl;
 else
	 cout << "The Numbers are Not good for the game" << endl;
}


..................
Topic archived. No new replies allowed.