Recursive Code

C

cplusplusquestion

The code likes:

Lets make this iterative, shall we?
void fun(Array a){
    for(int i=0; i<MAX; i++){
      ....
      if(a != -1) {
         a = -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 != -1) {
a = -1;
fun(a);
}
}
}
 
C

cplusplusquestion

The code likes:
Lets make this iterative, shall we?
void fun(Array a){
    for(int i=0; i<MAX; i++){
      ....
      if(a != -1) {
         a = -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 != -1) {
         a = -1;
         fun(a);
      }
    }

}


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

Daniel Pitts

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 != -1) {
a = -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 != -1) {
a = -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?
 
C

cplusplusquestion

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.
 
C

cplusplusquestion

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.
 
A

anon

joseph said:
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 != -1) {
a = -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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top