• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Pascal's Triangle

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76397
364
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
... and welcome to the Ranch
 
Timmy Williams
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the welcome, finally cracked it.



 
Bartender
Posts: 5061
188
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic