12 Replies - 345 Views - Last Post: 10 February 2014 - 09:35 PM Rate Topic: -----

#1 proprsk8  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 10-November 13

Looping Recursive Functions

Posted 10 February 2014 - 04:30 PM

Hi, I need help trying to figure out how to recursively modify my program. The problem I'm hacing is the the program is not looping correctly and also not printing the correct number. I've calculated the payoff correctly, also I've only been able to print the first section of R3. I can't figure out how to loop it to get R2 to stay at 2 then go to 3 after all possiblities of R2 at 2. Enventually, R1 will change to 2 then 3; 3 being the highest number earned. To be mentioned that will be three recursive function loopR1, loopR2, and loopR3 for each column.

The result of the program should look like:

R1 R2 R3
1 1 1 payoff is 1
1 1 2 .......... 1
1 1 3 .......... 1
1 2 1 .......... 1
1 2 2 .......... 1
...
...
...
3 3 2 .............. 5
this is what I have so far:
#include <stdio.h>
#include <stdlib.h>

int payOff(int r1, int r2, int r3);
void loopR3(int R3, int upto);
void loopR2(int R2, int upto);

int main(void)
{

    loopR3(1,3);
    loopR2(1,3);

    return 0;
}

/*DID NOT INCLUDE PAYOFF AS IT IS IRRELEVENT FOR THE QUESTION*/

void loopR3(int R3, int upto)
{
   int R1 =1;
   int R2 =1;

   if (R3 > upto){
     return loopR2(R2, upto);
   }//end if
   else{
     printf("%d\t %d\t %d\t payoff is %d\n", R1, R2, R3, payOff(R1,R2, R3));
     loopR3(R3+1, upto);
   }//end else
}

void loopR2(int R2, int upto)
{
   if (R2<=upto){
     loopR3(R2+1,upto);
     printf("R2 is %d \n", R2);
   }//end if
}



Is This A Good Question/Topic? 0
  • +

Replies To: Looping Recursive Functions

#2 GWatt  Icon User is offline

  • member icon

Reputation: 278
  • View blog
  • Posts: 3,078
  • Joined: 01-December 05

Re: Looping Recursive Functions

Posted 10 February 2014 - 05:47 PM

It looks like you are expecting the loopR2 function to alter the value of loopR3's R2 variable, which will not happen. when you call loopR2 maybe you could return the desired value and assign that to the R2 variable in loopR3.
Was This Post Helpful? 0
  • +
  • -

#3 proprsk8  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 10-November 13

Re: Looping Recursive Functions

Posted 10 February 2014 - 06:26 PM

Unfortunately, I don't think I can do it that as R2 in the column must change to 2 when R3 column reaches 3 then r2 changes to 3 when r3 reaches 3 again. My problem is that R2 doesn't change when called.

This post has been edited by Skydiver: 10 February 2014 - 09:03 PM
Reason for edit:: No need to quote the message directly above yours.

Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,291
  • Joined: 05-May 12

Re: Looping Recursive Functions

Posted 10 February 2014 - 06:40 PM

I'm surprised that line 25 even compiles.
Was This Post Helpful? 0
  • +
  • -

#5 proprsk8  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 10-November 13

Re: Looping Recursive Functions

Posted 10 February 2014 - 06:48 PM

Hah actually, it does but it ends up as a infinite loop. I forgot to update the code. That's not what I have there now.
This is what I have now:
void loopR3(int R3, int upto)
{
    int R1 =1;
    int R2 =1;
    if (R3 > upto){
       loopR2(R2, upto);
    }//end if
    else{
        printf("%d\t %d\t %d\t payoff is %d\n", R1, R2, R3, payOff(R1,R2, R3));
        loopR3(R3+1, upto);
        }//end else
}



I just removed the return. I've been messing with loopR2 to try and figure out how to printout a 2 for the second section of R2.

This post has been edited by Skydiver: 10 February 2014 - 09:02 PM
Reason for edit:: No need to quote the message directly above yours.

Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1086
  • View blog
  • Posts: 4,564
  • Joined: 09-June 09

Re: Looping Recursive Functions

Posted 10 February 2014 - 07:15 PM

void loopR3(int R3, int upto)
{
    int R1 =1;
    int R2 =1;
    if (R3 > upto){
       loopR2(R2, upto);
    }//end if
    else{
        printf("%d\t %d\t %d\t payoff is %d\n", R1, R2, R3, payOff(R1,R2, R3));
        loopR3(R3+1, upto);
        }//end else
}




Thats infinite recursion. Every recurrance algorithm involves a recursive case, and a end case. You don't have any end case in your algorithm.

An end case is a condition which the recursive case is not executed.


Didn't realize your end case was in loopR2

This post has been edited by jjl: 10 February 2014 - 08:24 PM

Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,291
  • Joined: 05-May 12

Re: Looping Recursive Functions

Posted 10 February 2014 - 07:15 PM

Is there any particular reason why you are using tail recursion to simulate loops?
Was This Post Helpful? 0
  • +
  • -

#8 proprsk8  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 10-November 13

Re: Looping Recursive Functions

Posted 10 February 2014 - 08:02 PM

I'm not what you mean, but I'm using psuedocode a friend showed me to help me out. To be honest, it made thing more difficult.

If you would like to see the psuedocode here is it is:

function for the third column:

if you've encountered the base case of the current value being larger than the max number
return back to the function for the second column
else
call the payoff function with all three column values
recursively call this function incremented one

This post has been edited by Skydiver: 10 February 2014 - 09:01 PM
Reason for edit:: No need to quote message directly above yours.

Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,291
  • Joined: 05-May 12

Re: Looping Recursive Functions

Posted 10 February 2014 - 08:40 PM

It could have been so much easier if you didn't use recursion and simply used nested for loops. In pseudo code:
for(r1 = 1; r1 <= 3; r1++)
{
    for(r2 = 1; r2 <= 3; r2++)
    {
        for(r3 = 1; r3 <= 3; r3++)
        {
            print values for r1, r2, r3
            compute payout using r1, r2, r3
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#10 proprsk8  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 10-November 13

Re: Looping Recursive Functions

Posted 10 February 2014 - 08:56 PM

That is true, actually out of practice I did it this way but I'm reading up on recursive function and this was one of the exercises. so basically what I'm trying to do is convert this exact nested for loops into recursive functions. :)/>
Thank you for helping so far! Talking it out is helping a little, it's helping my mind think of different ways of doing this. no success though.

This post has been edited by Skydiver: 10 February 2014 - 09:01 PM
Reason for edit:: No need to quote the message directly above yours.

Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,291
  • Joined: 05-May 12

Re: Looping Recursive Functions

Posted 10 February 2014 - 09:15 PM

If you want to use recursion, consider this more readable version of doing permutations. (I'm sorry for the fans of the version offered by Dijsktra -- I could never quite grok his solution.)

void Permute(int depth, int values[MAX_DEPTH])
{
    if (depth >= MAX_DEPTH)
    {
        PrintPermutation(values, MAX_DEPTH);
        return;
    }

    for(int i = MIN_VALUE; i <= MAX_VALUE; i++)
    {
        values[depth] = i;
        Permute(depth + 1, values);
    }
}

void PrintPermutation(int values[], int count)
{
    for(int i = 0; i < count; i++)
        print values[i];
}



You could replace PrintPermutation with your code to print out the permutations and compute the payout.
Was This Post Helpful? 1
  • +
  • -

#12 proprsk8  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 40
  • Joined: 10-November 13

Re: Looping Recursive Functions

Posted 10 February 2014 - 09:17 PM

So this is the simplest thing i could've done so far just to test it out. I did get a good result the only thing is that the column for R2 which is the middle one stays at 1 where the last column is working just fine.

Here is what I did so far:
void loopR1(int R1, int upto)
{
    if (R1 <= upto){
        loopR2(R1,upto);
        R1++;
    }
}

void loopR2(int R2, int upto)
{
    if(R2 <= upto){
        loopR3(R2,upto);
        R2++;
    }
}

void loopR3(int R3, int upto)
{
    int R1 = 1;
    int R2 = 1;

    if(R3 <= upto){
        printf("%d\t %d\t %d\t payoff is %d\n",R1,R2,R3,payOff(R1,R2,R3));
        loopR3(R3+1,upto);
    }
}



I feel like the declaration of R1 and R2 in loopR3 might be the cause of this problem. However I'm not sure about how to go on to get 2 in the second column.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3623
  • View blog
  • Posts: 11,291
  • Joined: 05-May 12

Re: Looping Recursive Functions

Posted 10 February 2014 - 09:35 PM

Pass them as parameters...
void loopR1(int r1);
void loopR2(int r1, int r2);
void loopR3(int r1, int r2, int r3);



Obviously that solution doesn't scale very well as you get more and more loops. This is why my Permute() function above takes an array as well as indicator of how many elements of the array are active.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1