Re: [Dev-C++] Basic Case in recursivity
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Per W. <pw...@ia...> - 2003-06-09 01:25:55
|
See below:
/Per W
On Sun, 8 Jun 2003, Andr=E9 Mac=E1rio Barros wrote:
> Dear Users,
> Please consider the followin code:
>
> #include <iostream>
> using std::cout;
> using std::endl;
> #include <cstdlib>
>
> void someFunction(int [], int);
>
> int main()
> {
> const int arraySize =3D 10;
> int a[arraySize] =3D {32, 27, 64, 18, 95, 14, 90, 70, 60, 37};
>
> cout << "Array values are:" << endl;
> someFunction(a, arraySize); // the array is printed in reversal order
> cout << endl;
>
> system("PAUSE");
> return 0;
> }
>
> 1-void someFunction(int b[], int size)
> 2-{
> 3- if(size > 0)
> 4- {
> 5- someFunction(&b[1], size-1);
> 6- cout << b[0] << " ";
> 7- }
> 8-}
PWM: This one would (try to) emit:
cout << a[1] << " "; (call stack now have 10 levels of someFunction)
cout << a[2] << " ";
=2E..
cout << a[10] << " "; (now only one level of someFunction on the stack)
PWM: As you can see, you have an off-by-one error in the code.
PWM: Since this is a linear problem, it is inefficient to use a recursive
solution. Just use a for loop instead. Had it been a tree you wanted to
dump, then recursion would have been suitable.
>
> A) I allways worked with recursive functions that had an explict
> Basic Case, i.e., an "if" statement, and a return command;
> B) Here in this recursive function there isn=B4t a if/return command
> as you can see. I think when the calls arrive in the
> "someFunction(&b[9], size-9);", the cout in line 6 is executed;
>
> My Questions:
> 1) Is there any implicit basic case for recursive functions without an
> explicit if/return command?
> 2) How is the dynamic of this function? All the calls to someFunction are
> made and only after this the command of line 6 is made? In the last ca=
ll
> for the 10th element?
>
> Regards
> Andre
>
>
>
>
> -------------------------------------------------------
> This SF.net email is sponsored by: Etnus, makers of TotalView, The best
> thread debugger on the planet. Designed with thread debugging features
> you've never dreamed of, try TotalView 6 free at www.etnus.com.
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www.noicys.cjb.net/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|