RE: [Dev-C++] Basic Case in recursivity
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Ioannis V. <iv...@em...> - 2003-06-09 10:30:24
|
> -----Original Message-----
> From: dev...@li...=20
> [mailto:dev...@li...] On Behalf=20
> Of Andr? Macario Barros
> Sent: Monday, June 09, 2003 4:11 AM
> To: Dev-Cpp-Users
> Subject: [Dev-C++] Basic Case in recursivity
>=20
>=20
> Dear Users,
> Please consider the followin code:
>=20
> #include <iostream>
> using std::cout;
> using std::endl;
> #include <cstdlib>
>=20
> void someFunction(int [], int);
>=20
> int main()
> {
> const int arraySize =3D 10;
> int a[arraySize] =3D {32, 27, 64, 18, 95, 14, 90, 70, 60, 37};
>=20
> cout << "Array values are:" << endl;
> someFunction(a, arraySize); // the array is printed in=20
> reversal order
> cout << endl;
>=20
> system("PAUSE");
> return 0;
> }
>=20
> 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-}
>=20
> 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;
>=20
> My Questions:
> 1) Is there any implicit basic case for recursive functions without an
> explicit if/return command?
What do you mean. A recursive function is one that calls itself
recursivelly, no matter if it returns a value or has a if statement
(although it must have a termination check condition unavoidably).=20
> 2) How is the dynamic of this function? All the calls to=20
> someFunction are
> made and only after this the command of line 6 is made? In=20
> the last call
> for the 10th element?
Yes. And ithen t prints the "b[0]" which is the last element towards the
first (the b[0] of last call is executed, then from the one before the =
last
and so on).
The dynamic is stupid. It can be implemented using a loop (=3Dmore =
run-time
efficiently and less stack memory consuming).
Ioannis Vranos
=20
* Programming pages: http://www.noicys.freeurl.com
* Alternative URL 1: http://run.to/noicys
* Alternative URL 2: http://www.noicys.cjb.net
|