CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Member
Join Date
Sep 2013
Posts
36

## Please find the bug in the c++ code

Code:
```#include <iostream>
#include <math.h>
#include <string>
#include <cstdlib>

using namespace std;
long tot, tsol;

int P[8][6];
int A[8][1];

template <typename T> int sgn(T val) {
return (T(0) < val) - (val < T(0));
}

int CheckIntersection(int pia, int pa, int pib, int pb) {
return abs(sgn<int>(P[A[pia][0]][pa+A[pia][1]] + P[A[pib][0]][pb+A[pib][1]]));
}

void PrintSolution() {
tsol = tsol + 1;
string c = "";
for (int z=0;z<=8;z++) {
c = c + (char)(A[z][0] + 64) + (char)(A[z][1] + 97);
}
cout << c << " ";
cout << tot;
cout << "\n";
}

int CheckPoss (int u) {
tot = tot + 1;
int f = 0;
int ret ;
switch (u) {
case 0:
f = 0;
case 1:
f = f + CheckIntersection(0,2,1,0); //'A
break;
case 2:
f = f + CheckIntersection(0,2,1,0); // 'A
f = f + CheckIntersection(1,2,2,0); // 'B
break;
case 3:
f = f + CheckIntersection(0,2,1,0) ; // 'A
f = f + CheckIntersection(1,2,2,0) ; // 'B
f = f + CheckIntersection(0,3,3,1) ; // 'C
break;
case 4:
f = f + CheckIntersection(0,2,1,0) ; // 'A
f = f + CheckIntersection(1,2,2,0) ; // 'B
f = f + CheckIntersection(0,3,3,1) ; // 'C
f = f + CheckIntersection(1,3,4,1) ; // 'D
f = f + CheckIntersection(3,2,4,0) ; // 'F
break;
case 5:
f = f + CheckIntersection(0,2,1,0) ; // 'A
f = f + CheckIntersection(1,2,2,0) ; // 'B
f = f + CheckIntersection(0,3,3,1) ; // 'C
f = f + CheckIntersection(1,3,4,1) ; // 'D
f = f + CheckIntersection(2,3,5,1) ; // 'E
f = f + CheckIntersection(3,2,4,0) ; // 'F
f = f + CheckIntersection(4,2,5,0) ; // 'G
break;
case 6:
f = f + CheckIntersection(0,2,1,0) ; // 'A
f = f + CheckIntersection(1,2,2,0) ; // 'B
f = f + CheckIntersection(0,3,3,1) ; // 'C
f = f + CheckIntersection(1,3,4,1) ; // 'D
f = f + CheckIntersection(2,3,5,1) ; // 'E
f = f + CheckIntersection(3,2,4,0) ; // 'F
f = f + CheckIntersection(4,2,5,0) ; // 'G
f = f + CheckIntersection(3,3,6,1) ; // 'H
break;
case 7:
f = f + CheckIntersection(0,2,1,0) ; // 'A
f = f + CheckIntersection(1,2,2,0) ; // 'B
f = f + CheckIntersection(0,3,3,1) ; // 'C
f = f + CheckIntersection(1,3,4,1) ; // 'D
f = f + CheckIntersection(2,3,5,1) ; // 'E
f = f + CheckIntersection(3,2,4,0) ; // 'F
f = f + CheckIntersection(4,2,5,0) ; // 'G
f = f + CheckIntersection(3,3,6,1) ; // 'H
f = f + CheckIntersection(4,3,7,1) ; // 'I
f = f + CheckIntersection(6,2,7,0) ; // 'K
break;
case 8:
f = f + CheckIntersection(0,2,1,0) ; // 'A
f = f + CheckIntersection(1,2,2,0) ; // 'B
f = f + CheckIntersection(0,3,3,1) ; // 'C
f = f + CheckIntersection(1,3,4,1) ; // 'D
f = f + CheckIntersection(2,3,5,1) ; // 'E
f = f + CheckIntersection(3,2,4,0) ; // 'F
f = f + CheckIntersection(4,2,5,0) ; // 'G
f = f + CheckIntersection(3,3,6,1) ; // 'H
f = f + CheckIntersection(4,3,7,1) ; // 'I
f = f + CheckIntersection(5,3,8,1) ; // 'J
f = f + CheckIntersection(6,2,7,0) ; // 'K
f = f + CheckIntersection(7,2,8,0) ; // 'L
break;
}

if (f == 0) {
ret = 1;
if (u == 8) {
PrintSolution();
}
}

//cout << " {f} = " << f << " {u} " << u << endl;

if (f != 0) {
ret = 0;
}
return ret;
}

void Recurse(int n) {
int m = n + 1;

//cout << "DEBUG: " << m << endl;

for (int k=0;k<=8;k++) {
int f = 0;

for(int z=0;z<=n-1;z++) {
if (A[z][0] == k) {
f = 1;
}
}

//cout << "DEBUG3 f = " << f << endl;
if(f == 0) {
for (int r=0;r<=3;r++) {
A[n][0] = k;
A[n][1] = r;
int i = CheckPoss(m);
//cout << "DEBUG2: m = " << m << " i = " << i << endl;
if ((m < 9) && (i == 1)) {
//cout << "should call recurse.. " << endl;
Recurse(m);
}
}
}
}
}

void SetPiece(int n, int A, int b, int c, int d) {
P[n][0] = A; // C/C++ indexes start from 0
P[n][1] = b;
P[n][2] = c;
P[n][3] = d;

P[n][4] = A;
P[n][5] = b;
P[n][6] = c;
}

int main () {
SetPiece(0, -3, -2, 3, 1);
SetPiece(1, 2, 1, -4, -2);
SetPiece(2, 4, 2, -3, -2);
SetPiece(3, -1, 1, 4, -4);
SetPiece(4, -4, 4, 2, -1);
SetPiece(5, 1, -1, -2, 2);
SetPiece(6, 3, 1, -2, -1);
SetPiece(7, -3, -4, 2, 3);
SetPiece(8, -4, 3, 3, -2);

Recurse(0);

long tp = pow(4.0,9)*9*8*7*6*5*4*3*2;
cout << tot << " possibilities checked." << endl;
cout << ((tp-tot)/1000000) << " million possibilities discarded." << endl;
cout << tsol << " solutions found." <<endl;
}```
This is the c++ code written by me for the BASIC program present in THIS LINK : http://nrich.maths.org/1388.
Could anyone please find the bug in my code.

Regards,
Navy.

2. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: Please find the bug in the c++ code

Originally Posted by navy1991
Could anyone please find the bug in my code.
That is what you're supposed to do.

What debugging have you done?

Regards,

Paul McKenzie

3. Member
Join Date
Sep 2013
Posts
36

## Re: Please find the bug in the c++ code

Hello Paul,
I figured out that in the CheckPoss function, case 8 is not being checked .I tried to find the value of 'f' by inserting the comments , which u can see. But, I am still not able to find the bug.

Regards,
Naveen.

4. ## Re: Please find the bug in the c++ code

Originally Posted by navy1991
Hello Paul,
I figured out that in the CheckPoss function, case 8 is not being checked .I tried to find the value of 'f' by inserting the comments , which u can see. But, I am still not able to find the bug.
As Paul pointed out you have to debug your code to find the bug!

Note also that noone will check your code until you will fromat it properly: with indentations and white spaces between operands.
Example: this is your unformatted code:
Originally Posted by navy1991
Code:
```for (int z=0;z<=8;z++) {
c = c + (char)(A[z][0] + 64) + (char)(A[z][1] + 97);
}```
And this is how it should look like to be read and understand very easy:
Code:
```for (int z=0; z<=8; z++)
{
c = c + (char)(A[z][0] + 64) + (char)(A[z][1] + 97);
}```

5. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: Please find the bug in the c++ code

Originally Posted by navy1991
Hello Paul,
I figured out that in the CheckPoss function, case 8 is not being checked .I tried to find the value of 'f' by inserting the comments , which u can see. But, I am still not able to find the bug.
I will go back to your post earlier:
This is the c++ code written by me
You must have had a plan before you wrote the code, correct? So where in the program does the code deviate from this plan?

The point is that you wrote the code, therefore you must be able to debug the code that you wrote. You can't write C++ code and not know what to do when or if a bug in the logic occurs.

Regards,

Paul McKenzie

6. Member
Join Date
Sep 2013
Posts
36

## Re: Please find the bug in the c++ code

Hello Victor and Paul,

Here you go: This is the formatted code , which I am trying:

Code:
```#include <iostream>
#include <math.h>
#include <string>
#include <cstdlib>

using namespace std;
long tot, tsol;

int P[9][7];
int A[9][2];

template <typename T> int sgn(T val) {
return (T(0) < val) - (val < T(0));
}

int CheckIntersection(int pia, int pa, int pib, int pb) {
return abs(sgn<int>(P[A[pia][1]][pa+A[pia][2]] + P[A[pib][1]][pb+A[pib][2]]));
}

void PrintSolution() {
tsol = tsol + 1;
string c = "";
for (int z=0;z<=8;z++) {
c = c + (char)(A[z][1] + 64) + (char)(A[z][2] + 97);
}
cout << c << " ";
cout << tot;
cout << "\n";
}

int CheckPoss (int u) {
tot = tot + 1;
int f = 0;
int ret = 0;
switch (u) {
case 1:
f = f + CheckIntersection(1, 3, 2, 1);    //'A
break;
case 2:
f = f + CheckIntersection(1, 3, 2, 1);  //   'A
f = f + CheckIntersection(2, 3, 3, 1);   //  'B
break;
case 3:
f = f + CheckIntersection(1, 3, 2, 1) ; //    'A
f = f + CheckIntersection(2, 3, 3, 1) ; //    'B
f = f + CheckIntersection(1, 4, 4, 2) ; //    'C
break;
case 4:
f = f + CheckIntersection(1, 3, 2, 1) ; //    'A
f = f + CheckIntersection(2, 3, 3, 1) ; //    'B
f = f + CheckIntersection(1, 4, 4, 2) ; //    'C
f = f + CheckIntersection(2, 4, 5, 2) ; //    'D
f = f + CheckIntersection(4, 3, 5, 1) ; //    'F
break;
case 5:
f = f + CheckIntersection(1, 3, 2, 1) ; //    'A
f = f + CheckIntersection(2, 3, 3, 1) ; //    'B
f = f + CheckIntersection(1, 4, 4, 2) ; //    'C
f = f + CheckIntersection(2, 4, 5, 2) ; //    'D
f = f + CheckIntersection(3, 4, 6, 2) ; //    'E
f = f + CheckIntersection(4, 3, 5, 1) ; //    'F
f = f + CheckIntersection(5, 3, 6, 1) ; //    'G
break;
case 6:
f = f + CheckIntersection(1, 3, 2, 1) ; //    'A
f = f + CheckIntersection(2, 3, 3, 1) ; //    'B
f = f + CheckIntersection(1, 4, 4, 2) ; //    'C
f = f + CheckIntersection(2, 4, 5, 2) ; //    'D
f = f + CheckIntersection(3, 4, 6, 2) ; //    'E
f = f + CheckIntersection(4, 3, 5, 1) ; //    'F
f = f + CheckIntersection(5, 3, 6, 1) ; //    'G
f = f + CheckIntersection(4, 4, 7, 2) ; //    'H
break;
case 7:
f = f + CheckIntersection(1, 3, 2, 1) ; //    'A
f = f + CheckIntersection(2, 3, 3, 1) ; //    'B
f = f + CheckIntersection(1, 4, 4, 2) ; //    'C
f = f + CheckIntersection(2, 4, 5, 2) ; //    'D
f = f + CheckIntersection(3, 4, 6, 2) ; //    'E
f = f + CheckIntersection(4, 3, 5, 1) ; //    'F
f = f + CheckIntersection(5, 3, 6, 1) ; //    'G
f = f + CheckIntersection(4, 4, 7, 2) ; //    'H
f = f + CheckIntersection(5, 4, 8, 2) ; //    'I
f = f + CheckIntersection(7, 3, 8, 1) ; //    'K
break;
case 8:
f = f + CheckIntersection(1, 3, 2, 1) ; //    'A
f = f + CheckIntersection(2, 3, 3, 1) ; //    'B
f = f + CheckIntersection(1, 4, 4, 2) ; //    'C
f = f + CheckIntersection(2, 4, 5, 2) ; //    'D
f = f + CheckIntersection(3, 4, 6, 2) ; //    'E
f = f + CheckIntersection(4, 3, 5, 1) ; //    'F
f = f + CheckIntersection(5, 3, 6, 1) ; //    'G
f = f + CheckIntersection(4, 4, 7, 2) ; //    'H
f = f + CheckIntersection(5, 4, 8, 2) ; //    'I
f = f + CheckIntersection(6, 4, 9, 2) ; //    'J
f = f + CheckIntersection(7, 3, 8, 1) ; //    'K
f = f + CheckIntersection(8, 3, 9, 1) ; //    'L
break;
}

if (f == 0) {
ret = 1;
if (u == 8) {
PrintSolution();
}
}

if (ret == 0) ret = f;

return ret;
}

void Recurse(int n) {
int m = n + 1;
for (int k=0;k<=8;k++) {
int f = 0;

for(int z=0;z<=n-1;z++) {
if (A[z][1] == k) {
f = 1;
}
}

if(f == 0) {
for (int r=0;r<=3;r++) {
A[m][1] = k;
A[m][2] = r;
int i = CheckPoss(m);
if (m < 9 && i == 1) {
Recurse(m);
}
}
}
}
}

void SetPiece(int n, int A, int b, int c, int d) {
P[n][0] = A; // C/C++ indexes start from 0
P[n][1] = b;
P[n][2] = c;
P[n][3] = d;

P[n][4] = A;
P[n][5] = b;
P[n][6] = c;
}

int main () {
SetPiece(0, -3, -2, 3, 1);
SetPiece(1, 2, 1, -4, -2);
SetPiece(2, 4, 2, -3, -2);
SetPiece(3, -1, 1, 4, -4);
SetPiece(4, -4, 4, 2, -1);
SetPiece(5, 1, -1, -2, 2);
SetPiece(6, 3, 1, -2, -1);
SetPiece(7, -3, -4, 2, 3);
SetPiece(8, -4, 3, 3, -2);

Recurse(0);

long tp = pow(4.0,9)*9*8*7*6*5*4*3*2;
cout << tot << " possibilities checked." << endl;
cout << ((tp-tot)/1000000) << " million possibilities discarded." << endl;
cout << tsol << " solutions found." <<endl;
}```

7. Elite Member Power Poster
Join Date
Apr 1999
Posts
27,449

## Re: Please find the bug in the c++ code

Originally Posted by navy1991
Hello Victor and Paul,

Here you go: This is the formatted code , which I am trying:
Why are you just "trying" the code? You must be confident that the code will do the job correctly. If it doesn't do the job, then you debug the code. This means that you must plan and design before you write one line of code. Then you write the code according to the plan/design.

Just writing code, seeing that it doesn't work, and then asking one of us to fix the code is not how programming works.

Regards,

Paul McKenzie

8. ## Re: Please find the bug in the c++ code

Originally Posted by navy1991
Hello Victor and Paul,

Here you go: This is the formatted code , which I am trying:

Code:
```#include <iostream>
#include <math.h>
#include <string>
#include <cstdlib>

using namespace std;
long tot, tsol;

int P[9][7];
int A[9][2];

...
if(f == 0) {
for (int r=0;r<=3;r++) {
A[m][1] = k;
A[m][2] = r;
int i = CheckPoss(m);
if (m < 9 && i == 1) {
Recurse(m);
}
}
...```
Arrays in C are indexed from zero, not from 1.
So int A[9][2]; means that there only
Code:
```A[m][0] = k;
A[m][1] = k;```
are allowed!

9. Member
Join Date
Sep 2013
Posts
36

## Re: Please find the bug in the c++ code

Hello victor,

Code:
```#include <iostream>
#include <math.h>
#include <string>
#include <cstdlib>

using namespace std;
long tot, tsol;

int P[8][6];
int A[8][1];

template <typename T> int sgn(T val) {
return (T(0) < val) - (val < T(0));
}

int CheckIntersection(int pia, int pa, int pib, int pb) {
return abs(sgn<int>(P[A[pia][0]][pa+A[pia][1]] + P[A[pib][0]][pb+A[pib][1]]));
}

void PrintSolution() {
tsol = tsol + 1;
string c = "";
for (int z=0;z<=8;z++) {
c = c + (char)(A[z][0] + 64) + (char)(A[z][1] + 97);
}
cout << c << " ";
cout << tot;
cout << "\n";
}

int CheckPoss (int u) {
tot = tot + 1;
int f = 0;
int ret = 0 ;
switch (u) {
case 0:
f = 1;
case 1:
f = f + CheckIntersection(0,2,1,0);    //'A
break;
case 2:
f = f + CheckIntersection(0,2,1,0);  //   'A
f = f + CheckIntersection(1,2,2,0);   //  'B
break;
case 3:
f = f + CheckIntersection(0,2,1,0) ; //    'A
f = f + CheckIntersection(1,2,2,0) ; //    'B
f = f + CheckIntersection(0,3,3,1) ; //    'C
break;
case 4:
f = f + CheckIntersection(0,2,1,0) ; //    'A
f = f + CheckIntersection(1,2,2,0) ; //    'B
f = f + CheckIntersection(0,3,3,1) ; //    'C
f = f + CheckIntersection(1,3,4,1) ; //    'D
f = f + CheckIntersection(3,2,4,0) ; //    'F
break;
case 5:
f = f + CheckIntersection(0,2,1,0) ; //    'A
f = f + CheckIntersection(1,2,2,0) ; //    'B
f = f + CheckIntersection(0,3,3,1) ; //    'C
f = f + CheckIntersection(1,3,4,1) ; //    'D
f = f + CheckIntersection(2,3,5,1) ; //    'E
f = f + CheckIntersection(3,2,4,0) ; //    'F
f = f + CheckIntersection(4,2,5,0) ; //    'G
break;
case 6:
f = f + CheckIntersection(0,2,1,0) ; //    'A
f = f + CheckIntersection(1,2,2,0) ; //    'B
f = f + CheckIntersection(0,3,3,1) ; //    'C
f = f + CheckIntersection(1,3,4,1) ; //    'D
f = f + CheckIntersection(2,3,5,1) ; //    'E
f = f + CheckIntersection(3,2,4,0) ; //    'F
f = f + CheckIntersection(4,2,5,0) ; //    'G
f = f + CheckIntersection(3,3,6,1) ; //    'H
break;
case 7:
f = f + CheckIntersection(0,2,1,0) ; //    'A
f = f + CheckIntersection(1,2,2,0) ; //    'B
f = f + CheckIntersection(0,3,3,1) ; //    'C
f = f + CheckIntersection(1,3,4,1) ; //    'D
f = f + CheckIntersection(2,3,5,1) ; //    'E
f = f + CheckIntersection(3,2,4,0) ; //    'F
f = f + CheckIntersection(4,2,5,0) ; //    'G
f = f + CheckIntersection(3,3,6,1) ; //    'H
f = f + CheckIntersection(4,3,7,1) ; //    'I
f = f + CheckIntersection(6,2,7,0) ; //    'K
break;
case 8:
f = f + CheckIntersection(0,2,1,0) ; //    'A
f = f + CheckIntersection(1,2,2,0) ; //    'B
f = f + CheckIntersection(0,3,3,1) ; //    'C
f = f + CheckIntersection(1,3,4,1) ; //    'D
f = f + CheckIntersection(2,3,5,1) ; //    'E
f = f + CheckIntersection(3,2,4,0) ; //    'F
f = f + CheckIntersection(4,2,5,0) ; //    'G
f = f + CheckIntersection(3,3,6,1) ; //    'H
f = f + CheckIntersection(4,3,7,1) ; //    'I
f = f + CheckIntersection(5,3,8,1) ; //    'J
f = f + CheckIntersection(6,2,7,0) ; //    'K
f = f + CheckIntersection(7,2,8,0) ; //    'L
break;
}

if (f == 0) {
ret = 1;
if (u == 8) {
PrintSolution();
}
}

//cout << " {f} = " << f << " {u} " << u << endl;

if (f != 0) ret = 0;

return ret;
}

void Recurse(int n) {
int m = n + 1;

//cout << "DEBUG: " << m << endl;

for (int k=0;k<=8;k++) {
int f = 0;

for(int z=0;z<=n-1;z++) {
if (A[z][0] == k) {
f = 1;
}
}

//cout << "DEBUG3 f = " << f << endl;
if(f == 0) {
for (int r=0;r<=3;r++) {
A[n][0] = k;
A[n][1] = r;
int i = CheckPoss(m);
//cout << "DEBUG2: m = " << m << " i = " << i << endl;
if ((m < 9) && (i == 1)) {
//cout << "should call recurse.. " << endl;
Recurse(m);
}
}
}
}
}

void SetPiece(int n, int A, int b, int c, int d) {
P[n][0] = A; // C/C++ indexes start from 0
P[n][1] = b;
P[n][2] = c;
P[n][3] = d;

P[n][4] = A;
P[n][5] = b;
P[n][6] = c;
}

int main () {
SetPiece(0, -3, 1, 2, -4);
SetPiece(1, 3, -2, 1, -4);
SetPiece(2, -1, 2, 3, -4);
SetPiece(3, -2, 3, 1, -4);
SetPiece(4, 1, -3, -4, 4);
SetPiece(5, 2, -2, 3, -1);
SetPiece(6, 4, -2, 1, -3);
SetPiece(7, 2, -4, 1, -3);
SetPiece(8, 4, -2, 3, -1);

Recurse(0);

long tp = pow(4.0,9)*9*8*7*6*5*4*3*2;
cout << tot << " possibilities checked." << endl;
cout << ((tp-tot)/1000000) << " million possibilities discarded." << endl;
cout << tsol << " solutions found." <<endl;
}```

10. ## Re: Please find the bug in the c++ code

Originally Posted by navy1991
Hello victor,

I won't... until you will have debugged it.

11. Member
Join Date
Sep 2013
Posts
36

## Re: Please find the bug in the c++ code

Hi Paul,
My plan was to follow the logic as explained for the BASIC program. With this in mind, I started writing my code. In BASIC programming, the array starts from 1, but in C++ it starts from 0. So, all the logic is same. Except that , everything starts from 0 instead of 1.

The code deviated from the plan when I started to write the Recurse and the CheckPoss function. I am sure that my bug is in these two functions. More specifically , I did not know what the CheckPoss function is returning in the BASIC program if the condition f = o is false.

Regards,
Naveen

12. Member
Join Date
Sep 2013
Posts
36

Hello Paul,

Regards,
Naveen.

13. Member
Join Date
Sep 2013
Posts
36

## Re: Please find the bug in the c++ code

Hello Victor,

This is the reply that i get on my console window:

188 possibilities checked.
0 solutions found.
Press any key to continue . . .

I tried this for around 20 times and got frustated.

Regards,
Naveen.

14. Elite Member Power Poster
Join Date
Apr 2000
Location
Belgium (Europe)
Posts
4,626

## Re: Please find the bug in the c++ code

since you transcribed from basic with a different array indexing, chances are you have an indexing error somewhere, focus there.

or change A and P to types that have bounds checking built in.

try this:
change the definition of A and P into
Code:
```#include <array>
std::array<std::array<int,6>,8> P;
std::array<std::array<int,1>,8> A;```
rest of the code should compile unchanged
run the code in a debug build. Any out of bounds error will cause an assert.

15. Member
Join Date
Sep 2013
Posts
36

## Re: Please find the bug in the c++ code

Hi OReubens,

I did like you told.I got a code like this:
Code:
```throw -- terminate on thrown exception REPLACEABLE
#define _HAS_EXCEPTIONS 0
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <crtdbg.h>

_STD_BEGIN

#ifdef _DEBUG
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message, const wchar_t *file, unsigned int line)
if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, message)==1)
{
::_CrtDbgBreak();
}
}
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const unsigned short *message, const unsigned short *file, unsigned int line)
_Debug_message((wchar_t *) message, (wchar_t *) file, line);
}

#endif

_STD_END```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•