Welcome to Dream.In.Code
Become a Java Expert!

Join 136,896 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,730 people online right now. Registration is fast and FREE... Join Now!




floyd's triangle

 
Reply to this topicStart new topic

floyd's triangle

sugunacode
3 Aug, 2008 - 11:39 PM
Post #1

New D.I.C Head
*

Joined: 3 Aug, 2008
Posts: 1

writw a code for printing

1
0 1
1 0 1
0 1 0 1
User is offlineProfile CardPM
+Quote Post

lordms12
RE: Floyd's Triangle
4 Aug, 2008 - 01:32 AM
Post #2

D.I.C Regular
Group Icon

Joined: 16 Feb, 2008
Posts: 314



Thanked: 15 times
Dream Kudos: 225
My Contributions
Please show your efforts first, it is site policy

This post has been edited by lordms12: 4 Aug, 2008 - 01:40 AM
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Floyd's Triangle
4 Aug, 2008 - 05:14 AM
Post #3

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 48 times
My Contributions
QUOTE(sugunacode @ 4 Aug, 2008 - 12:39 AM) *

writw a code for printing

1
0 1
1 0 1
0 1 0 1


In Pseudo-Code

int numberOfRows = 4; //You can change this number to adapt to the size of the triangle
int incrementalNumber = 0;
for (int i = 1; i = numberOfRows; i++) {
for (int j = 0; j < i; j++) { //iterates once the first row, twice the second row, and so on.
incrementalNumber++; //Increase this variable for every iteration
print((incrementalNumber % 2) + " "); //('% 2' = mod 2 = integer remainder after (multiple) division by 2 [0, 1]
//for example 9 % 4 = 1 since (integer division) 9/4 = (integer part) 2 with the remainder 1.
//Since the variable incrementalNumber will alter between odd and even, the remainder part will alter between 0 and 1, which is the number you print out on the screen.
} //print = (Java) System.out.print
println();
}

I think this is what you're asking for, although your image looks somewhat distorted. The code could however easily be modified to work with different distances between the numbers if needed.
If this is indeed schoolwork and you have difficulties then I recommend you brush up on your math, especially the algebra part.
User is offlineProfile CardPM
+Quote Post

Noldona
RE: Floyd's Triangle
4 Aug, 2008 - 08:20 AM
Post #4

Addicted to DIC
****

Joined: 2 Jul, 2002
Posts: 939



Thanked: 3 times
My Contributions
Instead of using the number % 2 like that, if it was just going to be 1s and 0s, I would set a boolean variable to true to start with then in the loop set the boolean variable to not itself. then if else statement to print 1 or 0. Although your way would take fewer lines of code.
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Floyd's Triangle
6 Aug, 2008 - 06:38 AM
Post #5

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 48 times
My Contributions
Naturally you could use a boolean if you want but since some programming languages don't interpret true = 1 and false = 0, and since I wouldn't want unnecessary if-statements, using modulo is not only a faster but also more beautiful solution. wink2.gif
User is offlineProfile CardPM
+Quote Post

pearl579
RE: Floyd's Triangle
29 Aug, 2008 - 03:48 AM
Post #6

New D.I.C Head
*

Joined: 4 Aug, 2008
Posts: 7

QUOTE(Gloin @ 6 Aug, 2008 - 07:38 AM) *

Naturally you could use a boolean if you want but since some programming languages don't interpret true = 1 and false = 0, and since I wouldn't want unnecessary if-statements, using modulo is not only a faster but also more beautiful solution. wink2.gif



plz can anybody write down a proper code for this program i desparately need it
User is offlineProfile CardPM
+Quote Post

JeroenFM
RE: Floyd's Triangle
29 Aug, 2008 - 12:21 PM
Post #7

D.I.C Head
Group Icon

Joined: 30 Jun, 2008
Posts: 188



Thanked: 9 times
Dream Kudos: 100
My Contributions
QUOTE(pearl579 @ 29 Aug, 2008 - 04:48 AM) *

QUOTE(Gloin @ 6 Aug, 2008 - 07:38 AM) *

Naturally you could use a boolean if you want but since some programming languages don't interpret true = 1 and false = 0, and since I wouldn't want unnecessary if-statements, using modulo is not only a faster but also more beautiful solution. wink2.gif



plz can anybody write down a proper code for this program i desparately need it


Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Floyd's Triangle
29 Aug, 2008 - 02:20 PM
Post #8

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 48 times
My Contributions
The Pseudo-code I posted is practically java-code. If you can't translate it into real java code then I don't think you tried your best.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 09:25PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month