• Post Reply Bookmark Topic Watch Topic
  • New Topic

PowerOf2Triangle  RSS feed

 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to make this triangle:
                            1
                        1   2   1
                    1   2   4   2   1
                1   2   4   8   4   2   1
            1   2   4   8  16   8   4   2   1
        1   2   4   8  16  32  16   8   4   2   1
    1   2   4   8  16  32  64  32  16   8   4   2   1
1   2   4   8  16  32  64 128  64  32  16   8   4   2   1

I don't know how to do this in java please help.
Thanks
 
Norm Radder
Ranch Foreman
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how to do this

Take a piece of paper and write down the relationship between the numbers on each row and their relationship with the row they are on.
Look at that set of data and come up with some rules for generating those numbers.
Also find a rule for the number of leading spaces vs the row.

If you have problems making the rules, post what relationships you have found and the rules and ask some questions about the problems you are having.
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code so far:

 
Junilu Lacar
Sheriff
Posts: 11160
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, since the only statements you've written to print something out print either a 1, a space, or nothing, I assume this isn't doing what you need it do.

You really need to spend some time analyzing the problem first and looking for similarities and differences between one line and the ones the precede and succeed it. Look for things that are repetitive. These indicate loops. Things that change consistently from one case to the next, those are increments or decrements or more generally, deltas. Start and end values would indicate your loop boundaries.

As I wrote in response to another of your questions, this is about developing a logical and systematic way of solving a problem that can be generalized to handle multiple cases.
 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Never mind about the one on top.
Here's what I have so far:

 
Lindsey Brooks
Ranch Hand
Posts: 58
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got it.

Thanks for everyone's help.

 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lindsey Brooks wrote:. . .
Not happy with that. It would be far better to use the %d tag and use printf. It would be better still to use a StringBuilder and append the individual Strings with or without %d. Then you can collect the entire pyramid as a single String and print it with
System.out.println(myStringBuilder);
which may give faster performance than multiple tiny print calls.

Don't post commented‑out code. Only post code you are using. Don't comment out code with /* tags */. Always use // at the beginning of each line. That is because your code will fail to compile if you try to nest comments inside each other.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lindsey Brooks wrote:I got it. . . .
You have a habit of claiming success just because you get the desired output, whilst posting suboptimal code. You would do better to post your final offering and allow us to improve it.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It would be better still to use a StringBuilder and append the individual Strings with or without %d. Then you can collect the entire pyramid as a single String and print it with System.out.println(myStringBuilder);
Yes I used StringBuilder for that, Is It fine to append "\n" each time at the end of for statement ( Top level for statement ) for printing output on next line? Problem is, If I don't append "\n" then when I print Stringbuilder after appending all, It prints all numbers on a single line.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Patekar wrote:. . , Is It fine to append "\n" each time a. . . .
No. You sh‍ould only use \n if somebody has told you they want the LF character. Otherwise append this (or similar). That maintains the correct form for your operating system.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As I have java version "1.8.0_101" so at this moment I used lineSeparator() which returns system-dependent line separator string introduced in 7 means I can run my program on any platform like M Windows (Uses "\r\n") and UNIX (Uses "\n)

But what If I want to run my program on Java 6? I read 'n' is the platform-specific line separator Here, does that mean System.out.printf("%s%n","Java"); will work on both Windows and UNIX? I would have tried It practically but don't have any other OS except Windows.
 
Campbell Ritchie
Marshal
Posts: 55761
163
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganesh Patekar wrote:. . . . does that mean System.out.printf("%s%n","Java"); will work on both Windows and UNIX? . . . .
Yes, it will give the correct line separator for the OS. Before that method was introduced, you had to say something like:-It is quite easy to run a computer as a dual‑boot machine running both Windows and Linux. You simply download a Linux distro onto a CD and install it. You can even run Linux as a trial application from the CD.
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Yes, it will give the correct line separator for the OS. Before that method was introduced, you had to say something like:-
Oh I see, day by day Java is making programmer's work easy. Yes I'll download a Linux distro so many threads I found on this forum will read It, If still has problem will ask you. Thank you   
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!