# Thread: ASCII art, drawing an asterisks diamond within dots.

1. Member Join Date
Nov 2011
Posts
2
Rep Power
0

## ASCII art, drawing an asterisks diamond within dots.

Hi all, new to the forum here. Currently doing a first year in a Computing course and have started learning my first programming language which is Java. Currently onto my second group of assignments and have been instructed to create a program which will drawn a diamond based on the width of a square. The number entered must be an odd number which i have already done. Here is my code and il explain the problem below:

Java Code:
```public class Main
{
public static void main(String args[])
{
System.out.println("#Size Of Square:");
int sizeofsquare = 7;
int down = 2;
int across = 1;
int dots = 0;
int btmacross = 1;
int stars = sizeofsquare / 2;
int starn = sizeofsquare / 2;

if ( sizeofsquare < 0 ){
System.out.print( "Sides of square must be positive" );

}
if ( sizeofsquare >= 0 ){

if (sizeofsquare % 2 == 0) {

System.out.print( "Size " );
System.out.print( "(" );
System.out.print( sizeofsquare );
System.out.print(") invalid must be odd" );
}

if (sizeofsquare % 2 != 0) {

while ( sizeofsquare <= 40 ){

while ( across < sizeofsquare ){

System.out.print(".");

if ( across == stars ){
System.out.print("*");
}

across = across + 1;

}
while ( down < sizeofsquare ){

stars = stars - 1;
starn = starn + 1;

System.out.println();

while ( dots < sizeofsquare ){
if ( dots == stars ){

System.out.print("*");
dots = dots + 1;
}
if ( dots == starn ){
System.out.print("*");
dots = dots + 1;
}
if ( dots < sizeofsquare ){
System.out.print(".");
}
dots = dots + 1;
}
if ( down > sizeofsquare / 2){

stars = sizeofsquare / 2;
starn = sizeofsquare / 2;

if ( dots == stars ){
System.out.print("*");
dots = dots + 1;
}
if ( dots == starn ){

System.out.print("*");
dots = dots + 1;
}

if (dots < sizeofsquare ){
System.out.print(".");
}
dots = dots + 1;
}

down = down + 1;
dots = 0;

}
while (down == sizeofsquare){

System.out.println();

while ( btmacross < sizeofsquare ) {
System.out.print(".");

if ( btmacross == sizeofsquare / 2 ){
System.out.print("*");
}

btmacross = btmacross + 1;

}

down = down + 1;
}
}
}
}
}
}```
This code outputs:

#Size Of Square:
...*...
..*.*..
.*...*.
*.....*
..*.*..
..*.*..
...*...

however it obviously needs to be:

#Size Of Square:
...*...
..*.*..
.*...*.
*.....*
.*...*.
..*.*..
...*...

Thanks for any help :)
Last edited by Norm; 11-10-2011 at 07:29 PM. Reason: added code tags  Reply With Quote

2. ## Re: ASCII art, drawing an asterisks diamond within dots.

Your logic seems not to put out enough inside .s on some lines. How do you determine how many dots to put out on each line?
None on first
1 on 2nd
3 on 3rd
5 on 4th
then reduce the number for the rest back to none.

What algorithm are you using to determine how many inside dots to put out on each line?  Reply With Quote

3. Member Join Date
Nov 2011
Posts
2
Rep Power
0

## Re: ASCII art, drawing an asterisks diamond within dots.

I'm confused, from the results shown my program is outputting enough "."s but isn't putting the *'s below the middle of the diamond in the right place due to the starn and stars variables being the same. They both need to increase/decrease like before but i can't get it to do that as i have to redeclare the variables as sizeofsquare / 2 instead of using that area to have them change e.g stars = stars - 1 or whatever.  Reply With Quote

4. ## Re: ASCII art, drawing an asterisks diamond within dots.

What is your logic to determine how to format what is on a line? How does the line number relate to what is printed on each line?
What is your algorithm for determining what goes on each line?  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•