Sudoku Puzzle Checker

This program will partially check the validity of a given sudoku puzzle. The user will enter the puzzle as 9 rows of 9 numbers in the range of 1-9. If the same digit appears twice on a row or column then the puzzle is considered invalid and should be reported as such. If no duplicates are found then the program should report that the puzzle is partially correct.

For example:
This is Brent's Sudoku Puzzle Checker. 
Please enter all 81 numbers corresponding to the 9 rows of 9 numbers each.
Remember that only numbers in the range 1-9 are allowed.
1 2 3 4 5 6 7 8 9
7 8 9 1 2 3 4 5 6
4 5 6 7 8 9 1 2 3
3 1 2 8 4 5 9 6 7
6 9 7 3 1 2 8 4 5
8 4 5 6 9 7 3 1 2
2 3 1 5 7 4 6 9 8
9 6 8 2 3 1 5 7 4
5 7 4 9 6 8 2 3 1
This puzzle is partially correct. If each 3x3 section does
not contain duplicates, then this puzzle would be correct.


Where do I start? How do I make an array 9x9?
Last edited on
> Where do I start? How do I make an array 9x9?
int sudoku[9][9];

Where do I start? How do I make an array 9x9?

Maybe like this:
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
#include <iostream>
#include <algorithm>

template<size_t rows, size_t columns>
class Soduko
{
public:
    Soduko()
    {
        std::fill_n(&data[0][0], rows * columns, 0);   
    }
    void set(size_t row, size_t col, int val)
    {
        // check if row and col and val are valid
        data[row][col] = val;
    }
    int get(size_t row, size_t col) const
    {
        // check if row and col are valid
        return data[row][col];
    }
    
private:
    int data[rows][columns];
};

int main()
{
    Soduko<9,9> soduko;    
}
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
#include <bits/stdc++.h>
using namespace std;

int main(){
	
	cout << "This is Jakes's Sudoku Puzzle Checker." << endl;
	cout << "Please enter all 81 numbers corresponding to the 9 rows of 9 numbers each." << endl;
	cout << "Remember that only numbers in the range 1-9 are allowed." <<endl;
	
	int sudoku[9][9];
	int i, j, sum ,temp, flag = 1;
	cout << "Enter a 9x9 sudoku: " << endl;
	for(i = 0; i < 9; i++){
		for(j = 0; j < 0; j++){
			scanf("%d", &temp);
			if(temp > 0 && temp < 10)				// check for valid input for Sudoku
				sudoku[i][j] = temp;
			else{									// if any element is not invalid then successive breaks
				flag = 0;
				cout << "Invalid Input";
				break;
			}
		}
		if(flag == 0)
			break;
	}
	if(flag){										// if every input is in valid range then go for validation of Sudoku
		for(i = 0;i < 9; i++){
			sum = 0;
			for(j = 0; j < 9; j++)
				sum = sum + sudoku[i][j];
			if(sum != 45)
				break;								// if sum is not 45 in any row then  go out of for loop
		}
		if(sum == 45){								// if sum is 45 in all rows then check for columns
			for(i = 0; i < 9; i++){
				sum = 0;
				for(j = 0; j < 9; j++)
					sum = sum + sudoku [j][i];
				if(sum != 45)
					break;							// f sum is not 45 in any column then  go out of for loop
			}
			if(sum == 45)							// if sum is 45 in all columns too
				cout << "Valid";
			else
				cout << "Invalid";
		}
		else
			cout << "Invalid";
	}
	
	return 0;
}


This is Jake's Sudoku Puzzle Checker.
Please enter all 81 numbers corresponding to the 9 rows of 9 numbers each.
Remember that only numbers in the range 1-9 are allowed.
Enter a 9x9 sudoku:
Invalid


If gives me a straight invalid answer. How do I fix it?
> for(j = 0; j < 0; j++){
< 0
Really?
Why are you mixing C and C++?

 
scanf("%d", &temp);


 
cin >> temp;


Perhaps:

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
#include <iostream>
using namespace std;

int main()
{
	constexpr size_t SUDSZE {9};
	constexpr auto SUDSUM {SUDSZE * (SUDSZE + 1) / 2};

	cout << "This is Jakes's Sudoku Puzzle Checker.\n";
	cout << "Please enter all " << SUDSZE * SUDSZE << " numbers corresponding to the " << SUDSZE << " rows of " << SUDSZE << " numbers each.\n";
	cout << "Remember that only numbers in the range 1-" << SUDSZE << " are allowed.\n";

	int sudoku[SUDSZE][SUDSZE] {};
	bool bad {};

	cout << "Enter a " << sudSize << "x" << sudSize << " sudoku:\n";

	for (size_t i = 0; !bad && i < SUDSZE; ++i)
		for (size_t j = 0; !bad && j < SUDSZE; ++j) {
			size_t temp {};

			cin >> temp;
			if (temp > 0 && temp <= SUDSZE)
				sudoku[i][j] = temp;
			else
				bad = true;
		}

	if (bad)
		return (cout << "Invalid input\n"), 1;

	size_t sum {};

	for (size_t i = 0; i < SUDSZE; ++i) {
		sum = 0;

		for (size_t j = 0; j < SUDSZE; ++j)
			sum += sudoku[i][j];

		if (sum != SUDSUM)
			break;
	}

	if (sum == SUDSUM) {
		for (size_t i = 0; i < SUDSZE; ++i) {
			sum = 0;

			for (size_t j = 0; j < SUDSZE; ++j)
				sum += sudoku[j][i];

			if (sum != SUDSUM)
				break;
		}

		if (sum == SUDSUM)
			return (cout << "Valid"), 0;
	}

	return (cout << "Invalid"), 2;
}




This is Jakes's Sudoku Puzzle Checker.
Please enter all 81 numbers corresponding to the 9 rows of 9 numbers each.
Remember that only numbers in the range 1-9 are allowed.
Enter a 9x9 sudoku:
1 2 3 4 5 6 7 8 9
7 8 9 1 2 3 4 5 6
4 5 6 7 8 9 1 2 3
3 1 2 8 4 5 9 6 7
6 9 7 3 1 2 8 4 5
8 4 5 6 9 7 3 1 2
2 3 1 5 7 4 6 9 8
9 6 8 2 3 1 5 7 4
5 7 4 9 6 8 2 3 1
Valid

Last edited on
So I changed some of the code to make it make more sense for me. Now there a errors to it. How would I fix them?


g++ "Sudoku Puzzle Checker.cpp" -o "Sudoku Puzzle Checker.exe"
Sudoku Puzzle Checker.cpp: In function 'int main()':
Sudoku Puzzle Checker.cpp:20:20: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
  const int sudSize {9};
                    ^
Sudoku Puzzle Checker.cpp:21:19: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
  const int sudSum {sudSize * (sudSize + 1) / 2};
                   ^
Sudoku Puzzle Checker.cpp:27:30: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
  int sudoku[sudSize][sudSize]{};
                              ^
Sudoku Puzzle Checker.cpp:28:10: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
  bool bad{};
          ^
Sudoku Puzzle Checker.cpp:28:11: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
  bool bad{};
           ^
Sudoku Puzzle Checker.cpp:34:12: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
    int temp{};
            ^
Sudoku Puzzle Checker.cpp:34:13: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
    int temp{};
             ^
Sudoku Puzzle Checker.cpp:46:10: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
  int sum {};
          ^
Sudoku Puzzle Checker.cpp:46:11: warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
  int sum {};
           ^
Last edited on
Any help?
This should do:
g++ -std=c++11 "Sudoku Puzzle Checker.cpp" -o "Sudoku Puzzle Checker.exe"
closed account (z05DSL3A)
Use the -std=c++11 or -std=gnu++11 option on your compile command?


Edit: thmm's post was not shown when I posted.
Last edited on
Last edited on
Topic archived. No new replies allowed.