Results 1 to 6 of 6
  1. #1
    mercyfulfate is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default Help needed to turn a recursive method into iterative

    Hi there! I'm in need of a bit of help, I'm going crazy with some Java practice I have to do.

    I need to turn this recursive method (cal):

    public class Recursivo {

    private int valores[] = {12, 13, 25, 34, 56, 75, 16, 78, 11, 65, 89};

    public int cal (int a, int b)
    {
    int i = (a+b)/2;
    if (a<=b) {
    return (valores[i]%2) + cal(a, i-1) + cal(i+1, b);
    }
    else {
    return 0;
    }
    }
    }

    into a non recursive (iterative) method that does the same thing.

    Anyone can spare some time to help me out?

    Thanks!

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,253
    Rep Power
    25

    Default

    Do you have a description of what the app does?

  3. #3
    mercyfulfate is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    Nope, sorry. I was given that code as it is and asked to change it so it does the same thing without using recursivity :(

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,253
    Rep Power
    25

    Default

    A description of what it does could help you find another way to code it.

  5. #5
    mercyfulfate is offline Member
    Join Date
    Jul 2010
    Posts
    3
    Rep Power
    0

    Default

    Believe me or not, you just made me solve the problem :D

    I tried to understand the purpose of that method for the n-th time and finally found the reasoning behind it (it returns how many odd numbers are stored in the array between 2 given positions (the method params a & b)).

    So...

    public int cal2 (int a, int b)
    {
    int total = 0;

    if (a<=b){
    for (int i=a;i<=b;i++){
    total+= valores[i]%2;
    }
    return total;
    }
    else
    return 0;
    }

    Thanks a lot! :D

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,253
    Rep Power
    25

Similar Threads

  1. Creating a 'turn' method
    By StateofMind in forum New To Java
    Replies: 6
    Last Post: 11-27-2012, 10:36 AM
  2. Turning Recursion Method into Iterative method
    By mattakuevan in forum New To Java
    Replies: 9
    Last Post: 06-15-2010, 06:46 AM
  3. Replies: 3
    Last Post: 02-09-2010, 05:22 AM
  4. Recursive method using int array, help needed
    By chupalo17 in forum New To Java
    Replies: 4
    Last Post: 09-07-2009, 11:15 PM
  5. Recursive Method
    By bluegreen7hi in forum New To Java
    Replies: 5
    Last Post: 11-29-2007, 04:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •