### two dimensional arrays!

Hi,

if I have numbers in 9X9 board/grid ,
the choice is to user whether to bring the numbers from a file or enter his numbers.

Then the program should display the 9x9 board as a two dimensional board.

how to do this part?
That could be done like this:
 ``1234567891011121314151617181920212223242526272829303132333435`` ``````#include #include using namespace std; int input(istream& in=cin) { int x; in >> x; return x; } int main() { int board[9][9]; //creates a 9*9 matrix or a 2d array. for(int i=0; i<9; i++) //This loops on the rows. { for(int j=0; j<9; j++) //This loops on the columns { board[i][j] = input(); //you can also connect to the file //and place the name of your ifstream in the input after opening the file will //let you read from the file. } } for(int i=0; i<9; i++) //This loops on the rows. { for(int j=0; j<9; j++) //This loops on the columns { cout << board[i][j] << " "; } cout << endl; } }``````

Input example:
 `1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9`

Output example:
 ```1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 ```

OK if the file have the numbers as your output example order or the user entered them in that order, I mean (9 lines each line have 9 values)

then will the two dimensional array be useless ?

The user entered 81 elements I stored them in a 2d array board[9][9] then I output first row then a new line then second row and then a new line and so on.
Look again here:
 ``12345678`` ``````for(int i=0; i<9; i++) //This loops on the rows. { for(int j=0; j<9; j++) //This loops on the columns { cout << board[i][j] << " "; } cout << endl; }``````

After the elements have been stored the two for loops in the first iteration will out put the element board[0][0] which is first then increment the j so it will output board[0][1] and so on till it reaches the end if 1st row in the 2d array then I print a new line then increment the i to output board[1][0] (1st element in the 2nd row) the increment the j to reach the end of the second row and so in

yeah but what I mean is that when I ask the user to enter 9x9 numbers (9 lines each line contains 9 numbers) from the beginning.

like this problem:
Write a C++ program that reads numbers into a 9x9 board/grid. The program should give the user the option to get the numbers from a file (and give the file name to the program) or via the key board. The input file should have the numbers stored in 9x9 grid (9 lines with 9 values on each line).

The program should check if any number entered is not suitable for the game (numbers which are allowed are integers from 1 to 9).

The program should then display the 9x9 board as a two dimensional board.

The program should 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. The following is an example of a successful Sudoku configuration.

Last edited on
Put this code in your IDE and input this
 ```1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9```

Will also work and pit it in a file connect ifstream to that file and put the name of the stream in the function input then close the file at the end of the program will also work!
@Thuraya If you use `infile >> number;` Shouldn't matter which way you enter it in the file. Whether it is in a single line or structured like the output.
Yes this is correct but before calling the finction input make sure that you have this in your code
 ``123456`` ``````ifstream fin; fin.open("input.txt"); //Or any [code]name your file has. input(fin); //After finishing with the function call don't forget to close the file fin.close();``````

Also don't forget to `#include<fstream> `

Thanks I've done every thing ,
,
One more lil Question,

How Can I check 2D array ?

Here e.g :

if (board[9][9]>=1 && board[9][9]<=9)

cout << "The Numbers are Suitable" << endl;

else

cout << "The Numbers are NOT suitable" << endl;

I did this but it always give me the second answer , !!!! But my numbers are all from 1-9

You have to place your conditionals in a nested for loop.
 ``12345678910`` ``````for(int i = 0; i < 9; i++) { for(int j = 0; j < 9; j++) { if (board[i][j]>=1 && board[i][j]<=9) cout << "The Numbers are Suitable" << endl; else cout << "The Numbers are NOT suitable" << endl; } }``````

But , I don't want the sentence to be repeated !!
BTW, it gives me the correct checking but ,(repeating thing is left)!

how to do it ?
This can easily be done by:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` ``````#include #include using namespace std; int input(istream& in=cin) { int x; in >> x; return x; } int main() { int board[9][9]; //creates a 9*9 matrix or a 2d array. ///////////////////////////////////////////////////////////////// //Here is a new part. ///////////////////////////////////////////////////////////////// //New boolean expression intialized to false. bool isSuitable = false; for(int i=0; i<9; i++) //This loops on the rows. { for(int j=0; j<9; j++) //This loops on the columns { board[i][j] = input(); //you can also connect to the file //and place the name of your ifstream in the input after opening the file will //let you read from the file. } } ///////////////////////////////////////////////////////////////// //Here is a new part. ///////////////////////////////////////////////////////////////// for(int i=0; i<9; i++) //This loops on the rows. { for(int j=0; j<9; j++) //This loops on the columns { if(board[i][j]>=1 && board[i][j]<=9) isSuitable = true; else { isSuitable = false; break; } } if(isSuitable == false) break; } for(int i=0; i<9; i++) //This loops on the rows. { for(int j=0; j<9; j++) //This loops on the columns { cout << board[i][j] << " "; } cout << endl; } //////////////////////////////////////////////////////////////// //Here is also a new part. //////////////////////////////////////////////////////////////// if (isSuitable) cout << "The Numbers are Suitable" << endl; else cout << "The Numbers are NOT suitable" << endl; }``````

many errors appear !!

Sorry add a `return 0;` at line 74 before the closing brace and if you use DEV CPP add a system("PAUSE"); before the return 0;
 ``123456789101112131415161718192021222324`` ``````//a function to check the rows and columns void col_row(int sudok[][9],int siz) { bool duplic = false; for(int i=0;i