Computer Science Canada Pascal's Triangle starting help |

Author: | xanderman2113 [ 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. |

Author: | DemonWasp [ 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. |

Author: | xanderman2113 [ 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 |

Author: | DemonWasp [ 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. |

Author: | xanderman2113 [ Wed Dec 03, 2014 8:32 pm ] |

Post subject: | Re: Pascal's Triangle starting help |

Thanks for the help. |

: |

You can syndicate this boards posts using the file backend.php or view the topic map using sitemap.php.