This is the c++ code written by me for the BASIC program present in THIS LINK : http://nrich.maths.org/1388.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; }

Could anyone please find the bug in my code.

Regards,

Navy.