RE: [Dev-C++] reg-Knight's tour problem
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Santiago P. C. <San...@yd...> - 2002-03-07 17:19:07
|
Hi Mohammed,
Let me try to give some help on this issue O:o)
A 'common' recursive algorithm looks like this one:
// Calculate factorial of int number
int recursive_function(int iParameter)
{
// Check for posible errors
if (iParameter < 0)
{
cout << "ERROR: Negative number!" << endl;
exit(SOME_ERROR_VALUE);
}
// Check race condition
if (iParameter == 0)
{
// We got it! No more loops ;o)
return 1;
}
else
{
// Handle params & manage with values
int iCurrentValue = iParameter;
iParameter--;
return (iCurrentValue*recursive_function(iParameter));
}
}
This is not THE answer, it's a simple function to ilustrate recursive
algorithms, there are variants depending on the desired behaviour of the
function (p.e. you can handle parameter/s before checking the race
condition).
The point here is that you must reach the race condition inside the function
and terminate the program normally, if you don't do that you'll exit the
program 'the hard way' even when the program itself succeeds.
As a good programming practice you should try to avoid exiting abruptly,
instead of this, you should perform any necessary cleanup tasks when you
exit a program p.e.: close open files, deallocate reserved memory, etc...
Hope this helps!
Best regards, Santy
-----Original Message-----
From: Mohammed Riyaz [mailto:p_m...@ho...]
Sent: Thursday, March 07, 2002 5:05 PM
To: dev...@li...
Subject: RE: [Dev-C++] reg-Knight's tour problem
hey ioannis;
The variables i,j are members of the class. Why do you say recursion never
end. The function assignment calls the other functions 'refresh', 'value' in
the line
refresh();
pos=value(j+(8*i));
hence when the this situation (given below)arises in the function value, the
program wil hald and hence the recursion ends there.
if(row_loc<0) {cout<<"NO SOLUTION";system("pause");exit(0);}
(or)
when the situation
if(l==63)
{
cout<<"RESULT :"<<endl;
for(int k=0;k<64;k++)
cout<<denied[k]<<" ";
system("pause");
exit(0);
}
in refresh function is encountered the recursion ends.
-Riyaz
--------------------------------------------------------------------------------------
This message and any files transmitted with it are confidential and intended solely
for the use of the individual or entity to whom they are addressed. No confidentiality
or privilege is waived or lost by any wrong transmission.
If you have received this message in error, please immediately destroy it and kindly
notify the sender by reply email.
You must not, directly or indirectly, use, disclose, distribute, print, or copy any
part of this message if you are not the intended recipient. Opinions, conclusions and
other information in this message that do not relate to the official business of
People Communications S.A. shall be understood as neither given nor endorsed by it.
--------------------------------------------------------------------------------------
|