Could someone help me with the explanation of the codes below?

thanks

#include<iostream.h>
#include<conio.h>
#include<process.h>

int a[9][9]={0,0,9,0,0,0,0,2,0,
          0,6,0,0,0,9,0,0,5,
          0,0,0,8,7,0,0,0,0,
          0,0,1,0,8,0,0,0,0,
          0,4,0,5,0,7,1,3,0,
          6,0,0,0,1,0,4,0,0,
          0,0,0,0,4,8,2,0,0,
          7,0,0,6,0,0,0,5,0,
          0,9,0,0,0,0,8,0,0};
int b[9][9],k,l,m,z,flag=0,g,h;
int horizontal(int,int,int);
int vertical(int,int,int);
int box(int,int,int);
int assume(int,int);
void main()
{
clrscr();
cout<<"enter values\n";
for(int i=0;i<9;i++)
for(int j=0;j<9;j++)
cin>>a[i][j];

for(i=0;i<9;i++)
for(j=0;j<9;j++)
b[i][j]=a[i][j];

for(i=0;i<9;i=i+1)
for(j=0;j<9;j=j+1)
{
//flag=0;
if(a[i][j]==0)
{
if(flag)
{
i=g;j=h;
flag=0;
}
if(assume(i,j)==2)
{
flag=1;
if(j==0)
{
h=8;
g=i;
g--;
}
else
{
g=i;
h=j;
h--;
}
if(a[g][h]!=0)
{
if(h==0)
{
h=8;
g--;
}
else
{
h--;
}
if(a[g][h]!=0)
{
if(h==0)
{
h=8;
g--;
}
else
{
h--;
}
if(a[g][h]!=0)
{
if(h==0)
{
h=8;
g--;
}
else
{
h--;
}
if(a[g][h]!=0)
{
if(h==0)
{
h=8;
g--;
}
else
{
h--;
}
}

}

}

}
}

}
}
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
cout<<b[i][j]<<"\t";
cout<<"\n";
}
getch();
}


int horizontal(int i,int j,int z)
{
k=i;l=j;
for(j=0;j<9;j++)
{
if(j==l)
{
if(j==8)
break;
j++;
}
if((z==b[i][j]))
return 0;
}
return 1;
}
int vertical(int i,int j,int z)
{
k=i;
l=j;
for(i=0;i<9;i++)
{
if(i==k)
{
if(i==8)
break;
i++;
}
if((z==b[i][j]))
return 0;
}
return 1;
}
int box(int i,int j,int z)
{
k=i;
l=j;
if(i<=2&&j<=2)
{
for(int m=0;m<=2;m++)
{
for(int n=0;n<=2;n++)
{
if((k!=m&&l!=n)&&(z==b[m][n])) //checkout here
return 0;
}}
return 1;
}

else if(i<=2&&j<=5)
{
for(int m=0;m<=2;m++)
for(int n=3;n<=5;n++)
{
if(k==m&&l==n);
else if(z==b[m][n])
return 0;
}
return 1;
}

else if(i<=2&&j<=8)
{
for(int m=0;m<=2;m++)
for(int n=6;n<=8;n++)
{
if(k==m&&l==n);
else if(z==b[m][n])
return 0;
}
return 1;
}

else if(i<=5&&j<=2)
{
for(int m=3;m<=5;m++)
for(int n=0;n<=2;n++)
{
if(k==m&&l==n);
else if(z==b[m][n])
return 0;
}
return 1;
}

else if(i<=5&&j<=5)
{
for(int m=3;m<=5;m++)
for(int n=3;n<=5;n++)
{
if(k==m&&l==n);
else if(z==b[m][n])
return 0;
}
return 1;
}

else if(i<=5&&j<=8)
{
for(int m=3;m<=5;m++)
for(int n=6;n<=8;n++)
{
if(k==m&&l==n);
else if(z==b[m][n])
return 0;
}
return 1;
}

else if(i<=8&&j<=2)
{
for(int m=6;m<=8;m++)
for(int n=0;n<=2;n++)
{
if(k==m&&l==n);
else if(z==b[m][n])
return 0;
}
return 1;
}

else if(i<=8&&j<=5)
{
for(int m=6;m<=8;m++)
for(int n=3;n<=5;n++)
{
if(k==m&&l==n);
else if(z==b[m][n])
return 0;
}
return 1;
}

else if(i<=8&&j<=8)
{
for(int m=6;m<=8;m++)
for(int n=6;n<=8;n++)
{
if(k==m&&l==n);
else if(z==b[m][n])
return 0;
}
return 1;
}

return 1;
}

int assume(int i,int j)
{
m=b[i][j]+1;
while(1)
{
b[i][j]=m;
if(m>9)
{
b[i][j]=0;
return 2;
}
if(vertical(i,j,b[i][j]))
if(horizontal(i,j,b[i][j]))
if(box(i,j,b[i][j]))
{
b[i][j]=m;
return 0;
}
m++;
}
}

Recommended Answers

All 5 Replies

I'm just as confused as you are, as expected with that mess.

Why don't you leave out all the white space and convert it with the letters of the alphabet in reverse!
There is a preview, before you post, there are code tags, there is info enough here to read before you post. Please do take into consideration we are only human. It is true we can perform miracles sometimes, but that does not mean we have reached a state of divinity(at least I haven't...)

Take off your glasses.

commented: Read the rules and post formatted code if you want help -5
Member Avatar for iamthwee

For humour I decided to compile and run it after a few mods:

test.cpp

#include <iostream>
#include <conio.h>
#include <process.h>

using namespace std;

int a[9][9] = {
               0, 0, 9, 0, 0, 0, 0, 2, 0,
               0, 6, 0, 0, 0, 9, 0, 0, 5,
               0, 0, 0, 8, 7, 0, 0, 0, 0,
               0, 0, 1, 0, 8, 0, 0, 0, 0,
               0, 4, 0, 5, 0, 7, 1, 3, 0,
               6, 0, 0, 0, 1, 0, 4, 0, 0,
               0, 0, 0, 0, 4, 8, 2, 0, 0,
               7, 0, 0, 6, 0, 0, 0, 5, 0,
               0, 9, 0, 0, 0, 0, 8, 0, 0};

int b[9][9], k, l, m, z, flag = 0, g, h;
int horizontal ( int, int, int );
int vertical ( int, int, int );
int box ( int, int, int );
int assume ( int, int );

int main()
{
  int i,j;
  
  cout << "enter values\n";
  for ( int i = 0; i < 9; i++ )
    for ( int j = 0; j < 9; j++ )
      cin >> a[i][j];

  for ( i = 0; i < 9; i++ )
    for ( j = 0; j < 9; j++ )
      b[i][j] = a[i][j];

  for ( i = 0; i < 9; i = i + 1 )
    for ( j = 0; j < 9; j = j + 1 )
    {
      if ( a[i][j] == 0 )
      {
        if ( flag )
        {
          i = g;
          j = h;
          flag = 0;
        }
        if ( assume ( i, j ) == 2 )
        {
          flag = 1;
          if ( j == 0 )
          {
            h = 8;
            g = i;
            g--;
          }
          else
          {
            g = i;
            h = j;
            h--;
          }
          if ( a[g][h] != 0 )
          {
            if ( h == 0 )
            {
              h = 8;
              g--;
            }
            else
            {
              h--;
            }
            if ( a[g][h] != 0 )
            {
              if ( h == 0 )
              {
                h = 8;
                g--;
              }
              else
              {
                h--;
              }
              if ( a[g][h] != 0 )
              {
                if ( h == 0 )
                {
                  h = 8;
                  g--;
                }
                else
                {
                  h--;
                }
                if ( a[g][h] != 0 )
                {
                  if ( h == 0 )
                  {
                    h = 8;
                    g--;
                  }
                  else
                  {
                    h--;
                  }
                }
              }
            }
          }
        }
      }
    }
  for ( i = 0; i < 9; i++ )
  {
    for ( j = 0; j < 9; j++ )
      cout << b[i][j] << "\t";
    cout << "\n";
  }
  getch();
} 

int horizontal ( int i, int j, int z )
{
  k = i;
  l = j;
  for ( j = 0; j < 9; j++ )
  {
    if ( j == l )
    {
      if ( j == 8 )
        break;
      j++;
    }
    if ( ( z == b[i][j] ) )
      return 0;
  }
  return 1;
}

int vertical ( int i, int j, int z )
{
  k = i;
  l = j;
  for ( i = 0; i < 9; i++ )
  {
    if ( i == k )
    {
      if ( i == 8 )
        break;
      i++;
    }
    if ( ( z == b[i][j] ) )
      return 0;
  }
  return 1;
}

int box ( int i, int j, int z )
{
  k = i;
  l = j;
  if ( i <= 2 && j <= 2 )
  {
    for ( int m = 0; m <= 2; m++ )
    {
      for ( int n = 0; n <= 2; n++ )
      {
        if ( ( k != m && l != n ) && ( z == b[m][n] ) ) return 0;
      }
    }
    return 1;
  }

  else if ( i <= 2 && j <= 5 )
  {
    for ( int m = 0; m <= 2; m++ )
      for ( int n = 3; n <= 5; n++ )
      {
        if ( k == m && l == n );
        else if ( z == b[m][n] )
          return 0;
      }
    return 1;
  }

  else if ( i <= 2 && j <= 8 )
  {
    for ( int m = 0; m <= 2; m++ )
      for ( int n = 6; n <= 8; n++ )
      {
        if ( k == m && l == n );
        else if ( z == b[m][n] )
          return 0;
      }
    return 1;
  }

  else if ( i <= 5 && j <= 2 )
  {
    for ( int m = 3; m <= 5; m++ )
      for ( int n = 0; n <= 2; n++ )
      {
        if ( k == m && l == n );
        else if ( z == b[m][n] )
          return 0;
      }
    return 1;
  }

  else if ( i <= 5 && j <= 5 )
  {
    for ( int m = 3; m <= 5; m++ )
      for ( int n = 3; n <= 5; n++ )
      {
        if ( k == m && l == n );
        else if ( z == b[m][n] )
          return 0;
      }
    return 1;
  }

  else if ( i <= 5 && j <= 8 )
  {
    for ( int m = 3; m <= 5; m++ )
      for ( int n = 6; n <= 8; n++ )
      {
        if ( k == m && l == n );
        else if ( z == b[m][n] )
          return 0;
      }
    return 1;
  }

  else if ( i <= 8 && j <= 2 )
  {
    for ( int m = 6; m <= 8; m++ )
      for ( int n = 0; n <= 2; n++ )
      {
        if ( k == m && l == n );
        else if ( z == b[m][n] )
          return 0;
      }
    return 1;
  }

  else if ( i <= 8 && j <= 5 )
  {
    for ( int m = 6; m <= 8; m++ )
      for ( int n = 3; n <= 5; n++ )
      {
        if ( k == m && l == n );
        else if ( z == b[m][n] )
          return 0;
      }
    return 1;
  }

  else if ( i <= 8 && j <= 8 )
  {
    for ( int m = 6; m <= 8; m++ )
      for ( int n = 6; n <= 8; n++ )
      {
        if ( k == m && l == n );
        else if ( z == b[m][n] )
          return 0;
      }
    return 1;
  }

  return 1;
}

int assume ( int i, int j )
{
  m = b[i][j] + 1;
  while ( 1 )
  {
    b[i][j] = m;
    if ( m > 9 )
    {
      b[i][j] = 0;
      return 2;
    }
    if ( vertical ( i, j, b[i][j] ) )
      if ( horizontal ( i, j, b[i][j] ) )
        if ( box ( i, j, b[i][j] ) )
        {
          b[i][j] = m;
          return 0;
        }
    m++;
  }
}

output

Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\crap>test.exe
enter values
3
4
34
3
4
34
3
4
5
4
6
56
65
64
4
4
4
4
4
4
45
6
3
3
4
5
4
54
345
4
43
33
3
4
5
4
45
4
7
66
6
6
6
66
6
66
6
66
65
5
55
5
56
6
656
6
7
7
6
5
6
22
3
4
3
43
4
34
3
4
343
5
5
5
66
6
6
6
5
57
7
3       4       34      3       4       34      3       4       5
4       6       56      65      64      4       4       4       4
4       4       45      6       3       3       4       5       4
54      345     4       43      33      3       4       5       4
45      4       7       66      6       6       6       66      6
66      6       66      65      5       55      5       56      6
656     6       7       7       6       5       6       22      3
4       3       43      4       34      3       4       343     5
5       5       66      6       6       6       5       57      7

So the answer to your question, what does your code do.... stuff!

commented: lol, qudos for dealing with that junk! +4
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.