CS1101C Lab 8

The deadline for this lab is Saturday 01 November 2008, 23:59:59 hours.

Preliminary — Pascal Triangle

Pascal's Triangle was originally developed by the ancient Chinese, but Blaise Pascal was the first person to discover the importance of all of the patterns it contained.

Look at the diagram on the right. At the tip of Pascal's Triangle is the number 1, which makes up the zeroth row. The first row (1 & 1) contains two 1's, both formed by adding the two numbers above them to the left and the right, in this case 1 and 0 (all numbers outside the Triangle are 0's). Do the same to create the 2nd row: 0+1=1; 1+1=2; 1+0=1. And the third: 0+1=1; 1+2=3; 2+1=3; 1+0=1. In this way, the rows of the triangle go on infinitely.

Since it is difficult for us to show Pascal's Triangle in a triangular form as shown in the diagram, we can show the first 12 rows of Pascal's Triangle in the following format:

1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
1 11 55 165 330 462 462 330 165 55 11 1

Each number in Pascal's Triangle is given a coordinate in the form of (n,k) where n is the row number and k is the column number. Both row and column numbers start from zero. So, (0,0) is the very first top-left number 1, (4,2) is the number 6, and (7,5) is the number 21.

In this assignment, we shall look at several ways of constructing the Pascal's Triangle.

Question 1 — Using Binomial Coefficients

Each coordinate (n,k) can be computed using binomial coefficients

which can be expressed as a computation involving factorials where the factorial of n is defined as

As an example, the factorial of six is

A skeleton program is provided for you. Your task is to define the function constructPascal that outputs the Pascal Triangle for a user-specified number of rows (a value greater than 0). You are to define two other functions binomialCoefficient and factorial to modularize the computation of the binomial coefficient and factorial. The function prototypes are as follows.

void constructPascal(int numRows);
int binomialCoefficient(int n, int k);
int factorial(int n);

A number of sample run are given below. Your program should give the correct output for Pascal Triangles of rows ranging from 1 to 13 (both inclusive).

Enter the number of rows of the Pascal Triangle: 1
1 
Enter the number of rows of the Pascal Triangle: 6
1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
Enter the number of rows of the Pascal Triangle: 10
1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1 
1 7 21 35 35 21 7 1 
1 8 28 56 70 56 28 8 1 
1 9 36 84 126 126 84 36 9 1 
Enter the number of rows of the Pascal Triangle: 13
1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1 
1 7 21 35 35 21 7 1 
1 8 28 56 70 56 28 8 1 
1 9 36 84 126 126 84 36 9 1 
1 10 45 120 210 252 210 120 45 10 1 
1 11 55 165 330 462 462 330 165 55 11 1 
1 12 66 220 495 792 924 792 495 220 66 12 1 

The name of your C program file must be called lab8q1.c, files with any other name will not be marked.

Question 2 — Re-evaluating Binomial Coefficients

Try the above program with a larger number of rows (say 20). If you merely followed the expression for computing binomial coefficients using factorial, then most probably you would get unexpected results. The reason is that the computation of large factorials (> 13) will result in overflow error.

In this part of the assignment, you are required to think about how the computation of the binomial coefficient can be restructured to avoid the computation of large factorials. You are to redefine the binomialCoefficient function in question 1. Try expanding the factorials involved in computing the binomial coefficient and look at the terms that can be cancelled. Once again, you are encouraged to discuss this in the forum, without giving the solution directly. You might also find the following statement helpful.

The product of n consecutive integers is divisible by the factorial of n.
You are not allowed to use any data type other than int. The function prototypes should remain the same as per question 1.

A number of sample run are given below. You can assume that for this and subsequent exercises, we will never exceed 23 rows when constructing the Pascal Triangle.

Enter the number of rows of the Pascal Triangle: 1
1 
Enter the number of rows of the Pascal Triangle: 13
1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1 
1 7 21 35 35 21 7 1 
1 8 28 56 70 56 28 8 1 
1 9 36 84 126 126 84 36 9 1 
1 10 45 120 210 252 210 120 45 10 1 
1 11 55 165 330 462 462 330 165 55 11 1 
1 12 66 220 495 792 924 792 495 220 66 12 1 
Enter the number of rows of the Pascal Triangle: 20
1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1 
1 7 21 35 35 21 7 1 
1 8 28 56 70 56 28 8 1 
1 9 36 84 126 126 84 36 9 1 
1 10 45 120 210 252 210 120 45 10 1 
1 11 55 165 330 462 462 330 165 55 11 1 
1 12 66 220 495 792 924 792 495 220 66 12 1 
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1 
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1 
1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1 
1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1 
1 17 136 680 2380 6188 12376 19448 24310 24310 19448 12376 6188 2380 680 136 17 1 
1 18 153 816 3060 8568 18564 31824 43758 48620 43758 31824 18564 8568 3060 816 153 18 1 
1 19 171 969 3876 11628 27132 50388 75582 92378 92378 75582 50388 27132 11628 3876 969 171 19 1 
Enter the number of rows of the Pascal Triangle: 23
1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1 
1 7 21 35 35 21 7 1 
1 8 28 56 70 56 28 8 1 
1 9 36 84 126 126 84 36 9 1 
1 10 45 120 210 252 210 120 45 10 1 
1 11 55 165 330 462 462 330 165 55 11 1 
1 12 66 220 495 792 924 792 495 220 66 12 1 
1 13 78 286 715 1287 1716 1716 1287 715 286 78 13 1 
1 14 91 364 1001 2002 3003 3432 3003 2002 1001 364 91 14 1 
1 15 105 455 1365 3003 5005 6435 6435 5005 3003 1365 455 105 15 1 
1 16 120 560 1820 4368 8008 11440 12870 11440 8008 4368 1820 560 120 16 1 
1 17 136 680 2380 6188 12376 19448 24310 24310 19448 12376 6188 2380 680 136 17 1 
1 18 153 816 3060 8568 18564 31824 43758 48620 43758 31824 18564 8568 3060 816 153 18 1 
1 19 171 969 3876 11628 27132 50388 75582 92378 92378 75582 50388 27132 11628 3876 969 171 19 1 
1 20 190 1140 4845 15504 38760 77520 125970 167960 184756 167960 125970 77520 38760 15504 4845 1140 190 20 1 
1 21 210 1330 5985 20349 54264 116280 203490 293930 352716 352716 293930 203490 116280 54264 20349 5985 1330 210 21 1 
1 22 231 1540 7315 26334 74613 170544 319770 497420 646646 705432 646646 497420 319770 170544 74613 26334 7315 1540 231 22 1 

Do not worry about lines being automatically wrapped around by your console window. That's not your fault.

The name of your C program file must be called lab8q2.c, files with any other name will not be marked.

Question 3 — Using Array

There is a much simpler way of constructing the Pascal Triangle which makes use of the following.

This involves storing the values of the current row in order to compute the values of the next row. As such an array is in order. Include the declaration of an appropriately sized array in the constructPascal function; this array should also be appropriately initialized. Once again, you are reminded that we will never exceed 23 rows when constructing the Pascal Triangle.

The sample run is exactly the same as per question 2.

The name of your C program file must be called lab8q3.c, files with any other name will not be marked.

Question 4 — Let's get drunk!

We can approximate the values of the Pascal Triangle by first getting ourselves drunk (not to be taken literally though) and taking a walk home. Assume that we start off at position 0 and can only move randomly forward (+1 step) or backward (-1 step) with equal probability in every step.

If we are allowed only one step, then we have an equal probability of 0.5 ending up either at positions -1 or +1. Multiply these two probabilities by 21, and we get the two values of 1 in row n = 1 of the Pascal Triangle. If we are allowed two steps, then we either end up at positions -2, 0 or +2. The probabilities of ending up at these positions are 0.25, 0.5 and 0.25 respectively. By multiplying these three probabilities by 22, we obtain the numbers 1, 2 and 1 respectively, consistent with the values of row n = 2 of the Pascal Triangle. So in general, if we are allowed to take n steps, we would end up at n+1 different positions. The probabilities of landing at these positions multiplied by 2n, gives us the desired values of row n of the Pascal Triangle. You might also like to note that you will always end up in odd (even) positions if n is odd (even).

Your task is to write a program to simulate the drunkard's walk. At the start of your program, the user will be prompted for the number of trials R and the random seed, in addition to the number of rows of the Pascal Triangle. For each row, simulate the walk for a total of R trials, and determine the frequencies of landing at the different positions. Use this to approximate the values of the Pascal Triangle, and print out the values to 3 decimal places. Do this for the required number of rows. Re-declare the array in the constructPascal function of question 3 with an appropriate size such that it can be used to store the frequencies of landing at a particular position. Write a function randomWalk that takes in this array and simulates the walk for R trials. The function prototypes of constructPascal and randomWalk is given below.

void constructPascal(int numRows, int numTrials);
void randomWalk(int walk[], int n, int numTrials);

You may define other helper functions to further modularize your program. The sample run is given below. Once again, due to the randomness of the program, your output might not be the same. However, as the number of trials increases, your values must converge to the values of the Pascal Triangle.

Enter the number of rows of the Pascal Triangle: 1
Enter number of trials: 10000
Enter seed: 123
1.000
Enter the number of rows of the Pascal Triangle: 6
Enter number of trials: 10000
Enter seed: 234
1.000 
0.996 1.004 
1.016 2.011 0.973 
1.004 2.991 2.970 1.035 
0.931 3.958 6.061 4.037 1.013 
1.114 5.050 9.894 9.994 4.982 0.966 
Enter the number of rows of the Pascal Triangle: 10
Enter number of trials: 1000000
Enter seed: 345
1.000 
1.000 1.000 
1.000 2.000 1.000 
1.000 3.000 3.000 1.000 
0.998 4.004 5.993 4.006 0.999 
1.005 4.984 10.012 10.010 4.983 1.006 
1.017 5.951 15.059 19.943 15.063 5.952 1.014 
1.003 7.029 20.908 35.063 35.054 20.918 7.021 1.004 
1.031 8.079 27.983 55.079 71.658 55.074 27.986 8.078 1.031 
1.066 8.944 36.221 83.021 126.710 126.774 83.032 36.218 8.947 1.066 

The name of your C program file must be called lab8q4.c, files with any other name will not be marked.

Note and Ponder

  1. Assume that all user input is valid unless otherwise stated.

  2. If your program does not work as you expect (logical errors), use extra printf statements to print out all the values of your variables to aid in your debugging.

  3. There should be no blank lines at the start or end of each output run.

  4. Most importantly, have lots of fun programming!


This document, index.html, has been accessed 55 times since 25-Jun-24 11:57:13 +08. This is the 1st time it has been accessed today.

A total of 43 different hosts have accessed this document in the last 444 days; your host, nsrp-source.comp.nus.edu.sg, has accessed it 7 times.

If you're interested, complete statistics for this document are also available, including breakdowns by top-level domain, host name, and date.