Thread: converting a for loop to a recursive function

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    2

    converting a for loop to a recursive function

    I am working on a problem that requires a nest for loop to be converted to a recursive function. Unfortunately, I am stuck and need guidance as to what i am doing wrong. I am going to provide just the code that i need help with instead of the entire program.

    Code:
    for (R1=1; R1 <+3, R1++){  //for loop
                printf (something);
    }
    // the recursive function
    void loopR1 (int R1, int max){
       
             if (R1 <= max){
                   printf (something);
             loopR1 (R1+1, max);
             }
               else{
                    return;
                    }
    when calling the recursive function in main i am using the following statement...

    loop r1(1,3)

    If this is not enough information, please let me know.

    Thank you

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Definitely not enough information. What are you stuck on and what guidance do you need? What specific problem(s) are you having? Have you tried the recursive function to see if it behaves identically to the loop version? At a quick glance, it seems you have the right idea with your translation.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    2
    Thank you for the reply. The recursive function only does not behave the same as the loop version. This is one of 3 loops that will be contained in the finished product. Once I can get the 1st recursive function to work correctly, I believe I will be able to get the other 2 loops converted without any issues. Here is the entire program. I have commented out the portions that I do not want to run while I am working on the recursion issue.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    
    int pay(int R1, int R2, int R3);  //defines function
    
    void loopR1 (int R1, int max);
    
    int main() {
    
        printf("Welcome to PA102\n");
    
    
       // int R2=0;   // intialize the value for the 2nd dice roll
       // int R3=0;   // intialize the value for the 3rd dice roll
    
    
    //loop to determine iterate all possible values for each variable
        loopR1 (1, 3);
           // for (R2=1; R2<=3; R2++){
             //   for (R3=1; R3<=3; R3++){
               // }
       // }
    
    
    } // end main
    
    void loopR1 (int R1, int max){
    
            int R2=0;
            int R3=0;
    
    
            if (R1 <= max){
    
             printf("%d%5d%5d    payoff is %5d\n", R1, R2, R3, pay(R1, R2, R3));
             loopR1 (R1+1, max);
            }
                else {
                    return;
                    }
    }
        
         //defines the pay function which takes the 3 values representing the dice rolls as paramaters and returns the payoff for each possible combination
    
    int pay (int R1, int R2, int R3){
    
        int payoff = R1;            // sets the value of payoff to R1
    
     // calculates payoff and sets up the rules that the rolls 1, 2 and 3 have to follow to determine the proper payoff
    
            if (R2 < R1) {
                payoff += R2;
    
              if (R3 < R2){
                    payoff += (2* R3);
                }
                    else {
                        if (R3< R1){
                       payoff += R3;
                    }
                }
            }
            else {
            if (R3 < R1){
                payoff +=R3;
                }
            }
            return payoff;
    
    }
    This is homework so I don't want a solution, I just want to know where I am off.

  4. #4
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by wildhare21 View Post
    The recursive function only does not behave the same as the loop version.
    What's different between the recursive function and the loop version? Note in the recursive function, you don't need the else return; since there aren't any lines of code after the else.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    So just to clarify:
    The purpose of this program is to iterate (or recurse rather) through all the possibilities of rolling 3 dice, and to print out the payoff for each combo? So you would be looking at a list of values for the dice like:
    Code:
    1, 1, 1
    1, 1, 2
    1, 1, 3
    1, 2, 1
    1, 2, 2
    1, 2, 3
    1, 3, 1
    1, 3, 2
    1, 3, 3
    2, 1, 1
    2, 1, 2
    2, 1, 3
    2, 2, 1
    2, 2, 2
    2, 2, 3
    2, 3, 1
    2, 3, 2
    2, 3, 3
    3, 1, 1
    3, 1, 2
    3, 1, 3
    3, 2, 1
    3, 2, 2
    3, 2, 3
    3, 3, 1
    3, 3, 2
    3, 3, 3
    If that's the case, then I will start you off with the following shell of a program I used to generate that list, leaving the juicy recursive logic for you.
    Code:
    #define DIE_MAX    3  // each die can have values of 1, 2 or 3
    
    
    void recurse_dice(int d1, int d2, int d3)
    {
        printf("%d, %d, %d\n", d1, d2, d3);
        // recursive calls/logic goes here
    }
    
    
    int main(void)
    {
        recurse_dice(1, 1, 1);  // start with each die having a value of 1
        return 0;  // you correctly declared main to return an int, now return one -- 0 means success
    }
    It's a basic backtracking problem, meaning when you "reach the end" of one path, you back up a step and "try the next path". As a hint, notice that, in the list above, when d3 reaches the max, you reset it to 1 and increase d2 by 1. Similarly, when d2 reaches the max (and d3 has reached the max), you reset them both to 1 and increase d1. Finally, when all 3 have reached the max, you are done.

    Start with adding the logic (a simple if statement) just to iterate through values of d3 (with d1 and d2 fixed at 1), to produce the output
    Code:
    1, 1, 1
    1, 1, 2
    1, 1, 3
    Then extend that (adding another if statement) to iterate through all combinations of d2 and d3, with d1 fixed at 1. Then add the last bit to iterate through all 3 dice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting recursive function to tail recursive
    By ajacobs365 in forum C Programming
    Replies: 1
    Last Post: 10-30-2011, 08:15 AM
  2. Replies: 1
    Last Post: 12-03-2010, 01:54 AM
  3. Make Recursive function 'Tail-Recursive'
    By dp2452 in forum C Programming
    Replies: 7
    Last Post: 12-04-2009, 10:13 AM
  4. transforming this loop into a recursive function question
    By transgalactic2 in forum C Programming
    Replies: 17
    Last Post: 01-22-2009, 05:11 AM
  5. How to change recursive loop to non recursive loop
    By ooosawaddee3 in forum C Programming
    Replies: 1
    Last Post: 06-24-2002, 08:15 AM