How do i code to check each line individually in a 2d array?

essentially i'm working on a project to make a sudoku game and then check for errors. I'm at the final steps and can't figure out how to do two things

1) How would I write a piece of code to check each row (or each column) to equal 45?
for example, my sudoku example is below:

1
2
3
4
5
6
7
8
9
4  3  5  2  6  9  7  8  1
6  8  2  5  7  1  4  9  3
1  9  7  8  3  4  5  6  2
8  2  6  1  9  5  3  4  7
3  7  4  6  8  2  9  1  5
9  5  1  7  4  3  6  2  8
5  1  9  3  2  6  8  7  4
2  4  8  9  5  7  1  3  6
7  6  3  4  1  8  2  5  9


how would i check if line 1 (4 3 5 2 6 9 7 8 1) was equal to line 2(6 8 2 5 7 1 4 9 3) and if line 2 is equal to line 3(1 9 7 8 3 4 5 6 2) etc etc

my loop to display the array is:
1
2
3
4
5
6
7
  for(int row=0; row<9; row++)
    {
      for(int col=0; col<9; col++)
        {
          cout << game[row][col]  << "  ";
        }
      cout << endl;


i can either check each line individually to equal 45(each line must equal 45) or check if all the lines are equal to themselves but i'm not sure how to write the code for either.


2) is it possible to check 3x3 arrays in this array? I want to see if each 3x3 box adds up to 45 and 1-9 once and only once.

so in the above sudoku grid somehow check:
4 3 5
6 8 2
1 9 7
then to the left of that block check:
2 6 9
5 7 1
8 3 4
etc etc
Last edited on
You can solve the problem with loops and arrays.
- Create an array of 9 ints for each individual piece that needs to be checed. That is 9 arrays for each row, 9 arrays for each column and 9 arrays for each "small board" (3x3 Section).
- now we have 27 arrays of 9 ints. Let's just make it an array of 27 arrays of 9 ints or int values[27][9].
- Let's fill the array with values from your main 2-d board. That will require 3 loops:
- One loop to fill arrays 0-8 with values from each row
- One loop to fill arrays 9-17 with values from each column
- One loop to fill arrays 18-26 with values from each small board. This part is tricky, well get there.

- Now let's add each of 9 ints from each array and see if it equals 45 (we do it 27 times).

That's it.
See the code - it is compilable:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
using namespace std;
const int ROWS = 9;
const int COLS = 9;
const int SMALLS = 9;


int main()
{
	int x = 0;
	int mainBoard[9][9];
	for(int i = 0; i < 9; i++)
	{
		for(int j = 0; j < 9; j++)
		{
			mainBoard[i][j] = x++;
		}
	}

	cout << "The entire board looks like this:\n";
	for(int i = 0; i < 9; i++)
	{
		for(int j = 0; j < 9; j++)
		{
			cout << mainBoard[i][j] << "\t";
		}
		cout << endl;
	}
	cout << endl;
	// Our helper array of arrays:
	int boardPieces [ROWS + COLS + SMALLS][9];
	// now fill each of these arrays with values from main board:
	// first lets do ROWS:
	for(int i = 0; i < ROWS; i++) // Will be done 9 times, for each row
	{
		for(int j = 0; j < 9; j++) // Each number from that row
		{
			boardPieces[i][j] = mainBoard[i][j]; // Thats easy
		}
	}
	// now lets do all COLS
	for(int i = 0; i < COLS; i++) // 
	{
		for(int j = 0; j < 9; j++)
		{
			boardPieces[i+9][j] = mainBoard[j][i]; // i and j are swapped here, 
			//i+9 is because we want to fill 9th through 18th arrays of our data structure
		}
	}
	// Now for some loop frenzy :) We want to get values from the small boards.
	// It's three rows of three columns of three rows of three columns
	for(int I = 0; I < 3; I++) // For each row of miniboards
	{
		for(int J = 0; J < 3; J++) // For each column in that row of miniboards
		{
			for(int i = 0; i < 3; i++) // for each row in that miniboard
			{	
				for(int j = 0; j < 3; j++) // for each column in that row of miniboard
				{
					// Thats the fun part. How to count index in one-dimensional array
					// if we fill it with values from 2-D array? The formula is
					// index n = (Row * Max_Col) + Col :)
					boardPieces[(I*3)+J+18][(i*3) +j] = mainBoard[(3*I)+i][(3*J)+j];
					// +18 beacause ROWS and COLS are 0-17;
				}
			}
		}
	}
	// Check the results
	cout <<" Now, let's see what are values in each section:\n";
	for(int i = 0; i < 27; i++)
	{
		if(i == 0)
			cout << " First rows: \n";
		if(i == 9)
			cout << " Now cols: \n";
		if(i == 18)
			cout << " Now small boards: \n";
		cout << "Array #" << i <<": \t";
		for(int j = 0; j < 9; j++)
		{
			cout << boardPieces[i][j] << " ";
		}
		cout << endl;
	}

	// Now let's see how much each section is worth. It should answer your first question:
	for(int i = 0; i < 27; i++)
	{
		int result = 0;
		for(int j = 0; j < 9; j++)
		{
			result += boardPieces[i][j];
		}
		cout << "Total in array #" << i << ": " << result << endl;
	}
	while(1);
}
Last edited on
Topic archived. No new replies allowed.