Yes, it would have to be passed as a reference. I don't know the syntax for it if I was going to do it that way.
Here's some code. I'm not finished with it yet; I wanted to fix this problem first. Sorry in advance for the length.
I have the outfile declared in the "printSudoku" function right now. But that's what needs to be changed.
Code:
#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
#include <string>
void inFile (int A[9][9]);
void printSudoku (int A[9][9], int row[9], int col[9], int box[3][3]);
int rowCheck(int A[9][9], int row[9]);
int colCheck(int A[9][9], int col[9]);
//int boxCheck(int A[9][9], int box[3][3]);
void main ()
{
int A[9][9], row[9], col[9], box[3][3];
inFile(A);
printSudoku(A, row, col, box);
system("pause");
}
void inFile (int A[9][9])
{
ifstream inData;
inData.open("sudoku.txt");
int i, j;
for (i = 0; i < 9; i++)
for (j = 0; j < 9; j++)
inData>>A[i][j];
}
void printSudoku (int A[9][9], int row[9], int col[9], int box[3][3])
{
int i, j;
ofstream outData;
outData.open("out.txt");
for (i = 0; i < 9; i++)
{
if ((i % 3 == 0) && (i != 0))
{
cout<<endl;
outData<<endl;
}
for (j = 0; j < 9; j++)
{
if ((j % 3 == 0) && (j !=0))
{
cout<<" ";
outData<<" ";
}
cout<<A[i][j]<<" ";
outData<<A[i][j]<<" ";
}
cout<<endl;
outData<<endl;
}
cout<<endl;
outData<<endl;
rowCheck(A, row);
colCheck(A, col);
//boxCheck(A, box);
if ((rowCheck(A, row)) && (colCheck(A, col)) /*&& (boxCheck(A, box))*/)
cout<<"This is a valid sudoku solution!"<<endl<<endl;
}
int rowCheck(int A[9][9], int row[9])
{
int i, j, k, l;
int v[9][10] = {0};
bool present[9][10] = {0};
bool b = true;
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9; j++)
{
row[j] = A[i][j];
for(k=1;k<10;k++)
{
if (row[j] == k)
{
v[i][k]++;
present[i][k]= true;
}
if (v[i][k] > 1)
{
cout<<"Inconsistancy detected on row "<<i+1<<" where the number "<<row[j]<<" is repeated on column "<<j+1<<endl;
outData<<"Inconsistancy detected on row "<<i+1<<" where the number "<<row[j]<<" is repeated on column "<<j+1<<endl;
b = false;
v[i][k] = 0;
}
}
}
for (l = 1; l <= 9; l++)
{
if (present[i][l] == false)
{
cout<<"Inconsistancy detected on row "<<i+1<<" where the following number is missing: "<<l<<endl<<endl;
outData<<"Inconsistancy detected on row "<<i+1<<" where the following number is missing: "<<l<<endl<<endl;
}
}
}
return b;
}
int colCheck(int A[9][9], int col[9])
{
int i, j, k, l;
int v[9][10] = {0};
bool present[9][10] = {0};
bool b = true;
for (i = 0; i < 9; i++)
{
for (j = 0; j < 9; j++)
{
col[j] = A[j][i];
for(k=1;k<10;k++)
{
if (col[j] == k)
{
v[i][k]++;
present[i][k]= true;
}
if (v[i][k] > 1)
{
cout<<"Inconsistancy detected on column "<<i+1<<" where the number "<<col[j]<<" is repeated on row "<<j+1<<endl;
outData<<"Inconsistancy detected on column "<<i+1<<" where the number "<<col[j]<<" is repeated on row "<<j+1<<endl;
b=false;
v[i][k] = 0;
}
}
}
for (l = 1; l <= 9; l++)
{
if (present[i][l] == false)
{
cout<<"Inconsistancy detected on column "<<i+1<<" where the following number is missing: "<<l<<endl<<endl;
outData<<"Inconsistancy detected on column "<<i+1<<" where the following number is missing: "<<l<<endl<<endl;
}
}
}
return b;
}
/*int boxCheck(int A[9][9], int box[3][3])
{
int i, j, k, l;
int v[9][10] = {0};
bool present[9][10] = {0};
bool b = true;
int box1[3][3] = {0};
int box2[3][3] = {0};
int box3[3][3] = {0};
int box4[3][3] = {0};
int box5[3][3] = {0};
int box6[3][3] = {0};
int box7[3][3] = {0};
int box8[3][3] = {0};
int box9[3][3] = {0};
for (i=0; i < 3; i++)
{
for (j=0; j < 3; j++)
{
box1[i][j]=A[i][j];
box2[i][j]=A[i][j+3];
box3[i][j]=A[i][j+6];
box4[i][j]=A[i+3][j];
box5[i][j]=A[i+3][j+3];
box6[i][j]=A[i+3][j+6];
box7[i][j]=A[i+6][j];
box8[i][j]=A[i+6][j+3];
box9[i][j]=A[i+6][j+6];
}
}
return b;
}*/