Results 1 to 20 of 24
Thread: Nested for loops
 10162010, 02:24 AM #1Member
 Join Date
 Sep 2010
 Posts
 62
 Rep Power
 0
Nested for loops
Hi,
I have to display a diamond shape using asterisks. At the beginning I start with four ' ' (single space) followed by one '*' and again four ' ' symbol. The second line is four minus one ' ' followed by one plus two '*' and again four minus one ' ' and so on.
Java Code:static void printDiamond() { for( int i = 1; i <= 4; i++ ) System.out.print(' '); System.out.print('*'); for( int i = 1; i <= 4; i++ ) System.out.print(' '); System.out.println(); for( int i = 1; i <= 3; i++ ) System.out.print(' '); for( int i = 1; i <= 3; i++ ) System.out.print('*'); for( int i = 1; i <= 3; i++ ) System.out.print(' '); System.out.println(); for( int i = 1; i <= 2; i++ ) System.out.print(' '); for( int i = 1; i <= 5; i++ ) System.out.print('*'); for( int i = 1; i <= 2; i++ ) System.out.print(' '); and so on (I am not pasting the whole code since it is going to be too much lines) }
I tried to do this by using nested for loops but I couldn't make the diamond shape. That's why I am using for loops one after another.
The idea is that I want to use more for loops and less output statements.
Any hints are more than welcome.
 10162010, 02:34 AM #2Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 7
I would sketch how this would look on paper first, including println logic
 10162010, 02:46 AM #3Member
 Join Date
 Sep 2010
 Posts
 62
 Rep Power
 0
 10162010, 02:51 AM #4Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 7
looks like the thickest part of the diamond is nine char's long and the height is 9 chars long. that should be a big clue
assume chars are * .
****'*'****
centre in any row is always the 5th char '*' and char length is always 9
I bet you can get it from here...
 10162010, 03:17 AM #5Member
 Join Date
 Sep 2010
 Posts
 62
 Rep Power
 0
 10162010, 03:21 AM #6Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 7
I am actually having a go at it myself as we speak, good challenge! I misread your post originally, sorry, will post any findings.
 10162010, 03:48 AM #7Member
 Join Date
 Sep 2010
 Posts
 62
 Rep Power
 0
Last edited by luke; 10162010 at 03:52 AM.

 10162010, 04:14 AM #9Member
 Join Date
 Sep 2010
 Posts
 62
 Rep Power
 0
Java Code:static void printDiamond() { for( int i = 4; i >= 0; i ) { System.out.print(' '); for( int j = 1; j <= 9; j += 2 ) { System.out.print('*'); for( int k = 4; k >= 0; k) { System.out.print(' '); } } } }
Thanks, Fubarable.
I recall one of the previous attempts:
Java Code:for( int i = 4; i >= 0; i ) { for( int j = 1; j <= 9; j += 2 ) { for( int k = 4; k >= 0; k) { System.out.print(' '); System.out.print('*'); System.out.print(' '); System.out.println(); } } }
Last edited by luke; 10162010 at 04:19 AM.
 10162010, 04:33 AM #10Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 7
I have got it down to 4 prints, just ironing out an array index bug I cant find, I got the diamond printing

Use Math.abs and you can get it down to just two System.out.prints and one System.out.println. No need for arrays, just some basic math.
 10162010, 04:49 AM #12Member
 Join Date
 Sep 2010
 Posts
 62
 Rep Power
 0
 10162010, 04:51 AM #13Member
 Join Date
 Sep 2010
 Posts
 62
 Rep Power
 0

Hint: The key is to count the left spaces and the stars and try to figure out formulas:
Java Code:i spaces stars ===================================== 0 4 1 1 3 3 2 2 5 3 1 7 4 0 9 5 1 7 6 2 5 7 3 3 8 4 1
Last edited by Fubarable; 10162010 at 05:04 AM.
 10162010, 04:58 AM #15Senior Member
 Join Date
 Feb 2010
 Location
 Waterford, Ireland
 Posts
 748
 Rep Power
 7
 10172010, 05:42 AM #16Member
 Join Date
 Sep 2010
 Posts
 62
 Rep Power
 0
Fubarable, thanks a lot for your hints. I've been working on this for a couple of hours. I've made some progress but it's not enough. This is what I've got so far:
Java Code:static void printDiamond() { for( int i = 0; i < 9; i++ ){ for( int j = Math.abs(i  4); j > 0; j = 1) System.out.print(' '); for( int k = 2 * i + 1; k >= i; k ){ System.out.print('*'); } System.out.println(); } }
but the result is
If you can possibly give me some more hints because I am stuck...

You're close.
If you look at my "hint" table again, you'll see a relationship between spaces and stars 
namely: (2 * spaces) + stars = what?
So, inside your outer for loop, just before your inner for loop, calculate spaceMax and use it in the for loop, then use this number and MAX (9) to calculate starMax:
Java Code:public class Fu2 { private static final int MAX = 9; public static void main(String[] args) { for (int i = 0; i < MAX; i++) { int spaceMax = Math.abs(MAX/2  i); for (int j = 0; j < spaceMax; j++) { System.out.print(" "); } int starMax = // [color="red"]*** use MAX and spaceMax to calculate this ***[/color] for (int j = 0; j < starMax; j++) { System.out.print("*"); } System.out.println(""); } } }
Having said all this, you really don't need to use Math.abs, but instead could do all this with a simple if condition, but I think the Math.abs is cooler, and makes it easier to be able to create a diamond of any size.
Luck!
 10172010, 07:59 PM #18Member
 Join Date
 Sep 2010
 Posts
 62
 Rep Power
 0
Thanks again for your help, Fubarable. Without your assistance I wouldn't be able to do it.
When I started this problem I thought it's easy. But eventually turned out to be challenging (at least to me).
The solution with Math.abs is very cool. Nice, short and flexible code :) Thank you.
 10182010, 03:49 AM #19Member
 Join Date
 Sep 2010
 Posts
 62
 Rep Power
 0
Fubarable, even though my problem is solved there is something that still confuses me. 2 * spaces + stars = middle line or MAX. I used this formula to get starMax. I thought you used the same formula to get spaceMax. But actuallyJava Code:spaceMax = Math.abs(MAX/2  i);
Java Code:spaceMax = Math.abs(MAX  i / 2);
But how did you find that the correct isJava Code:spaceMax = Math.abs(MAX/2  i);
Thanks in advance.

It's all about generating equations from patterns. If we linearize the data by making it a straight line without a "reflection" like so:
Java Code:x y =================== 0 4 1 3 2 2 3 1 4 0 5 1 6 2 7 3 8 4
y = x + 4
To make all negatives positive, I must take the absolute value of the right side:
y = 4  x
Substituting i for x and spaceMax for y and using the Java Math method for obtaining absolute value gives:
Java Code:spaceMax = Math.abs(C  i);
Now when we play around with different values of MAX and check to see how this affects C we get:
Java Code:MAX C =================== 3 1 5 2 7 3 9 4 11 5 13 6
but since int division tosses out any remainder, in Java, C is nothing more than MAX / 2
Similar Threads

Nested Loops Study Help Needed
By gysnewbie in forum New To JavaReplies: 5Last Post: 07122010, 08:14 AM 
Nested Loops for Java
By soccer_kid_6 in forum New To JavaReplies: 4Last Post: 02212010, 06:27 AM 
nested for loops
By Implode in forum New To JavaReplies: 4Last Post: 09012009, 08:47 AM 
Nested Loops
By ks1615 in forum New To JavaReplies: 4Last Post: 02182009, 03:48 AM 
Nested loops?
By gabriel in forum New To JavaReplies: 4Last Post: 08062007, 04:51 PM
Bookmarks