A Pascal’s triangle with levels 0 to 4 is shown below. Level 0 has a single value, and each value on
subsequent levels is the sum of the two entries diagonally above in the previous level of the triangle. For
example, the value 6 in level 4 is the sum of the values 3 and 3 in level 3.
1 level 0
1 1 level 1
1 2 1 level 2
1 3 3 1 level 3
1 4 6 4 1 level 4
Complete function pascalVector below to return the row vector corresponding to a specified level of
Pascal’s triangle. For example, if level lev is 4, then the returned vector must be [1, 4, 6, 4, 1]. Assume
that lev is a non-negative integer. The only Matlab built-in functions allowed are zeros, ones, and length.
Do not use the formula for binomial coefficients to solve this problem. Use a loop (or loops): the vector
for each level is based on the vector from the previous level.
function p = pascalVector(lev)
% p is the vector corresponding to level lev of Pascals triangle
.
"Old Test Question" from http://www.cs.cornell.edu/courses/cs1112/2015sp/Exams/final/oldQs.pdf
Comments
0 B
|👍
/👎