Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,245 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,210 people online right now. Registration is fast and FREE... Join Now!




diamond

 
Reply to this topicStart new topic

diamond

tgrsnpr
14 Oct, 2008 - 04:09 AM
Post #1

New D.I.C Head
*

Joined: 10 Oct, 2008
Posts: 11

I can't get my diamond to look like this:
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
Mines look like this:
*
***
*****
*******
*********
**********
********
******
****
**
The top part I got right, but the bottom of the diamond is wrong, and I don't know why. But when I try to do even numbers it comes out perfectly. But odd numbers are wrong.
CODE

    int num = keybd.nextInt();


    for (int row = 0; row <= num/2; row++) {
      for (int col = 1; col <= (num/2) - row; col++)
        System.out.print(" ");
      for (int mid = 1; mid <= (row*2) - 1; mid++)
        System.out.print("*");
        System.out.println();
    }        
    
    for (int row = 1; row <= (num/2); row++) {
      for (int col = 1; col <= row - 1; col++)
        System.out.print(" ");
      for (int mid = 1; mid <= num - (row*2) + 1; mid++)
        System.out.print("*");
        System.out.println();
    }    
  }
}


User is offlineProfile CardPM
+Quote Post

Gloin
RE: Diamond
14 Oct, 2008 - 04:23 AM
Post #2

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
The problem is here:

for (int row = 1; row <= (num/2); row++) {

or more specifically here:

(num/2)

Java uses integer division which means that the remainder is thrown away when dividing 2 integers, I.e

8/2 = 9/2 = 4


User is offlineProfile CardPM
+Quote Post

Gloin
RE: Diamond
14 Oct, 2008 - 04:28 AM
Post #3

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
*
***
*****
*******
*********
***********
***********
*********
*******
*****
***
*

Is this the supposed result when using an even number?
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Diamond
14 Oct, 2008 - 06:26 AM
Post #4

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
It's time to pull some math tricks out your sleeve..

You really only need one (nested) for loop for this.
If you're supposed to draw a figure using n rows where the middle row(s) should consist of n *'s or whatever you want, it's simple to adapt.

First if you take a look at the upper half the figure, you can easily determine a function for the number of *

The first row has 1 * where 1 = 2 * 1 - 1
The second row has 3 * where 3 = 2 * 2 - 1
The third row has 5 * where 5 = 2 * 3 - 1
The last row of the upper half (the middle row) has 2 * (n/2) - 1 *
I.e the function is 2 * i - 1 for the upper half.
The lower half is similar. It appears that if you go backwards you get the same result.
What if you apply some constant and subtract the function (going backwards). Let the constant be 2 times the total number of rows. You get 2 * n - ((2 * i) - 1 where n is the total number of rows and i is the current row
This ought to make you think there could be a mathematical solution to the problem, and there actually is.
If you take the absolute value of the number of rows minus the function and subtract it from the total number of rows you seem to get the numbers to turn when you get to the middle.

consider the case where we got 5 rows
1 = n - (abs(n - 1)) where 1 = 2 * 1 - 1
3 = n - (abs(n - 3)) where 3 = 2 * 2 - 1
5 = n - (abs(n - 5)) where 5 = 2 * 3 - 1
3 = n - (abs(n - 7)) where 7 = 2 * 4 - 1
1 = n - (abs(n - 9)) where 9 = 2 * 5 - 1
n=5
but does it also work for even numbers of n? Let's have a look at the example with 6 rows.

1 = n - (abs(n - 1)) where 1 = 2 * 1 - 1
3 = n - (abs(n - 3)) where 3 = 2 * 2 - 1
5 = n - (abs(n - 5)) where 5 = 2 * 3 - 1
5 = n - (abs(n - 7)) where 7 = 2 * 4 - 1
3 = n - (abs(n - 9)) where 9 = 2 * 5 - 1
1 = n - (abs(n - 11)) where 11 = 2 * 6 - 1
n = 6
It works for even numbers too!!


Sorry about the somewhat poor explanation. Perhaps someone can rewrite this so it gets more readable?!

This post has been edited by Gloin: 14 Oct, 2008 - 06:28 AM
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Diamond
14 Oct, 2008 - 09:10 AM
Post #5

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
I forgot to post the part where you can get an idea of how to translate this magic into code.

if you want the diamond to have n rows, first create a for-loop from 1 to n:

for (int i = 1; i <= n; i++) {

then calculate the number of *s there should be on this row:

NumOfStars = n - Math.abs(n - (i * 2) - 1); //compare with the examples from the previous post.

Create another loop inside the first loop:

for (int j = 0; j < NumOfStars; j++) {
System.out.print("*");
} // end the inner loop
System.out.println("");
} // end the outer loop

I think this ought to be pretty much working code although I'd rather not submit the whole solution.

This post has been edited by Gloin: 14 Oct, 2008 - 09:11 AM
User is offlineProfile CardPM
+Quote Post

NeoTifa
RE: Diamond
14 Oct, 2008 - 09:30 AM
Post #6

oohhhh, i get one nao!!!
****

Joined: 24 Sep, 2008
Posts: 968



Thanked: 4 times
My Contributions
great, thanks for answering him and not me. >:[ but i still need a diamond that actually looks like a diamond, and have an option for it to not be filled in!!!! plz answer in my thread!!!
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 03:50AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month