Hello everyone,

First, please excuse the sloppy-ish coding. I'm working on my ability to shorten my code as we speak. However, being new to this, the hardest way seems to be what I always end up doing.

That said - on to my problem.

I wrote this code for a programming class of mine. While the code does execute properly (from what I can tell, with limited testing), I receive a weird error on the output.

No matter how I try executing the checks, the first error found on the row will output after both the columns and the 3x3 grid. All subsequent errors will output in the correct spot however.

The output should look like:

row error
row error
column error
column error
3x3 grid error
3x3 grid error

However, it appears as:

row error
column error
column error
3x3 grid error
3x3 grid error
row error

The purpose of this code is to verify a sudoku solution. Any errors will be reported with their block, line, and column (the first instance of the number will not be reported, only the extras that follow). There is a lot of output right now which is for debugging purposes.

In the code below, I have included a sudoku puzzle with 2 errors. Row 1, Column 1 should be a 7, not a 5, and Row 2, Column 1 should be a 4, not a 5.

The code is ready to be run/complied, so there should be no modifications necessary in order to reproduce the error I keep seeing.

Any help on this would be greatly appreciated!

Thanks in advance.

#include <iostream>
#include <iomanip>

using namespace std;

const int grid = 9;
const int block = 3;

void fRef();
void chkMat();
void chkRow(int x);
void chkCol(int x);
void chkBlk(int x);
void chkMsng(int x, int y, int z);
void fixRef();
void error(const int x, const int y, const int r);
void printGrid(const int x[][grid]);

int refMat[grid][grid] = {0};
int matrix[grid][grid] = {5,9,2,6,8,4,5,1,3,5,5,1,9,7,3,8,2,6,6,8,3,5,2,1,7,4,9,5,2,9,7,4,8,3,6,1,1,6,7,2,3,9,4,8,5,8,3,4,1,5,6,2,9,7,3,
								4,6,8,9,7,1,5,2,9,7,5,4,1,2,6,3,8,2,1,8,3,6,5,9,7,4};

int main()
{

	fRef();
	chkMat();
	printGrid(matrix);

	system("pause"); return 0;
}
void fRef()
{
	for (int i = 0; i < grid; i++)
		refMat[i][0] = i+1;
}
void chkMat()
{
	for (int i = 0; i < grid; i++)
	{
		chkRow(i+1);
		chkCol(i+1);
		chkBlk(i+1);
	}
}
void chkRow(int x)
{
	int tmp;
	for (int i = 0; i < grid; i++)
	{
		tmp=matrix[x-1][i];
		refMat[tmp-1][1]++;
		if (refMat[tmp-1][1] > 1)
			error(i,(x-1),tmp);
	}	
	for (int j = 0; j < grid; j++)
		if (refMat[j][1] == 0)
			chkMsng(j+1, x, 1);
	fixRef();
}
void chkCol(int x)
{
	int tmp;
	for (int i = 0; i < grid; i++)
	{
		tmp=matrix[i][x-1];
		refMat[tmp-1][1]++;
		if (refMat[tmp-1][1] > 1)
			error((x-1),i,tmp);
	}
	for (int j = 0; j < grid; j++)
		if (refMat[j][1] == 0)
			chkMsng(j+1, x,  2);
	fixRef();
}
void chkBlk(int x)
{
	int mOf, nOf;
	switch(x)
	{		
	case 1: 
		mOf = 0;
		nOf = 0;
		break;
	case 2:
		mOf = 0;
		nOf = 3;
		break;
	case 3:
		mOf = 0;
		nOf = 6;
		break;
	case 4: 
		mOf = 3;
		nOf = 0;
		break;
	case 5:
		mOf = 3;
		nOf = 3;
		break;
	case 6:	
		mOf = 3;
		nOf = 6;
		break;
	case 7: 
		mOf = 6;
		nOf = 0;			
		break;
	case 8:
		mOf = 6;
		nOf = 3;
		break;
	case 9:
		mOf = 6;
		nOf = 6;
		break;
	}

	
	int blk[block][block] = {0};

	for (int m = 0; m < block; m++)
		for (int n = 0; n < block; n++)
			blk[m][n] = matrix[m+mOf][n+nOf];

	int tmp;
	for (int i = 0; i < block; i++)
		for (int j = 0; j < block; j++)
		{
			tmp=blk[i][j];
			refMat[tmp-1][1]++;
			if (refMat[tmp-1][1] > 1)
				error((i+mOf),(j+nOf),tmp);
		}
	for (int j = 0; j < grid; j++)
		if (refMat[j][1] == 0)
			chkMsng(j+1, x, 3);
	fixRef();
}
void chkMsng(int x, int y, int z)
{
	if (z == 1)
		cout<<"Row "<<y<<" is missing a "<<x<<endl;
	else if (z == 2)
		cout<<"Column "<<y<<" is missing a "<<x<<endl;
	else if (z == 3)
		cout<<"3x3 grid number "<<y<<" is missing a "<<x<<endl;
}
void fixRef()
{
	for (int i = 0; i < grid; i++)
		refMat[i][1] = 0;
}
void error(const int x, const int y, const int r)
{
	int blk;
	if ((y == 0 || y == 1 || y == 2 ) && ( x == 0 || x == 1 || x == 2))
		blk = 1;
	else if ((y == 0 || y == 1 || y == 2 ) && ( x == 3 || x == 4 || x == 5))
		blk = 2;
	else if ((y == 0 || y == 1 || y == 2 ) && ( x == 6 || x == 7 || x == 8))
		blk = 3;
	else if ((y == 3 || y == 4 || y == 5 ) && ( x == 0 || x == 1 || x == 2))
		blk = 4;
	else if ((y == 3 || y == 4 || y == 5 ) && ( x == 3 || x == 4 || x == 5))
		blk = 5;
	else if ((y == 3 || y == 4 || y == 5 ) && ( x == 6 || x == 7 || x == 8))
		blk = 6;
	else if ((y == 6 || y == 7 || y == 8 ) && ( x == 0 || x == 1 || x == 2))
		blk = 7;
	else if ((y == 6 || y == 7 || y == 8 ) && ( x == 3 || x == 4 || x == 5))
		blk = 8;
	else if ((y == 6 || y == 7 || y == 8 ) && ( x == 6 || x == 7 || x == 8))
		blk = 9;

	cout<<"Multiple instances of "<<r<<" found in block "<<blk<<". Overall grid location ("<<x+1<<", "<<y+1<<")"<<endl;
}
void printGrid(const int x[][grid])
{
	int iOf = 0, jOf = 0;
	int nI = 0, nJ = 0;
	int i = -1;
	for (int m = 0; m < 3; m++)
	{
		for (int n = 0; n < grid; n++)
		{
			switch(n)
			{
			case 0: 
				i++;
				jOf = 0;
				break;
			case 1:
				jOf = 3;
				break;
			case 2:
				jOf = 6;
				break;
			case 3: 
				cout<<endl<<endl;
				i++;
				jOf = 0;
				break;
			case 4:
				jOf = 3;
				break;
			case 5:	
				jOf = 6;
				break;
			case 6: 
				cout<<endl<<endl;
				i++;
				jOf = 0;			
				break;
			case 7:
				jOf = 3;
				break;
			case 8:
				jOf = 6;
				break;
			}
			for (int j = 0; j < 3; j++)
				{
				nJ = (j + jOf);
				cout<<setw(2)<<x[i][nJ]<<" ";
				}
			cout<<"  ";
		}
		cout<<endl<<endl<<endl;
	}
}

Recommended Answers

All 2 Replies

In case anyone actually followed this, I ended up redoing the code completely. It's longer, but more streamlined. Again, I need lessons in refining my code; however, I'm pretty happy with the result.

I'll post the code if anyone is interested. Otherwise, I'll save the Daniweb DB some space.

In case anyone actually followed this, I ended up redoing the code completely. It's longer, but more streamlined. Again, I need lessons in refining my code; however, I'm pretty happy with the result.

I'll post the code if anyone is interested. Otherwise, I'll save the Daniweb DB some space.

Thank you for the follow up. You could post your code here, I'm sure people will have a look at it. Perhaps someone can give you some tips on 'streamlining' your code :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.