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>
usingnamespace std;
int main(void)
{
autoint grid[3][3];
autoint row;
autoint 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.
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.
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.
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.
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 ?/:
//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;
}