#include<iostream>
#include<fstream>
usingnamespace 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;
}
}
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.
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.
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
1 2 3 4 5 6
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();
#include<iostream>
#include<fstream>
usingnamespace 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;
}
//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;
}
but it doesn't do what I want ,It gives random numbers after the array and then the program stop working .. !!!