# Creating For Loops Dynamically

• 11-19-2013, 06:59 AM
mynorka
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?
• 11-19-2013, 07:31 AM
chandrasekhar.melam
Re: Creating For Loops Dynamically
Quote:

Originally Posted by mynorka
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...
• 11-19-2013, 02:12 PM
Norm
Re: Creating For Loops Dynamically
Can you use arrays and recursion instead of individual parameters and loops?
• 11-19-2013, 02:55 PM
mynorka
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.
• 11-19-2013, 04:50 PM
Norm
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
• 11-19-2013, 07:10 PM
mynorka
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.
• 11-19-2013, 07:11 PM
mynorka
Re: Creating For Loops Dynamically
For example, if Method f had two parameters, x and why, then the pseudocode would be:
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:
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 + ")");       }   } }```
• 11-19-2013, 07:26 PM
jim829
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
• 11-19-2013, 07:34 PM
Norm
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"
• 11-19-2013, 08:22 PM
mynorka
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-19-2013, 08:45 PM
Norm
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.
• 11-19-2013, 08:47 PM
mynorka
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.
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);```
• 11-19-2013, 08:51 PM
mynorka
Re: Creating For Loops Dynamically
In this situation, what does the radix do?
• 11-19-2013, 08:54 PM
Norm
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
• 11-19-2013, 09:16 PM
mynorka
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)
• 11-19-2013, 09:54 PM
Norm
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.
• 11-20-2013, 08:49 AM
JosAH
Re: Creating For Loops Dynamically
Quote:

Originally Posted by Norm
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:

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
• 11-20-2013, 02:35 PM
jim829
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
• 11-20-2013, 02:42 PM
Norm
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.
• 11-20-2013, 03:08 PM
jim829
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