Results 1 to 4 of 4
Thread: nested for loop question
- 05-20-2008, 06:49 AM #1
Member
- Join Date
- May 2008
- Posts
- 2
- Rep Power
- 0
nested for loop question
say i have the following nested for loops
I want the user to input integers a and b, where there are 'a' nested for loopsJava Code:for(x1 = 1; x1 < b; x1++) for(x2 = 1; x2 < b; x2++) for(x3 = 1; x3 < b; x3++) y+=x1 * x2 * x3
(goes up to xa) and then output an integer y, In the above example a = 3 , since I won't know what a and b are beforehand I can't hand write the for loops. I tried using recursion but still can't solve the problem after working for several hours and I dont even know if that's the best way to go. Can somebody at least show me a correct first step to solving this problem?Last edited by javabob; 05-20-2008 at 06:58 AM.
- 05-20-2008, 04:52 PM #2
Member
- Join Date
- May 2008
- Posts
- 3
- Rep Power
- 0
it shud surly work wit recursion!! if I wer in ur place I ud do it wit recursin function!! put som mor time.. that's the best way!!
- 05-20-2008, 10:13 PM #3
Java Code:import java.util.Arrays; public class Test { public static void main(String[] args) { System.out.printf("%3s %3s %3s %5s %3s%n", "x1", "x2", "x3", "next", "y"); int a = 3; int b = 4; int x1, x2, x3, y = 0; for(x1 = 1; x1 < b; x1++) { for(x2 = 1; x2 < b; x2++) { for(x3 = 1; x3 < b; x3++) { int next = x1 * x2 * x3; y += next; System.out.printf("%2d %2d %2d %3d %4d%n", x1, x2, x3, next, y); } } } System.out.printf("desired y = %d%n", y); y = 0; int[] counts = new int[a]; Arrays.fill(counts, 1); int index = counts.length-1; int lastCount = counts.length-1; boolean more = true; do { int product = 1; for(int i = 0; i < counts.length; i++) { product *= counts[i]; } y += product; System.out.printf("index = %d counts = %s product = %2d y = %3d%n", index, Arrays.toString(counts), product, y); more = !isDone(counts, b-1); if(counts[lastCount] == b-1) { index = shiftUp(counts, index, b-1); } else { counts[lastCount]++; } } while(more); System.out.printf("loop y = %d%n", y); } private static int shiftUp(int[] counters, int index, int max) { int nextIndex = counters.length-1; while(counters[nextIndex] == max && nextIndex > 0) { counters[nextIndex] = 1; nextIndex--; } for(int i = nextIndex+1; i < counters.length; i++) { counters[i] = 1; } counters[nextIndex]++; return nextIndex; } private static boolean isDone(int[] counters, int limit) { for(int i = 0; i < counters.length; i++) { if(counters[i] < limit) return false; } return true; } }
- 05-20-2008, 11:00 PM #4
Member
- Join Date
- May 2008
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Question regarding foreach loop...
By theonlywalks in forum New To JavaReplies: 2Last Post: 03-15-2008, 06:15 AM -
Breaking from nested switch
By javaplus in forum New To JavaReplies: 3Last Post: 02-02-2008, 08:28 AM -
Nested loops?
By gabriel in forum New To JavaReplies: 4Last Post: 08-06-2007, 04:51 PM -
Nested For Loop
By yuchuang in forum New To JavaReplies: 1Last Post: 07-08-2007, 01:11 PM -
Nested Tags JSP
By Marcus in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 06-25-2007, 05:42 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks