Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
Pascal's Triangle starting help
Author Message
xanderman2113

Posted: Tue Dec 02, 2014 7:55 pm   Post subject: Pascal's Triangle starting help

Hi, I'm currently in grade 11 computer science and was tasked with this question. I am usually pretty good with these textbook questions, but I can not figure out how to do this specific one for the life of me. Once I understand the concept, I am fairly confident that I will be able to code this program, but just need some help with understanding how this works and an algorithm.
Thanks!

Textbook Problem:
http://prntscr.com/5cn4b7
http://prntscr.com/5cn4or

P.S. I am currently just confused as to how 'n' over 'r' translates into outputting the numbers up until the inputted row.

DemonWasp

Posted: Tue Dec 02, 2014 9:03 pm   Post subject: RE:Pascal\'s Triangle starting help

It's not n / r (n over r), it's (n choose r). That is, (n choose r) is the number of different ways to select (r) things from (n) objects. For example, the number of ways to choose 6 different numbers from the numbers 1 to 10 is (10 choose 6). See also http://en.wikipedia.org/wiki/Binomial_coefficient

The formula on the second image you linked gives (n choose r) = n! / ( r! * (n-r)! ). This gives you a straightforward way to calculate the correct answer for small values of n and r.
xanderman2113

Posted: Wed Dec 03, 2014 12:04 pm   Post subject: Re: Pascal's Triangle starting help

Ok, thanks for the reply I understand that a little bit better, but could someone give me a starting build block? Our teacher gave us a hint to use longs instead of ints, so we can go up to 20 rows, and also to put the formula in its own method.
Thanks
DemonWasp

Posted: Wed Dec 03, 2014 3:57 pm   Post subject: RE:Pascal\'s Triangle starting help

Start by writing a formula for 'factorial'. That method will take one long argument, and return a long. Make your main() method print out the answers from your factorial(n) function for n from 1 to 10, which should exactly match the numbers from this page: http://en.wikipedia.org/wiki/Factorial

Once you have that, you should be able to write another method, 'choose', that takes two long arguments (n, r) and returns a long. The implementation of that function should be very obvious.
xanderman2113

Posted: Wed Dec 03, 2014 8:32 pm   Post subject: Re: Pascal's Triangle starting help

Thanks for the help.
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 5 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: