• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Pascal's Triangle

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all.
Started to read the art and science of Java and completing chapter exercises, below is pascals triangle as graphics program. I wanted to use the For loops which create the pyramid in combinations method. For this to work 3 If statements are used. I'm wondering if there is a better method to altering the values of the For loops to get the same outcome. At the moment if the pyramid size is increased in size I would need to add more if statements. Any advice greatly appreciated.



 
Sheriff
Posts: 6456
172
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are right to think that if you have to add more if statements as your triangle gets bigger then there is something wrong with your algorithm.

You way to tackle the problem is to think about how you would create a Pascal Triangle by hand, that is, with a pencil and paper.  Only then try to turn the algorithm into Java code.

So how would you do this by hand?
 
Marshal
Posts: 66637
251
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... and welcome to the Ranch
 
Timmy Williams
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the welcome, finally cracked it.



 
Bartender
Posts: 3674
151
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done!

Years ago I made a similar Triangle, with two differences: 1) I let Java do all the centering calculations, and I determined a Pascal value as the sum of the two values above. In case you are interested:
 
Timmy Williams
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for attaching the way you created Pascals triangle, this has made me realise I have so much more to learn as most of the imports required I have never heard of. Arrays are coming next I will spend time going through your code as this will help with future questions from the book.
 
Evacuate the building! Here, take this tiny ad with you:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!