Results 1 to 20 of 20
Like Tree1Likes
  • 1 Post By JosAH

Thread: Creating For Loops Dynamically

  1. #1
    mynorka is offline Member
    Join Date
    Nov 2013
    Posts
    10
    Rep Power
    0

    Default Creating For Loops Dynamically

    Hello all! :)

    I'm writing a program that will essentially display points that could be plotted on a graph. These points are generated based on how many parameters I pass to my method. In my method, I'm passing a Method that contains a certain number of parameters, and the number of parameters in that Method determines how many times I will loop.

    For example: If I have Method f with parameters x, y, z. I want to print the following output
    (1,1,1)
    (1,1,2)
    (1,1,3)
    .
    .
    .
    (5,5,5)

    And if the method I pass has four parameters, say x, y, z, w, I want the this output
    (1,1,1,1)
    (1,1,1,2)
    (1,1,1,3)
    .
    .
    .
    (5,5,5,5)

    To print these values out, I know I'll need nested for loops, but the number of for loops I need depends on how many parameters. Is there a way to dynamically create the necessary amount of loops based on how many parameters I have?

  2. #2
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: Creating For Loops Dynamically

    Quote Originally Posted by mynorka View Post
    Hello all! :)

    I'm writing a program that will essentially display points that could be plotted on a graph. These points are generated based on how many parameters I pass to my method. In my method, I'm passing a Method that contains a certain number of parameters, and the number of parameters in that Method determines how many times I will loop.

    For example: If I have Method f with parameters x, y, z. I want to print the following output
    (1,1,1)
    (1,1,2)
    (1,1,3)
    .
    .
    .
    (5,5,5)

    And if the method I pass has four parameters, say x, y, z, w, I want the this output
    (1,1,1,1)
    (1,1,1,2)
    (1,1,1,3)
    .
    .
    .
    (5,5,5,5)

    To print these values out, I know I'll need nested for loops, but the number of for loops I need depends on how many parameters. Is there a way to dynamically create the necessary amount of loops based on how many parameters I have?
    i think instead of passing variables you can use range variable to create your format...

  3. #3
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Creating For Loops Dynamically

    Can you use arrays and recursion instead of individual parameters and loops?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    mynorka is offline Member
    Join Date
    Nov 2013
    Posts
    10
    Rep Power
    0

    Default Re: Creating For Loops Dynamically

    I can use a simple int array to represent the parameters, but I'm unsure of how I could use recursion to loop the number of times I need to.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Creating For Loops Dynamically

    Here's an idea: treat the digits to be printed as digits of a base 6 number that ranges in value from 111... to 555....
    Start the number with the requires number of 1s (for example 5 would be 11111) split out the digits and print them,
    add 1 to the number (base 6) and add 1 if overflow and do it again. For example 11115 + 1 is 11121
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    mynorka is offline Member
    Join Date
    Nov 2013
    Posts
    10
    Rep Power
    0

    Default Re: Creating For Loops Dynamically

    Perhaps I should have been more specific about my problem. I am to write a method that takes four arguments. The signature is below:

    public static void printTable(double from, double to, int n, Method f);

    Method f can be any mathematical function, and the variables from and to represent limits on how many times my outermost loop will loop.
    For each parameter in f, I must print out its value, starting at from, and ending at to.

    For example, if Method f had two parameters, x and why, then the pseudocode would be:

    for(int i = from; i <= to; i++){
    for(int j = from; j <= to; j++){
    System.out.println("("+i+", "+j+")");
    }
    }

    And if Method f had three parameters, x y and z, then it would look like this:

    for(int i = from; i <= to; i++){
    for(int j = from; j <= to; j++){
    for(int k = from; k <= to; k++){
    System.out.println("(" + i + ", " + j + ", " + k + ")");
    }
    }
    }

    I need to know how I can determine the number of times I loop based on the number of parameters Method f has. That, or if there is another solution.

  7. #7
    mynorka is offline Member
    Join Date
    Nov 2013
    Posts
    10
    Rep Power
    0

    Default Re: Creating For Loops Dynamically

    For example, if Method f had two parameters, x and why, then the pseudocode would be:
    Java Code:
    for(int i = from; i <= to; i++){
       for(int j = from; j <= to; j++){ 
          System.out.println("("+i+", "+j+")"); 
       }
    }
    And if Method f had three parameters, x y and z, then it would look like this:
    Java Code:
    for(int i = from; i <= to; i++){
       for(int j = from; j <= to; j++){
          for(int k = from; k <= to; k++){
             System.out.println("(" + i + ", " + j + ", " + k + ")"); 
          }
       }
    }

  8. #8
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,379
    Rep Power
    5

    Default Re: Creating For Loops Dynamically

    You only need two loops and one array. As Norm said, use a cascading effect. This is easiest by initializing an array of all ones and iterating thru it backward.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Creating For Loops Dynamically

    Given a number as a String: "11111111" the 1s can be split out to 1,1,1,1,1,1,1
    convert that number to an int, increment by 1 and convert to String using radix 6 gives: 11111112 which can be split as above
    continue incrementing by 1 until String is "5555555"
    That can be done in a single for loop that increments the initial number: "1111111" by one and ends when at "5555555"
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    mynorka is offline Member
    Join Date
    Nov 2013
    Posts
    10
    Rep Power
    0

    Default Re: Creating For Loops Dynamically

    With the string.split() method, I get an array of Strings. How can I convert that to an integer?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Creating For Loops Dynamically

    What does the split() method do that you need? Given the String: "1111111", how would you use split()?
    Do you want to get the digit characters one at a time?

    See the Integer class for the methods to convert ints to String in a selected radix/base.
    If you don't understand my response, don't ignore it, ask a question.

  12. #12
    mynorka is offline Member
    Join Date
    Nov 2013
    Posts
    10
    Rep Power
    0

    Default Re: Creating For Loops Dynamically

    This is what I got so far. I'm just confused on how to loop through this integer. I found that I have to iterate through this loop n*n times in order to print each possible value for all parameters from FROM to TO.
    Java Code:
    		Class[] params = f.getParameterTypes(); 
    		int[] countArray = new int[params.length]; double delta = (to - from) / (n - 1); 
    		String string = ""; 
    		
    		for(int i = 0; i < countArray.length; i++){
    			countArray[i] = 1;
    			string+=countArray[i] + " ";
    		}
    		
    		String[] splitString = string.split(" ");
    		String newString = ""; 
    		
    		for(int j = 0; j < splitString.length; j++)
    			newString+=splitString[j];
    		
    		int newInt = Integer.parseInt(newString);

  13. #13
    mynorka is offline Member
    Join Date
    Nov 2013
    Posts
    10
    Rep Power
    0

    Default Re: Creating For Loops Dynamically

    In this situation, what does the radix do?

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Creating For Loops Dynamically

    Do a Search for number radix for a better description.
    int variables hold their values in radix 2. When numbers are displayed for human viewing, the int value is converted to radix 10.
    In your problem you want to see the int converted to radix 6 (the valid digits are 0 to 5).

    A pseudo code example:
    for(int nbr = 111base6; nbr <= 555base6; nbr++) {
    // convert nbr to base 6 String
    // get each digit from the String
    // output the separated digits
    }

    I use base and radix the same
    Last edited by Norm; 11-19-2013 at 08:01 PM.
    If you don't understand my response, don't ignore it, ask a question.

  15. #15
    mynorka is offline Member
    Join Date
    Nov 2013
    Posts
    10
    Rep Power
    0

    Default Re: Creating For Loops Dynamically

    I forgot something else in the problem, I'm sorry :/

    The values I will be testing for the printTable() method are:

    printTable(3, 6, 4, f1);
    printTable(1, 10, 10, f2);
    printTable(5, 9, 5, f3);

    In the second run through, I have to print out values:
    (1,1,1,1)
    (1,1,1,2)
    (1,1,1,3)
    .
    .
    .
    (10,10,10,10)

  16. #16
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Creating For Loops Dynamically

    That looks like the radix to use is 11 so you get the digit characters from o to a. The a is 10 in hex that you will have to manually test for and change.
    If you don't understand my response, don't ignore it, ask a question.

  17. #17
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,337
    Blog Entries
    7
    Rep Power
    20

    Default Re: Creating For Loops Dynamically

    Quote Originally Posted by Norm View Post
    Do a Search for number radix for a better description.
    int variables hold their values in radix 2. When numbers are displayed for human viewing, the int value is converted to radix 10.
    In your problem you want to see the int converted to radix 6 (the valid digits are 0 to 5).

    A pseudo code example:
    for(int nbr = 111base6; nbr <= 555base6; nbr++) {
    // convert nbr to base 6 String
    // get each digit from the String
    // output the separated digits
    }

    I use base and radix the same
    Although it works, this solution doesn't scale very well; better count each 'variable' separately:

    Java Code:
    boolean next(int[] vars, int[] uppers) {
       for (int i= vars.length; i-- > 0;)
          if (++vars[i] <= uppers[i]) return true;
          else vars[i]= 0;
       return false;
    }
    The array vars contains the next 'loop counters' and variable uppers contains the upper bounds; the method returns false if all vars have reached their upper bounds; the counting works exactly as in your method but it scales better ...

    kind regards,

    Jos
    Norm likes this.
    cenosillicaphobia: the fear for an empty beer glass

  18. #18
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,379
    Rep Power
    5

    Default Re: Creating For Loops Dynamically

    It wasn't clear from the OP's post as to whether each value should start from 0 or 1. I assumed 1. Easy fix in any case. And you could even make it more flexible by specifying "lowers" for lower bounds.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  19. #19
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: Creating For Loops Dynamically

    If all the upper bounds were all the same and the lower bounds were the same, there wouldn't be a need for arrays.
    Last edited by Norm; 11-20-2013 at 02:29 PM. Reason: Tried to clarify
    If you don't understand my response, don't ignore it, ask a question.

  20. #20
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    3,379
    Rep Power
    5

    Default Re: Creating For Loops Dynamically

    Are you responding to my comment? I can' tell anymore in this thread. In any event to be clear, I never said they would be the same, just that you could specify the lower bounds. So the first variable could start at 2 and go to 10, the second could start a 1 and go to 5, etc, etc.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Creating Objects within While Loops
    By Rocketz in forum New To Java
    Replies: 1
    Last Post: 11-15-2011, 12:19 AM
  2. Replies: 6
    Last Post: 09-12-2011, 02:27 PM
  3. Creating files using loops
    By lakshmibvaraprasad in forum New To Java
    Replies: 2
    Last Post: 06-07-2011, 05:09 PM
  4. HELP - Creating OPTIONS in program and LOOPS
    By whateverme in forum New To Java
    Replies: 14
    Last Post: 12-07-2010, 05:55 AM
  5. Creating Jbuttons, etc. dynamically
    By JohnST in forum New To Java
    Replies: 2
    Last Post: 01-30-2010, 04:08 PM

Posting Permissions

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