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 .
Printable View
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 .
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
The specific problem is this:
This is the BASIC code of CheckPoss function:
The c++ equivalent is :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
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.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;
}
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.
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.
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
So why does it backtrack fully? Why does it stop searching for pieces that fit exactly?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.
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
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 <.