# Sudoku row & column checker. Help please.

Page 1 of 1

## 14 Replies - 1218 Views - Last Post: 22 July 2015 - 05:31 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=378923&amp;s=26f4f0b5f42ba4b4d243e5d5283ba3a6&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

Reputation: 2
• Posts: 59
• Joined: 04-June 15

# Sudoku row & column checker. Help please.

Posted 17 July 2015 - 08:51 PM

Hello, so i have been making a console sudoku game, I've got everything done except for the double checking functions.
I am having trouble with it, the column double checker works fine at the moment but the row double checker doesn't.

when i run the program and use the double checker for the rows and columns it comes up with
NumOfDoubles_Row = 8
NumOfDoubles_Col = 0

but there shouldn't be 8 doubles in the rows because no numbers have been added yet.

The functions are below:
```bool DoublesIn_Row(int SNum[][9], int iS, int &NumOfDoubles_Row)
{
int RowCheckVar = 0;
NumOfDoubles_Row = 0;

for(int row = 0; row < iS; row++)
{
for(int col = 0; col < iS; col++)
{
RowCheckVar = SNum[row][col];
// assigns the value in SNum[x][y] to the check Variable
IsValid_Row(SNum, iS, NumOfDoubles_Row, RowCheckVar, row, col);
}
}

SetCursorPosition(0, 24);
std::cout << " No.Doubles: " << NumOfDoubles_Row << std::endl;
// only here for testing purposes
if(NumOfDoubles_Row != 0) return true; // i realize i probably dont need to have this in here as IsValid returns a boolean but ill fix that later
else return false;
}

bool IsValid_Row(int SNum[][9], int iS, int &NumOfDoubles_Row, int RowCheckVar, int row, int col )
{
for(int n = 1; n < iS - 1; n++)
{
// n is used to iterate over the numbers in a row eg. col = 0, 0 + 1, 0 + 2, 0 + 3. etc.
if(RowCheckVar == SNum[row][col + n] && RowCheckVar != 0) NumOfDoubles_Row++;
// if check variable is equal to number at SNum[x][y] and the variable is not equal to 0 then add 1 to the number of doubles
}

if(NumOfDoubles_Row == 0) return true;
// return true if there are no doubles in the rows else return false
else return false;
}

bool DoublesIn_Col(int SNum[][9], int iS, int &NumOfDoubles_Col)
{
int ColCheckVar = 0;
NumOfDoubles_Col = 0;

for(int row = 0; row < iS; row++)
{
for(int col = 0; col < iS; col++)
{
ColCheckVar = SNum[col][row];
// assigns the value in SNum[x][y] to the check Variable
IsValid_Col(SNum, iS, NumOfDoubles_Col, ColCheckVar, row, col);
}
}

SetCursorPosition(0, 25);
std::cout << " No.Doubles: " << NumOfDoubles_Col << std::endl;
// only here for testing purposes
if(NumOfDoubles_Col != 0) return true; // i realize i probably dont need to have this in here as IsValid returns a boolean but ill fix that later
else return false;
}

bool IsValid_Col(int SNum[][9], int iS, int &NumOfDoubles_Col, int ColCheckVar, int row, int col)
{
for(int n = 1; n < iS - 1; n++)
{
// n is used to iterate over the numbers in a col eg. col = 0, 0 + 1, 0 + 2, 0 + 3. etc.
if(ColCheckVar == SNum[col + n][row] && ColCheckVar != 0) NumOfDoubles_Col++;
// if check variable is equal to number at SNum[x][y] and the variable is not equal to 0 then add 1 to the number of doubles
}

if(NumOfDoubles_Col == 0) return true;
// returns true if there are no doubles in the columns else return false
else return false;
}

```

i have been stuck on this for about a week and its the last thing i need to do for the sudoku program i have tried to find a solution on my own but i just cant figure it out />

any help would be greatly appreciated.

This post has been edited by Shadow-coder: 17 July 2015 - 08:53 PM

Is This A Good Question/Topic? 0

## Replies To: Sudoku row & column checker. Help please.

### #2 Xupicor

• Nasal Demon

Reputation: 457
• Posts: 1,179
• Joined: 31-May 11

## Re: Sudoku row & column checker. Help please.

Posted 17 July 2015 - 09:21 PM

So what is int iS for in DoublesIn_Row()? Also, you have RowCheckVar = SNum[row][col]; there, but ColCheckVar = SNum[col][row]; in your analogous DoublesIn_Col() - notice row and col switched places - is that how it should be? I'm merely asking, I didn't quite make heads or tails out of that code yet.

You might want to reduce the code sample to something that will compile and run, but still have the problem in. If it doesn't help you spot the problem (which it often does), we might have an easier job to spotting it on our end.

This post has been edited by Xupicor: 17 July 2015 - 09:23 PM

### #3 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: Sudoku row & column checker. Help please.

Posted 18 July 2015 - 06:51 PM

Hi, you pass col to isValid_Row, col can be a value 0 to size-1. In the function you then add n to the col and use as a subscript, so you can see the subscript can become out of bounds.

You could print the value to see this.

You want to iterate along the row, but skip the passed col.

The name hasRow_Doubled might be a more decriptive name for the function.

Reputation: 2
• Posts: 59
• Joined: 04-June 15

## Re: Sudoku row & column checker. Help please.

Posted 21 July 2015 - 07:40 PM

okay so my code now looks like this
```bool DoublesIn_Row(int SNum[][9], int iS, int &NumOfDoubles_Row)
{
int RowCheckVar = 0;
NumOfDoubles_Row = 0;

for(int row = 0; row < iS; row++)
{
for(int col = 0; col < iS; col++)
{
RowCheckVar = SNum[row][col];
// assigns the value in SNum[x][y] to the check Variable
IsValid_Row(SNum, iS, NumOfDoubles_Row, RowCheckVar, row, col);
}
}

SetCursorPosition(0, 24);
std::cout << " No.Doubles: " << NumOfDoubles_Row << std::endl;
if(NumOfDoubles_Row != 0) return true;
else return false;
}

void IsValid_Row(int SNum[][9], int iS, int &NumOfDoubles_Row, int RowCheckVar, int row, int col1)
{

for(int n = 0; n < iS; n++)
{
int col2 = 0;
// n is used to iterate over the numbers in a row eg. col = 0, 0 + 1, 0 + 2, 0 + 3. etc.
if(RowCheckVar == SNum[row][col2 + n] && RowCheckVar != 0 && col2 + n != col1) NumOfDoubles_Row++;
// if check variable is equal to number at SNum[x][y] and the variable is not equal to 0 then add 1 to the number of doubles
}
}

bool DoublesIn_Col(int SNum[][9], int iS, int &NumOfDoubles_Col)
{
int ColCheckVar = 0;
NumOfDoubles_Col = 0;

for(int col = 0; col < iS; col++)
{
for(int row = 0; row < iS; row++)
{
ColCheckVar = SNum[row][col];
// assigns the value in SNum[x][y] to the check Variable
IsValid_Col(SNum, iS, NumOfDoubles_Col, ColCheckVar, row, col);
}
}

SetCursorPosition(0, 25);
std::cout << " No.Doubles: " << NumOfDoubles_Col << std::endl;
if(NumOfDoubles_Col != 0) return true;
else return false;
}

void IsValid_Col(int SNum[][9], int iS, int &NumOfDoubles_Col, int ColCheckVar, int row1, int col)
{

for(int n = 0; n < iS; n++)
{
int row2 = 0;
// n is used to iterate over the numbers in a col eg. col = 0, 0 + 1, 0 + 2, 0 + 3. etc.
if(ColCheckVar == SNum[row2 + n][col] && ColCheckVar != 0 && row2 + n != row1) NumOfDoubles_Col++;
// if check variable is equal to number at SNum[x][y] and the variable is not equal to 0 then add 1 to the number of doubles
}
}

```

now, if i change the value of n to 0 the results for number of doubles becomes:
(1 double in row) = NumOfDoubles = 2
(2 doubles in row) = NumOfDoubles = 4
(3 Doubles in row) = NumOfDoubles = 6

i dont know why its doubling the number /> if i change n to 1 then the results are:
(1 double in row) = NumOfDoubles = 1
(2 double in row) = NumOfDoubles = 2
(3 double in row) = NumOfDoubles = 4

i dont understand why it's doing this />

This post has been edited by Shadow-coder: 21 July 2015 - 07:40 PM

### #5 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: Sudoku row & column checker. Help please.

Posted 21 July 2015 - 07:56 PM

Hi, if you have a two numbers in a row, you will count the first and then the second. With three you will count the first as two, the second as two and the third as two.

Although that doesn't quite appear to be the same as your output. You could print out the row and column numbers to check.

Reputation: 2
• Posts: 59
• Joined: 04-June 15

## Re: Sudoku row & column checker. Help please.

Posted 21 July 2015 - 08:04 PM

oh wow.. i really should have realized that <.< i feel dumb now haha, thank you i just have to divide the number of doubles by 2 and then it gives me what i want, i think why it was having a weird output if i changed n to 1 is that it wouldnt iterate over every number of the array since it would go to 7 and then not do another loop, you were right though it was counting each number into the number of doubles variable so i just divide it by 2 so it just says that there are 3 "lots" of doubles.

now i just have to work out a good way to check the boxes for doubles, i was thinking of a function where before i call the function i assign the numbers from 1 of the 9 boxes into a separate array then passing it to the function and check it that way.

so should i have 9 arrays for the boxes and then assign the numbers from those to a checking array or just assign the numbers straight from the main sudoku puzzle array to the checking array?

### #7 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: Sudoku row & column checker. Help please.

Posted 21 July 2015 - 08:12 PM

I wonder, if you have four numbers the same in a row, then you will have a count of three for each, giving a total of twelve.

Reputation: 2
• Posts: 59
• Joined: 04-June 15

## Re: Sudoku row & column checker. Help please.

Posted 21 July 2015 - 08:20 PM

hmm, realistically no one would put 4 of the same number in a sudoku row or collumn but how could i work around that

### #9 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: Sudoku row & column checker. Help please.

Posted 21 July 2015 - 08:32 PM

Perhaps, your idea of another array but with a twist.

If you have another array each element of which represents a count of an input value eg 1 to 9.

Before reading row data the array is zeroed.
Then when you check the row the array is updated, eg when a 1 is found the count at the first position is incremented.
Then you can iterate along the count array and count/sum values that are greater than 1.

### #10 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: Sudoku row & column checker. Help please.

Posted 21 July 2015 - 08:39 PM

Do you need a count?

Presumably you would test, when a number is inputted, whether it would double with another number on the row, column or box. Then the input could be disallowed. In that case, all you would need is a boolean value.

Reputation: 2
• Posts: 59
• Joined: 04-June 15

## Re: Sudoku row & column checker. Help please.

Posted 21 July 2015 - 08:46 PM

i guess i dont really need a count for the amount of doubles there are, but i dont really like the idea of not allowing the player to place a number that would double up into the grid, it makes the game a bit easier, i'm more or less using the count to determine whether the function returns true or not but i could easily just put that within the if statement where it tests the variable i guess that would make the code a bit simpler and easier to read as well.

### #12 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: Sudoku row & column checker. Help please.

Posted 21 July 2015 - 09:28 PM

No, you don't need to disallow input.

### #13 #define

• Duke of Err

Reputation: 1853
• Posts: 6,671
• Joined: 19-February 09

## Re: Sudoku row & column checker. Help please.

Posted 21 July 2015 - 09:45 PM

So perhaps your function just needs to test whether there is one or more doubles, or none, and you don't need to be concerned about a count.

Reputation: 2
• Posts: 59
• Joined: 04-June 15

## Re: Sudoku row & column checker. Help please.

Posted 21 July 2015 - 10:58 PM

yeah i just changed my functions so it doesn't matter about the number of doubles and just matters if there is 1 double within the grid

### #15 baavgai

• Dreaming Coder

Reputation: 7113
• Posts: 14,830
• Joined: 16-October 07

## Re: Sudoku row & column checker. Help please.

Posted 22 July 2015 - 05:31 AM

Ok, now that you're starting to rethink this...

Create an array of bool with 9 slots, call it found. Init to false, because it's empty. Scan through your 9 values, be it row or col or 3x3. For each value you find, look at found[value] ( or found[value-1] .) If the value is false, great, make it true. If the value is already true, you have a duplicate, return an error.

Note, also, that if all slots in your found array are true without duplicate, you not only have a valid selection but a solved one.

Hope this helps.