Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Recursive Code

Reply
Thread Tools

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/>
 
Reply With Quote
 
 
 
 
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);
}
}
}
 
Reply With Quote
 
 
 
 
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.
 
Reply With Quote
 
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.
 
Reply With Quote
 
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/>
 
Reply With Quote
 
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.


 
Reply With Quote
 
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.


 
Reply With Quote
 
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Embeding pipeline stages to a recursive adder tree code capitan VHDL 3 09-25-2011 09:39 AM
Recursive functions Vs Non-recursive functions - performance aspect vamsi C Programming 21 03-09-2009 10:53 PM
Two recursive calls inside of a recursive function n00m C++ 12 03-13-2008 03:18 PM
detect recursive C code Einar ?rn C Programming 6 11-10-2004 05:29 AM
detect recursive C code Einar ?rn C Programming 0 10-28-2004 02:18 PM



Advertisments