-
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.
-
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
-
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.
-
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);
}
-
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;
}
-
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
-
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
-
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
-
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!
-
Re: Please find the bug in the c++ code
Hello victor,
please check the code below:
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;
}
-
Re: Please find the bug in the c++ code
Quote:
Originally Posted by
navy1991
Hello victor,
please check the code below:
I won't... until you will have debugged it.:cool:
-
Re: Please find the bug in the c++ code
Hello Paul,
I will follow your advice from now on.
Regards,
Naveen.
-
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.
2147 million possibilities discarded.
0 solutions found.
Press any key to continue . . .
I tried this for around 20 times and got frustated.
Regards,
Naveen.
-
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.
-
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.
-
Re: Please find the bug in the c++ code
When I debugged it after removing comments , I got like this in the command window:
DEBUG2: m = 3 i = 0
{f} = 2 {u} 2
DEBUG2: m = 2 i = 0
The bug is exactly here at the end of the CheckPoss function, because the value of 'm' should not decrease .
-
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)
{ // report error and die
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)
{ // report error and die
_Debug_message((wchar_t *) message, (wchar_t *) file, line);
}
#endif
_STD_END
-
Re: Please find the bug in the c++ code
The specific problem is this:
This is the BASIC code of CheckPoss function:
Code:
FUNCTION CheckPoss (u)
tot = tot + 1
f = 0
SELECT CASE u
CASE 2
f = f + CheckIntersection(1, 3, 2, 1) 'A
CASE 3
f = f + CheckIntersection(1, 3, 2, 1) 'A
f = f + CheckIntersection(2, 3, 3, 1) 'B
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
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(4, 3, 5, 1) 'F
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
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
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(7, 3, 8, 1) 'K
CASE 9
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
END SELECT
IF f = 0 THEN
CheckPoss = 1
IF u = 9 THEN
PrintSolution
END IF
END IF
END FUNCTION
The c++ equivalent is :
Code:
int CheckPoss (int u) {
tot = tot + 1;
int f = 0;
int ret = 0 ;
switch (u) {
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;
}
Please do not worry about the different values inside CheckIntersection function..It arises due change in counting from 1 to 0. I am not sure if what I have written at the end of the c++ function is correct.
-
Re: Please find the bug in the c++ code
You don't seem to understand what's meant by debugging. Using the debugger you can run your program, interrupt execution at any point using breakpoints, examine the values of any variables, follow the execution line by line, use the call stack to find out how you got where you are in the code, change values, set the next line of execution and all kinds of fun stuff. Nobody's talking about using ifdefs. You need to learn how to use the debugger that comes with whatever compiler you're using.
-
Re: Please find the bug in the c++ code
At GCDEF,
you are absolutely correct. I will try to use the debugger to interrupt execution and follow execution and to do all the fun stuff.
Regards.
-
Re: Please find the bug in the c++ code
The correct solution still eludes me. Every thing seems to work correct . I tried debugging line by line too. The method tries to search the correct combination by backtracking method. So even after fitting say 5 pieces , if it is not possible to find the correct piece , it backtracks. But, it backtacks fully and stops searching for new pieces that fit exactly.
thanks, navy
-
Re: Please find the bug in the c++ code
Quote:
Originally Posted by
navy1991
The correct solution still eludes me.
Quote:
if it is not possible to find the correct piece , it backtracks. But, it backtacks fully and stops searching for new pieces that fit exactly.
So why does it backtrack fully? Why does it stop searching for pieces that fit exactly?
The answer is not "I don't know" -- it should be "I will debug to see where the fault is in the logic".
Lastly, why not start out with a simpler test? Why do you need to jump in and try all of these pieces? How about just 4 pieces, or 3 pieces? Then you can follow the code (using the debugger) much easily with a simpler example. Then when you get the simpler version working, you then apply that to more pieces.
Regards,
Paul McKenzie
-
Re: Please find the bug in the c++ code
I suspect that part of the problem is due to array bounds issue. eg in printsolution() the loop is <= 8 when it should be < 8. This could be repeated through the code. Has all of the code been converted from Basic or just the function checkpos()? If all has been converted, I suggest you start again from the original code, size the arrays way too big for what you want and assume the index starts at 1. Get this code to work then deal with moving the index to start at 0 rather than start at 1. Also I would suggest using a meaninful const int for the array dimensions and 'magic numbers' in the code rather than have numbers like 8, 3, 1 etc scattered about the code. Also check the use of <= and confirm this is what is meant and not just <.