# Bigger triangle made up of multiple ones?

• January 24th, 2011, 11:42 PM
Kimimaru
Bigger triangle made up of multiple ones?
Hey everyone! I have an assigment due next Monday, and in this assignment I must create an ASCII triangle in Java. I've managed to do that, but for extra credit I have to do something like this:

http://img585.imageshack.us/img585/6610/triangleq.png

Here is the code I have so far:

Code Java:

```/********************************************************************** * Program: Triangles * Author: * * Programming Assignment #2 * Date: * * This program prompts the user to input the length of the side of an * equilateral triangle that is larger than 2. An equilateral triangle * will then be created using the ASCII character "*" (ASCII graphics) * * Input: * Side of the equilateral triangle * * Output: * An equilateral triangle made in ASCII graphics **********************************************************************/   import java.io.*; import java.util.*;   public class Triangle { public static void main(String[] args) {   //Variable declarations - i is the main LCV (Loop Control Variable) int triangleside = 0, triangleheight = 0, i, j, side; String intriangleside = null, option = null; boolean Start = true;   //Prepares input Console console = System.console();   //Welcomes the user to the program System.out.println("Welcome to the ASCII triangle drawing program!");   //Loop that controls whether the main part of the program should start or not while(Start==true) { try { intriangleside = console.readLine("Enter the length of the triangle's side: "); triangleside = Integer.parseInt(intriangleside); side = triangleside;   //Tells the user that a number greater than or equal to two is required if one is input if (triangleside<2) { System.out.println("Sorry, please choose a number greater than or equal to 2."); }   //Draws the triangle if the value is 2 or above else {   System.out.println("Here is your triangle: "); System.out.println();   //Loop that controls how many lines are made for(i=triangleside; i>0; i--) {   //Loop that controls how many spaces are in each line for(triangleheight=i; triangleheight<side; triangleheight++) { System.out.print(" "); }   //Loop that controls how many stars are in each line for(triangleside=0; triangleside<i; triangleside++) { System.out.print("* "); }   System.out.println(); } }   Start=false; option = console.readLine("Would you like to make another triangle? Please type either 'Yes' or 'No': ");   if (option.equals("Yes")) { Start=true; }   else if (option.equals("No")) { System.out.println("Goodbye!"); System.exit(0); } }   /*Finds if an inappropriate response (Ex: Typing in 1 when prompted for a yes or no answer) is given *and closes the program if one isn't*/ catch(Throwable e) { System.out.println("Please type in an appropriate response next time. Goodbye!"); System.exit(0); } } } }```

Apparently I have to make a larger triangle out of the smaller ones, but I'm not quite sure how to approach it. Any help would be greatly appreciated!
• January 25th, 2011, 07:42 AM
KevinWorkman
Re: Bigger triangle made up of multiple ones?
Well, how would you do this "by hand" without a computer?
• January 25th, 2011, 07:14 PM
Kimimaru
Re: Bigger triangle made up of multiple ones?
I would draw a bigger triangle out of smaller ones.
• January 26th, 2011, 06:06 AM
KevinWorkman
Re: Bigger triangle made up of multiple ones?
Quote:

Originally Posted by Kimimaru
I would draw a bigger triangle out of smaller ones.

Funny, but obviously not what I meant.

One of the challenges that programmers face is breaking up things that seem obvious to us (like "draw a bigger triangle out of smaller ones") into steps that a computer can understand. HOW would you draw that bigger triangle out of smaller ones?

If you can't figure that out, pretend you have a friend who has no idea how to draw a triangle, let alone a big tiangle out of smaller ones. Write out the exact, specific steps he should follow to accomplish this goal, and you'll have an algorithm that should be pretty easy to translate to code.
• January 29th, 2011, 06:28 PM
Kimimaru
Re: Bigger triangle made up of multiple ones?
Okay, thanks! Writing the pseudo code helped me quite a bit, but I still haven't got it so far. I'm VERY close, but I'm definitely overlooking something. Here's what I got so far:

Code :

```for (j=0;j<side; j++) { for (triangleheight=triangleside; triangleheight>j; triangleheight--) {   for (i=0; i < triangleside; i++) { System.out.print(" *"); }   } System.out.println(); for (triangleheight=0; triangleheight<=j; triangleheight++) { System.out.print(" "); } }```

As you can see, all I really need to do is space the lower lines out at certain values. After that, I just need to add an outer loop and the program can be considered done. Can anyone tell me how I can go about doing this? The extra credit is due on Monday, so I'd really appreciate it if I got an answer soon. Thanks!

Also, my code looks unorganized because of how the forum formats it. I'll see if I can fix it up to make it more readable.
• January 30th, 2011, 08:40 AM
nine05
Re: Bigger triangle made up of multiple ones?
Hell,

I'm just new in java. I tried to make what it is shown in the image. For the output make sure your font is monospaced so the height and width is the same... Thanks...

....edited
• January 30th, 2011, 10:18 AM
copeg
Re: Bigger triangle made up of multiple ones?
Quote:

Originally Posted by nine05
Hell,

I'm just new in java. I tried to make what it is shown in the image. For the output make sure your font is monospaced so the height and width is the same... Thanks...

....edited

nine05, thanks for contributing and welcome to the forums. While I believe your intentions were good, we'd like to ask that you do not hand over solutions...as stated in the Announcements 'Providing homework solutions in full or in part is frowned upon, and contributions that are considered as such will be subject to moderation (editing and/or deletion)'. I have edited your post as a result.