Assignment #4 - Programming Languages | C S 345, Assignments of Programming Languages

Material Type: Assignment; Professor: Lavender; Class: PROGRAMMING LANGUAGES; Subject: Computer Sciences; University: University of Texas - Austin; Term: Fall 2007;

Typology: Assignments

Pre 2010

Uploaded on 08/31/2009

koofers-user-if9
koofers-user-if9 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 345 Programming Languages Fall 2007
11/14/07 12:57 PM Page 1 of 2
Programming Assignment #4 – Monday, 26 November
Problem Statement:
For this programming assignment, you will gain some experience programming in Scheme using
basic expressions, lists, list operations, recu rsive functi ons, and higher-order functions.
Pascal’s Triangle:
The philosopher and mathematician Blaise Pascal (162 3-1662) is famous among modern day
computer scientists for Pascal’s Triangle, and the programming language Pascal was named in his
honor. Pascal's Triangle is an arithmetical triangle representing the integer coefficients of the
expansion of the binomial equation (x+1)n, for n=0,1,…,n. For example, here is Pascal’s Triangle
for n = 0,1,…,7:
n=0 1
n=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
n=7 1 7 21 35 35 21 7 1
To compute Pascal’s Triangle in Scheme start with the list ‘(1). To construct the ith ro w, compute
the sum of the first two entries in the row above, then the next entry as the sum of the 2nd & 3rd
entries, and so on, until the last entry of the row above. To compute the 1’s on the edges, you
pretend that each row above the current one is 0 1 … 1 0.
Task 1: Implement a function that named “pascals-triangle” that given an argument n
returns a list of lists represe nting rows 0 ,1,…,n. I.E., ((1) (1 1) (1 2 1) (1 3 3 1) …).
When we refer to a given entry in Pascal's Triangle, we give a row number and a place in that
row, beginning with row zero and place zero. For instance, the numb er 20 appears in row 6, place
3. This value can be computed using a closed formula of factorials:
n!
C(n, k) = -------- for k <=n
k! (n-k)!
Notice that in co mputing n!, k! is computed along the way. For example, C(6,3) = 6!/(3!*(6-3)!)
= 20.
Task 2: Implement a recursive function named “n-choose-k” that implemen ts the C(n,k)
equation above so that no redundant multiplications are needed for any factorial
calculation. (Hint: use let or let* expressions).
Horner’s Method:
In 1819, the mathematician William Horner rediscovered (Isaac Newton discovered it first) a
method for efficiently evaluating an nth degree polynomial in less than the (n2+n)/2
multiplications required if the naïve algorithm suggested by the standard equation for a
polynomial is used:
pf2

Partial preview of the text

Download Assignment #4 - Programming Languages | C S 345 and more Assignments Programming Languages in PDF only on Docsity!

CS 345 Programming Languages Fall 2007 11 / 14 / 07 12 : 57 PM Page 1 of 2

Programming Assignment #4 – Monday, 26 November

Problem Statement: For this programming assignment, you will gain some experience programming in Scheme using basic expressions, lists, list operations, recursive functions, and higher-order functions. Pascal’s Triangle: The philosopher and mathematician Blaise Pascal (1623-1662) is famous among modern day computer scientists for Pascal’s Triangle, and the programming language Pascal was named in his honor. Pascal's Triangle is an arithmetical triangle representing the integer coefficients of the expansion of the binomial equation (x+1)n, for n=0,1,…,n. For example, here is Pascal’s Triangle for n = 0,1,…,7 : n=0 1 n=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 n=7 1 7 21 35 35 21 7 1 To compute Pascal’s Triangle in Scheme start with the list ‘(1). To construct the ith^ row, compute the sum of the first two entries in the row above, then the next entry as the sum of the 2nd^ & 3rd entries, and so on, until the last entry of the row above. To compute the 1’s on the edges, you pretend that each row above the current one is 0 1 … 1 0. Task 1: Implement a function that named “pascals-triangle” that given an argument n returns a list of lists representing rows 0,1,…,n. I.E., ((1) (1 1) (1 2 1) (1 3 3 1) …). When we refer to a given entry in Pascal's Triangle, we give a row number and a place in that row, beginning with row zero and place zero. For instance, the number 20 appears in row 6, place

  1. This value can be computed using a closed formula of factorials: n! C(n, k) = -------- for k <=n k! (n-k)! Notice that in computing n!, k! is computed along the way. For example, C(6,3) = 6!/(3!*(6-3)!) = 20. Task 2: Implement a recursive function named “n-choose-k” that implements the C(n,k) equation above so that no redundant multiplications are needed for any factorial calculation. (Hint: use let or let expressions). Horner’s Method:* In 1819, the mathematician William Horner rediscovered (Isaac Newton discovered it first) a method for efficiently evaluating an nth^ degree polynomial in less than the (n^2 +n)/ multiplications required if the naïve algorithm suggested by the standard equation for a polynomial is used:

CS 345 Programming Languages Fall 2007 11 / 14 / 07 12 : 57 PM Page 2 of 2 Pn(x) = anxn^ + an- 1 xn-^1 + an- 2 xn-^2 + … + a 1 x^1 + a 0 x^0 requires O(n^2 ) multiplications Horner recognized that a polynomial can written in the form: Pn(x) = (((anx + an- 1 )x + an- 2 )x + …)x + a 1 )x + a 0 ) requires O(n) multiplications This form of a polynomial requires 2n+1 multiplications and n additions. From this equation, we can see directly how to implement Horner’s Method as a recursive function Hn(x) for a given list of coefficients a 0 ,a 1 ,…,an , where Hn(x) is defined recursively as: Ho(x) = ao Hi(x) = Hi- 1 (x) * x + ai where 0 <= i <= n To evaluate a polynomial Pn(x) we just compute Hn(x) for a given value of x and a list of n coefficients ai. In Scheme, we can represent the coefficients as a list of integer or real values, depending on whether we need to evaluate the polynomial using integer or real coefficients. For example, a function to evaluate a 4th^ degree polynomial with real coefficients a 0 =1.0 , a 1 =5.0, a 2 =0.0 , and a 3 =3.0 , for some value of x, we be written as: (rec-eval-poly x ‘(1.0 5.0 0.0 3.0)) Task 3: Implement a recursive function named “rec-eval-poly” function using Horner’s method. Task 4: Implement a function named “foldl-eval-poly” using the higher-order foldl function to implement Horner’s method. Task 5: Implement a function named “eval-pascals-triangle” that given arguments n and x, which evaluates the polynomial (x+1)n^ whose coefficients are the nth^ row of Pascal’s Triangle. You must use your n-choose-k function and your evaluation function must allow either the rec-eval-poly or foldl-eval-poly to be given as a function argument. You must use your previously defined functions. It is incorrect to just compute x+1 and then raise it to the power n. Submission Requirements: You are to hand in at the start of class on the due date a complete source code listing of your Scheme program printed using enscript – 2Gr – C - Escheme. You are also to submit an electronic copy of your Scheme source program using the “turnin” program BEFORE class begins on the due date. Late assignments will not be accepted. Honor Policy: This assignment is a individual learning opportunity that will be evaluated based on your ability to think independently, work through a problem in a logical manner and implement a software program on your own. You may however discuss verbally or via email the general nature of the conceptual problem to be solved with your classmates, the course TA or the course instructor, but you are to complete the actual programming for this assignment without resorting to help from any other person or other resources (electronic or otherwise) that are not authorized as part of this course. If in doubt, ask the course instructor. Any indication of improper cooperation or the use of non-approved course materials will result in a grade of zero being assigned for this project and referral to the College for possible disciplinary action with respect to your continued participation in this course.