Results 1 to 4 of 4
  1. #1
    javabob is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default nested for loop question

    say i have the following nested for loops
    Java Code:
    for(x1 = 1; x1 < b; x1++)
        for(x2 = 1; x2 < b; x2++)
             for(x3 = 1; x3 < b; x3++)
                 y+=x1 * x2 * x3
    I want the user to input integers a and b, where there are 'a' nested for loops
    (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.

  2. #2
    bksamrat is offline Member
    Join Date
    May 2008
    Posts
    3
    Rep Power
    0

    Default

    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!!

  3. #3
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    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;
        }
    }

  4. #4
    javabob is offline Member
    Join Date
    May 2008
    Posts
    2
    Rep Power
    0

    Default

    Thank you very much for taking the time to help me. I had a similar approach but i couldn't get the finer details in even after hours of thinking. If you did all that in less than an hour, then you sir are a genious(at least from my point of view)

Similar Threads

  1. Question regarding foreach loop...
    By theonlywalks in forum New To Java
    Replies: 2
    Last Post: 03-15-2008, 06:15 AM
  2. Breaking from nested switch
    By javaplus in forum New To Java
    Replies: 3
    Last Post: 02-02-2008, 08:28 AM
  3. Nested loops?
    By gabriel in forum New To Java
    Replies: 4
    Last Post: 08-06-2007, 04:51 PM
  4. Nested For Loop
    By yuchuang in forum New To Java
    Replies: 1
    Last Post: 07-08-2007, 01:11 PM
  5. Nested Tags JSP
    By Marcus in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 06-25-2007, 05:42 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
  •