Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 05-20-2008, 08:49 AM
Member
 
Join Date: May 2008
Posts: 2
javabob is on a distinguished road
nested for loop question
say i have the following nested for loops
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 08:58 AM.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 05-20-2008, 06:52 PM
Member
 
Join Date: May 2008
Posts: 3
bksamrat is on a distinguished road
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!!
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 05-21-2008, 12:13 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,222
hardwired is on a distinguished road
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; } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 05-21-2008, 01:00 AM
Member
 
Join Date: May 2008
Posts: 2
javabob is on a distinguished road
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)
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question regarding foreach loop... theonlywalks New To Java 2 03-15-2008 08:15 AM
Breaking from nested switch javaplus New To Java 3 02-02-2008 10:28 AM
Nested loops? gabriel New To Java 4 08-06-2007 06:51 PM
Nested For Loop yuchuang New To Java 1 07-08-2007 03:11 PM
Nested Tags JSP Marcus JavaServer Pages (JSP) and JSTL 1 06-25-2007 07:42 AM


All times are GMT +3. The time now is 09:03 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org