# Drawing out triangles

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 01-12-2009, 03:28 AM
leiferouis
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.
• 01-12-2009, 12:04 PM
mtyoung
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?
• 01-12-2009, 04:01 PM
leiferouis
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.
• 01-13-2009, 04:51 AM
mtyoung
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
• 01-13-2009, 04:58 PM
leiferouis
How would I do that in a loop? Is there a built in offset function?
• 01-14-2009, 05:03 AM
mtyoung
2 for(or while) loop needed, i think you know that,
for
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?
• 01-15-2009, 01:23 AM
leiferouis
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.
• 01-15-2009, 02:40 AM
angryboy
It's not difficult, just really boring.

WARNING!! The following is a spoiler.

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();         } }```
• 01-15-2009, 02:44 AM
angryboy
wow I got some really nice graphic when I changed n=500. Its like typing "yes" on gnu/linux, except a more interesting response.
• 01-15-2009, 03:12 AM
CJSLMAN
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
• 01-15-2009, 03:36 AM
angryboy
Sorry, I thought this was a help-solutions forum.
• 01-15-2009, 03:43 AM
mtyoung
me, c++ type thinking...
use of arrays... hadn't thought of that
not thought can cout whole array...
• 01-15-2009, 06:55 AM
leiferouis
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?
• 01-15-2009, 07:07 AM
mtyoung
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?
• 01-15-2009, 07:10 AM
angryboy
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++.
• 01-15-2009, 02:35 PM
CJSLMAN
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
• 01-15-2009, 04:08 PM
leiferouis
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?
• 01-15-2009, 04:29 PM
angryboy
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.
• 01-15-2009, 04:34 PM
angryboy
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.
• 01-15-2009, 04:41 PM
leiferouis
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.
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last