1. Member
Join Date
Jan 2009
Posts
31
Rep Power
0

## Drawing out triangles

Hello. For an assignment I have to ask for input of a character and a number. And then the program will draw out a triangle based on the character and the number as the number of rows. So if the input was "@" and "5" it would draw out this:
|||||@||||
|||@||@||
||@||||@||
|@|||||||@|
@|@|@|@|@

The "|" is just there so that the triangle actually shows up. First row should have 4 spaces on either side of the @. Last row should have five @ with one space separating them.
I can't think of a code efficient way of doing this. If anyone could help with this, it would be greatly appreciated.
Last edited by leiferouis; 01-12-2009 at 03:32 AM.

2. Senior Member
Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
8
there are 3 conditions for printing @
first 2 are suitable for all lines
last one used to handle base line of Triangle

you can see, symmetry line in the center of Triangle
so the first 2 conditions are "center position -/+ offset" while offset change by line number

last conditions... do you know how to find a number is odd or even?

3. Member
Join Date
Jan 2009
Posts
31
Rep Power
0
No I don't know how to find whether a number is odd or even, would you do a multiplication test? Also, I don't understand what you mean by center position -/+ offset.

4. Senior Member
Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
8
for example input number is "5"
expected result in line 1
@ will appear in the center, so is center(which is 5) + or - offset (which is 0 in line 1)
in line 2, @ will appears at center (5) + or - offset (1)
@s appear => 4 and 6 pos

test odd/even, suggested method is num % 2 = 0 or 1

5. Member
Join Date
Jan 2009
Posts
31
Rep Power
0
How would I do that in a loop? Is there a built in offset function?

6. Senior Member
Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
8
2 for(or while) loop needed, i think you know that,
for
Java Code:
```for (i=0; i < triangleWidth; i++) // row
{
for (j=0; j < xxxx ; j++) // column
{
// process
}
}```
1) consider what xxxx should be,

2) by the previous post
first line, offset is +/-0
second line, offset is +/- 1
third line, offset is +/- 2 and so on,
can you see any relation between the offset and value of i, j

you can create a method intake value of i,j or simply use nested if statement to check

3) do you know how to find out center position of Triangle by user input?
Last edited by mtyoung; 01-14-2009 at 05:07 AM.

7. Member
Join Date
Jan 2009
Posts
31
Rep Power
0
1) I think that xxxx would have to be the inputed number times 2 because that equals the number of columns every triangle should have.
2)Wouldn't the offset have to equal the current i value?
3)center position = user input - ((user input-1)/2) for odd numbers, don't know what to do for even numbers.

8. It's not difficult, just really boring.

WARNING!! The following is a spoiler.

Java Code:
```public class DrawTri{
public static void main(String[] args){

// add features to allow user input
int n = 2;		// user input
char c = '@';	// user input

final int spacer = n * 2;

// print first section
for(int line=0; line<(n-1); line++){
char[] charArray = new char[spacer];
int mid = charArray.length / 2;

charArray[mid - line] = c;
charArray[mid + line] = c;

System.out.println(charArray);
}

// print last line
while(n != 0){
System.out.print(" " + c);
n--;
}
System.out.println();
}
}```

9. wow I got some really nice graphic when I changed n=500. Its like typing "yes" on gnu/linux, except a more interesting response.

10. ## very interesting...

Neat solution, but I would suggest not throwing out complete solutions before the OP presents his solution. It really doesn't help giving a complete solution... too much temptation to cut & paste & forget... no learning in the process.

CJSL

11. Sorry, I thought this was a help-solutions forum.

12. Senior Member
Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
8
me, c++ type thinking...
use of arrays... hadn't thought of that
not thought can cout whole array...

13. Member
Join Date
Jan 2009
Posts
31
Rep Power
0
umm... you forgot to set all the array as ' ' before the loop. But other than that, whenever I run the program, it sets the center array space as the char as well.
Also what does final int mean?

14. Senior Member
Join Date
Dec 2008
Location
Hong Kong
Posts
473
Rep Power
8
final means constant

leiferouis, can you check the output of first post is correct or not
some lines have 9 characters and some have 10 characters?
Last edited by mtyoung; 01-15-2009 at 07:11 AM.

15. I didn't really want to dwell too deep into that, because its your program. haha. But I think the jvm auto sets the array. I forgot, i'll have to look that up again. final is like const in c/c++.

16. ## It is a help forum...

AB: It is a help forum... help people with their problems with Java programs. Giving complete solutions doesn't help them because there is no learning (they can take it and run :-). Now, what is helpful is pseudo code, hints, code snippets, explainations, etc. That kind of help is very useful to the OP.

CJSL

17. Member
Join Date
Jan 2009
Posts
31
Rep Power
0
For the code that angryboy offered, if you put charArray[4] and charArray[6] instead of charArray[mid-line] and charArray[mid+line] it works perfectly. However, its obvious that numbers cannot be used in the loop so I tried putting mid-line and mid+line in two different variables, however that did not work either. It always has the center space changed into the character as well.

mtyoung:All lines have 10 spaces.
Another few things i wanted to ask: 1. Why did you use charArray.length to set the value of mid instead of just spacer/2? 2.Why is the mid integer created within the loop?
Last edited by leiferouis; 01-15-2009 at 04:18 PM.

18. There's many ways to code, just as there are many colors to paint with. I used charArray.length cause I wanted it to depend on charArray, so if something breaks in the chain, it'll still work. But I realize that adds an extra char at the console because of the zero-based array.

19. Well basically, the whole thing is ascii art. I use the charArray as a canvas filled by spaces. Then set the correct place in the array with '@'. And have that put inside a loop to shift its position.

20. Member
Join Date
Jan 2009
Posts
31
Rep Power
0
I got it working. I can't believe how I never noticed this before. I didn't reset the array in the loop therefore the characters that were changed before were still changed. Thanks for the help angryboy.

Page 1 of 2 12 Last

#### Posting Permissions

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