-3

I have learnt C++ of late and I'm trying to contrive a Sudoku - Solving algorithm. I developed this algorithm but when I run the program, the computer takes input values from me but then doesn't give any output. Any help would be appreciated.

Kindly tell me if the program is too lengthy to execute and how can I improve on that.

#include <iostream>
#include <math.h>
using namespace std;

int main() {
    int a[9][9], r[9][9][9], pv[9][9], super[9][9], i, j, k, ij, tv, m, n, g1, g2, g, c, power, p, argh;
{
    cout<<"PROGRAM TO SOLVE A 9 X 9 SUDOKU: "<<endl<<endl<<"Enter sudoku matrix columnwise with rowwise entries in each column: "<<endl;
    for (i=0, j=0, ij = 0 ; (i<9)&&(j<9) ; i=(i<8)?i++:0, j=ij/9, ij++) {
        cin>>a[i][j];
        while ((a[i][j]<0)||(a[i][j]>9)) {
            cout<<"wrong input for c["<<i+1<<"] and r["<<j+1<<"]"<<endl<<"Enter again: ";
            cin>>a[i][j];
        }
    }
}
for (i=0, j=0, ij=0 ; (i<9)&&(j<9) ; i=(i<8)?i++:0, j=ij/9, ij++) {
    if (a[i][j]==0) {
        for (k=0 ; k<9 ; k++) {
            r[i][j][k]=k+1;
        }
    }
    else {
        for (k=0 ; k<9 ; k++) {
            r[i][j][k]=a[i][j];
        }
    }
}
for (c=1, tv = 1 ; ; c++) {
    for (i=0, j=0, ij = 0; ij<80 ; i=(i<8)?i+1:0, j = ij/9, ij++) {
        argh = i + 9*j;
        super[i][j]=9^argh;
        pv[i][j] = (c/super[i][j])%9;
        for (m=0 ; m<9 ; m=(m==i)?m+2:m+1) {
                tv = tv && (r[m][j][(pv[m][j])]!=r[i][j][(pv[i][j])]);
        }
        for (n=0 ; n<9 ; n=(n==j)?(n+2):(n+1)) {
                tv = tv && (r[m][j][(pv[m][j])]!=r[i][j][(pv[i][j])]);
        }
        for (g1=0, g2=0, g=0 ; (g1<8) ; g++, g1=g%3, g2=g/3) {
                tv = tv && (r[3*(i/3)+g1][3*(j/3)+g2][(pv[3*(i/3)+g1][3*(j/3)+g2])]!=r[i][j][(pv[i][j])]);
        }
    }
    if (tv==1) {
        for (i=0, j=0, ij=0 ; ij<80 ; ij++, i=ij%9, j=ij/9) {
            cout<<a[i][j]<<" ";
            if (i=8*i/8) {
                cout<<endl;
            }
        }
    }
    else {
        cout<<"sudoku can not be solved exactly: ";
    }
    return 0;
  }
}
Madhav Datt
  • 1,065
  • 2
  • 10
  • 25
Siddharth Joshi
  • 157
  • 1
  • 9
  • 1
    What did you observe when stepping through your code with the debugger? – πάντα ῥεῖ Jan 10 '16 at 11:48
  • There is no error at all but still the program doesn't give an output. – Siddharth Joshi Jan 10 '16 at 11:50
  • 2
    An important software-engineering skill is to **isolate** problems. Find out which area of the program causes the problem. It surely runs fine until a certain point. Which point is that? – Christian Hackl Jan 10 '16 at 11:52
  • `for (c=1, tv = 1 ; ; c++) ` has no condition. Are you sure this loop is ending at all?. That's why I asked you what you observed when stepping through your code line by line. You probably miss what I meant in my 1st comment. – πάντα ῥεῖ Jan 10 '16 at 11:53
  • @πάνταῥεῖ that loop ends when it reaches `return 0;`. It's not a loop actually. – Bob__ Jan 10 '16 at 12:51
  • @Bob__, add this as an answer, probably it is an actual issue. – Alex Jan 10 '16 at 14:47
  • Look into the `backtrack` algorithm. You can probably even find an example using itl – Greg M Jan 10 '16 at 15:10
  • The whole point fo sudoko is to exercise your mind. – Ed Heal Jan 10 '16 at 15:31
  • `I have learnt C++ of late` - where are the _code comments_? Can you tell from the names of your variables what they represent? When you have a "multi-dimensional data structure", it is most probably most appropriate to iterate using _nested loops_. `ij` looks awful. – greybeard Jan 10 '16 at 15:59

1 Answers1

0

I honestly didn't even try to understand your code (you should really comment in source code some of the most obscure parts), but I've noticed some issues.

The first loop for example (all of them, really), couldn't it be simpler?

for ( i = 0; i < 9; ++i ) {
    for ( j = 0; j < 9; ++j ) {
        cin >> a[i][j];
        // ... check the input ...
    }
}

What were you trying to accomplish with all those i=(i<8)?i++:0, j=ij/9, ij++ parts?

The last "loop" as I already commented is executed only once because of the return 0; inside its scope. Probably that wasn't your purpose, but on the other side, as many others pointed out, if you write it like you did

for (c=1, tv = 1 ; ; c++) { ... }

It will continue indefinitly because there's no ending condition.

What causes the program to freeze (one of the problems at least) is, I belive, a loop inside it (the sub square check, after those for rows and columns, I guess):

for (g1=0, g2=0, g=0 ; (g1<8) ; g++, g1=g%3, g2=g/3) { ... }

the condition g1 < 8 is always true, due to how you update it: g1 = g % 3.

There are other issues, maybe, but I'm sorry I can't really figure out what (and how) are you trying to do.

Bob__
  • 12,361
  • 3
  • 28
  • 42