# Please find the bug in the c++ code

Show 50 post(s) from this thread on one page
Page 1 of 2 12 Last
• September 24th, 2013, 04:38 AM
navy1991
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.
• September 24th, 2013, 04:41 AM
Paul McKenzie
Re: Please find the bug in the c++ code
Quote:

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
• September 24th, 2013, 04:45 AM
navy1991
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.
• September 24th, 2013, 04:54 AM
VictorN
Re: Please find the bug in the c++ code
Quote:

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:
Quote:

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); }```
• September 24th, 2013, 05:11 AM
navy1991
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; }```
• September 24th, 2013, 05:18 AM
Paul McKenzie
Re: Please find the bug in the c++ code
Quote:

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:
Quote:

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
• September 24th, 2013, 05:21 AM
Paul McKenzie
Re: Please find the bug in the c++ code
Quote:

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
• September 24th, 2013, 05:27 AM
navy1991
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
• September 24th, 2013, 05:30 AM
VictorN
Re: Please find the bug in the c++ code
Quote:

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!
• September 24th, 2013, 05:35 AM
navy1991
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; }```
• September 24th, 2013, 05:37 AM
VictorN
Re: Please find the bug in the c++ code
Quote:

Originally Posted by navy1991
Hello victor,

I won't... until you will have debugged it.:cool:
• September 24th, 2013, 05:38 AM
navy1991
Re: Please find the bug in the c++ code
Hello Paul,

Regards,
Naveen.
• September 24th, 2013, 05:42 AM
navy1991
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.
• September 24th, 2013, 06:42 AM
OReubens
Re: Please find the bug in the c++ code
With variable names like 'a', 'b', 'c', 'f', no comments, and no idea what this is supposed to be doing.

how do you expect us to tell you what's wrong with it.
we don't even know what's going wrong because we don't know what's the "right" answer.

Test/debug your code, if the answer you get is not the one you expected, you have a logic error.

just running the code X times won't change a bug or won't help you find it.
• September 24th, 2013, 06:51 AM
OReubens
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.
Show 50 post(s) from this thread on one page
Page 1 of 2 12 Last