Welcome to Dream.In.Code
Become a C++ Expert!

Join 137,212 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,252 people online right now. Registration is fast and FREE... Join Now!




c++ soduku help

 
Reply to this topicStart new topic

c++ soduku help, practice assignment.. HELP APPRECIATED!

michael1201
10 Jul, 2008 - 05:35 AM
Post #1

New D.I.C Head
*

Joined: 2 Jul, 2008
Posts: 13

need help writing a program for practice program for my exam that is comming up i dont exactly know what to do. if someone can explain step by step how to do this i would appreciate it. here are the intructions for the sample exam question the teacher gave us.


Create a program that verifies Sudoku solutions:

a) Write a function that initializes a matrix with 9 lines and 9 columns from a file. The file should contain 9 lines, and each line should contain 9 numbers from 1 to 9 (separated by a space).

Example of input file (Sudoku.txt):
7 9 2 6 8 4 5 1 3
4 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

cool.gif Write a function that takes as an argument a two-dimensional array with 9 lines and 9 columns (with integer elements), and verifies if the matrix contains a valid Sudoku solution.

The rules of Sudoku state that a Sudoku puzzle is solved when the 9x9 game board contains the numbers 1 through 9 exactly once in each row (see Figure 1), column (see Figure 2), and 3x3 box (see Figure 3). The numbers can appear in any order and diagonals are not considered.

The function should verify if all the rows are valid, all the columns are valid and also if all the 3x3 boxes are valid using ONLY loop structures (e.g. for loops). You can use any temporary variables including arrays if necessary. The validation should not use specific component references (i.e. m[0][0], m[0][1], m[0][2]), instead it should use more general component references (e.g. assuming col=1 and row=0, the previous 3 elements could be referred as m[row][col-1], m[row][col], m[row][col+1]).
The function should print the matrix in a Sudoku-like format (separating the 3x3 boxes by an extra space/line). The function should print a message for each inconsistencies detected on lines, columns or 3x3 boxes, and the messages should have the following format:

If no consistencies are found, the function should print the message "Valid Sudoku solution!"

User is offlineProfile CardPM
+Quote Post

gabehabe
RE: C++ Soduku Help
10 Jul, 2008 - 05:48 AM
Post #2

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,557



Thanked: 99 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
Check out baavgai's snippet you may find it useful.

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
User is online!Profile CardPM
+Quote Post

michael1201
RE: C++ Soduku Help
10 Jul, 2008 - 07:35 AM
Post #3

New D.I.C Head
*

Joined: 2 Jul, 2008
Posts: 13

sorry i had a code just forgot to post it. here is my code but im having trouble figuring out how to post what line and number the incorrect number is. and my program wont even run. if someone can look at it and tell me where the problem is and how to fix it i would appreciate it thanks in advance.

CODE
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <iomanip>

using namespace std;
void readAPuzzle(int grid[] [9]);
bool search(int grid[] [9]);
void printGrid(int grid[] [9]);
bool isValid(int grid[] [9]);

int main()
{
    // Read a Sudoku puzzle
    int grid[9] [9];

    
    readAPuzzle(grid);
    
    printGrid(grid);
    
    if (isValid(grid))
    cout << "Is a sudoku puzzle. " << endl;
    
else
    cout << "Is not a sudoku puzzle. " << endl;
    
    return 0;
}




//Reads Puzzle into 9x9 array ( modify to read from file )
void readAPuzzle(int grid[] [9])
{
    ifstream in_f;

    
    in_f.open("soduku.txt");
    
    for (int i = 0; i < 9; i++)
    for (int j = 0; j < 9; j++)
    in_f >> grid[i] [j];
}







//Prints out the puzzle read from file (keep)
void printGrid(int grid[] [9])
{
    cout<<" Your Puzzle Is Listed Below: "<<endl;
    for (int i = 0; i < 9; i++)
    {
        for (int j = 0; j < 9; j++)
            cout << grid[i] [j] << " ";
        cout << endl;
    }
}







/** Check whether grid[i][j] is valid in the grid */

bool isValid(int grid[] [9] )
{
    bool status;
    status = true;
        
    for (int column = 0; column < 9; column++)
    if (column != j && grid[i] [column] == grid[i] [j])
        status = false;
    cout << "1st test: " << status << endl;

    for (int row = 0; row < 9; row++)
        if (row != i && grid[row] [j] == grid[i] [j])
            status = false;
cout << "2nd test: " << status << endl;
    
    for (int row = (i / 3) * 3; row < (i / 3) * 3 + 3; row++)
        for (int col = (j / 3) * 3; col < (j / 3) * 3 + 3; col++)
            if (row != i && col != j && grid[row] [col] == grid[i] [j])
                status = false;
cout << "3rd test: " << status << endl;
    
    for (int i = 0; i < 9; i++)
        for (int j = 0; j < 9; j++)
            if (grid[i][j] != 0)
                status = false;
cout << "4th test: " << status << endl;
    
    for (int i = 0; i < 9; i++)
        for (int j = 0; j < 9; j++)
            if ((grid[i][j] < 0) || (grid[i][j] > 9))
                status = false;
cout << "5th test: " << status << endl;

    
    return status;
}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/4/08 01:18PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month