Velocity Reviews > C++ > Recursive Code

# Recursive Code

Daniel Pitts
Guest
Posts: n/a

 06-20-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>>> void fun(Array a){
>>> for(int i=0; i<MAX; i++){
>>> ....
>>> a = ...;
>>> fun(a);
>>> }
>>> }
>>> Here, "Array a" will updated each time and I need "Array a" keeps
>>> previous status, so dynamic allocation for "Array a" does not work.

>> What is your stop condition? It looks like infinite recursion to me..
>>

>
> The code likes:
>

Lets make this iterative, shall we?
> void fun(Array a){
> for(int i=0; i<MAX; i++){
> ....
> if(a[i] != -1) {
> a[i] = -1;

i = 0;
> }
> }
> }

There you go, problem solved.

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

cplusplusquestion@gmail.com
Guest
Posts: n/a

 06-20-2008
On Jun 19, 9:18*pm, Daniel Pitts
<(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> >>> void fun(Array a){
> >>> * *for(int i=0; i<MAX; i++){
> >>> * * * *....
> >>> * * * *a = ...;
> >>> * * * *fun(a);
> >>> * *}
> >>> }
> >>> Here, "Array a" will updated each time and I need "Array a" keeps
> >>> previous status, so dynamic allocation for "Array a" does not work.
> >> What is your stop condition? It looks like infinite recursion to me..

>
> > The code likes:

>
> Lets make this iterative, shall we?
>
> > void fun(Array a){
> > * * for(int i=0; i<MAX; i++){
> > * * * ....
> > * * * if(a[i] != -1) {
> > * * * * *a[i] = -1;

> * * * * * * i = 0;
> > * * * }
> > * * }
> > }

>
> There you go, problem solved.
>

It is different to:

void fun(Array a){
for(int i=0; i<MAX; i++){
....
if(a[i] != -1) {
a[i] = -1;
fun(a);
}
}
}

cplusplusquestion@gmail.com
Guest
Posts: n/a

 06-20-2008
> > > The code likes:
>
> > Lets make this iterative, shall we?

>
> > > void fun(Array a){
> > > * * for(int i=0; i<MAX; i++){
> > > * * * ....
> > > * * * if(a[i] != -1) {
> > > * * * * *a[i] = -1;

> > * * * * * * i = 0;
> > > * * * }
> > > * * }
> > > }

>
> > There you go, problem solved.

>
> It is different to:
>
> void fun(Array a){
> * * for(int i=0; i<MAX; i++){
> * * * ....
> * * * if(a[i] != -1) {
> * * * * *a[i] = -1;
> * * * * *fun(a);
> * * * }
> * * }
>
> }
>
>

I may not be right. Let me try this code again.

cplusplusquestion@gmail.com
Guest
Posts: n/a

 06-20-2008
On Jun 19, 9:18*pm, Daniel Pitts
<(E-Mail Removed)> wrote:
> (E-Mail Removed) wrote:
> >>> void fun(Array a){
> >>> * *for(int i=0; i<MAX; i++){
> >>> * * * *....
> >>> * * * *a = ...;
> >>> * * * *fun(a);
> >>> * *}
> >>> }
> >>> Here, "Array a" will updated each time and I need "Array a" keeps
> >>> previous status, so dynamic allocation for "Array a" does not work.
> >> What is your stop condition? It looks like infinite recursion to me..

>
> > The code likes:

>
> Lets make this iterative, shall we?
>
> > void fun(Array a){
> > * * for(int i=0; i<MAX; i++){
> > * * * ....
> > * * * if(a[i] != -1) {
> > * * * * *a[i] = -1;

> * * * * * * i = 0;
> > * * * }
> > * * }
> > }

>
> There you go, problem solved.
>
> --
> Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

Thanks! It is really good idea.

Daniel Pitts
Guest
Posts: n/a

 06-20-2008
(E-Mail Removed) wrote:
> On Jun 19, 9:18 pm, Daniel Pitts
> <(E-Mail Removed)> wrote:
>> (E-Mail Removed) wrote:
>>>>> void fun(Array a){
>>>>> for(int i=0; i<MAX; i++){
>>>>> ....
>>>>> a = ...;
>>>>> fun(a);
>>>>> }
>>>>> }
>>>>> Here, "Array a" will updated each time and I need "Array a" keeps
>>>>> previous status, so dynamic allocation for "Array a" does not work.
>>>> What is your stop condition? It looks like infinite recursion to me..
>>> The code likes:

>> Lets make this iterative, shall we?
>>
>>> void fun(Array a){
>>> for(int i=0; i<MAX; i++){
>>> ....
>>> if(a[i] != -1) {
>>> a[i] = -1;

>> i = 0;
>>> }
>>> }
>>> }

>> There you go, problem solved.
>>

>
> It is different to:
>
> void fun(Array a){
> for(int i=0; i<MAX; i++){
> ....
> if(a[i] != -1) {
> a[i] = -1;
> fun(a);
> }
> }
> }

The only difference that I can see is that it doesn't process "-1" so
many times. Without knowing more semantics of the application, I can't
tell you if that works or doesn't work. Can you enlighten us on what
this function is supposed to actually do?

--
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>

cplusplusquestion@gmail.com
Guest
Posts: n/a

 06-20-2008
>
> The only difference that I can see is that it doesn't process "-1" so
> many times. *Without knowing more semantics of the application, I can't
> tell you if that works or doesn't work. *Can you enlighten us on what
> this function is supposed to actually do?
>

Sorry that it is very difficult for me to explain my program, I try to
make it simple. However, there is one more loop there, like:

void fun(Array a){
for(int i=0; i<MAX1; i++){
....
for(int j=0; j<MAX2; j++){
if(a[j] != -1) {
a[j] = -1;
fun(a);
}
}
}
}

Any idea is appreciated.

cplusplusquestion@gmail.com
Guest
Posts: n/a

 06-20-2008
>
> The only difference that I can see is that it doesn't process "-1" so
> many times. *Without knowing more semantics of the application, I can't
> tell you if that works or doesn't work. *Can you enlighten us on what
> this function is supposed to actually do?
>

Sorry that it is very difficult for me to explain my program, I try to
make it simple. However, there is one more loop there, like:

void fun(Array a){
for(int i=0; i<MAX1; i++){
....
for(int j=0; j<MAX2; j++){
if(a[j] != -1) {
a[j] = -1;
fun(a);
}
}
}
}

Any idea is appreciated.

anon
Guest
Posts: n/a

 06-20-2008
joseph cook wrote:
>>> What is your stop condition? It looks like infinite recursion to me..

>> The code likes:
>>
>> void fun(Array a){
>> for(int i=0; i<MAX; i++){
>> ....
>> if(a[i] != -1) {
>> a[i] = -1;
>> fun(a);
>> }
>> }
>> }

>
> Well..that's interesting. It looks like fun(a) should be called 2^MAX
> - 1 times, but any one branch is only "MAX" deep.
>
> So, that would mean that if you are running out of stack (because max
>> 100, say) then this is only a small part of your problem, since your

> program will never complete trying to do 2^100 - 1 function calls.
> (At least not within a few quadrillion years)

lol maybe he is trying to solve the question to life, universe and
everything