455,805 Members | 1,280 Online
Need help? Post your question and get tips & solutions from a community of 455,805 IT Pros & Developers. It's quick & easy.

# Find all vectors with sum equal of vector elements and elements not bigger of k witho

 P: 5 I want to find all vectors with sum equal of vector elements and elements not bigger of k but without using recursion. For example in number of vector elements n is 5, sum=10 and k=3 than, the output will be: 3 1 2 2 2 3 1 2 1 3 3 1 1 3 2 3 1 1 2 3 3 1 0 3 3 3 0 3 3 1 3 0 3 2 2 3 0 3 1 3 3 0 2 3 2 3 0 2 2 3 3 0 1 3 3 2 3 3 2 0 2 3 3 1 1 2 3 3 0 2 2 3 2 3 0 The source code with using recursion in Java is: Expand|Select|Wrap|Line Numbers private static void printVectors(int p[], int n) {           for (int i = 0; i < n; i++) {             System.out.print(p[i] + " ");         }         System.out.println();     }       private static void computeVectors(int[] n, int sum, int k, int k1, int i) {             if (sum == 0) {               printVectors(n, n.length);         } else if (i < n.length) {               for (int j = k; j >= 0; j--) {                   if (j <= k1) {                     n[i] = j;                     computeVectors(n, sum - j, sum - j, k1, i + 1);                 }             }         }     } My question is how to redesign computeVectors() function without using recursion? Apr 9 '13 #1