Recursive Numerical Functions in Sifflet
- 9 points
- Open Wed 2012 Oct 24, due Wed 2012 Nov 7
Overview
Define and test two recursive functions in Sifflet.
This lab is for the assessment of the competency 07-3 (SiffRecNum).
Problems
Coding Standards through Chapter 9 apply to this lab. While the coding standards are actually for Python code, apply the spirit of the standards also to Sifflet programs—for example, use descriptive variable and function names.
Problem 1: Summing a Function
Define and test a Sifflet function which computes the sum of the terms f(x) = x2 + 3x + 7, for x = 1 to n. You should use a combination of functions. The parameter of your summing function is n. The other function should compute f(x).
Hints:
- Base case: What is the sum of zero terms (i.e., when n = 0)?
- Recursive step: What is the relation between the sum of n terms and the sum of n − 1 terms?
Testing:
- Test your summing function with n = 5.
- Testing implies that you should check the result of the computation; if it is not correct, then fix your function!
- Turn in one screenshot showing both of the functions—the summing function (top level only) and the f(x) function—when n = 5, x = 5.
Problem 2: power
Define a Sifflet function which computes xn for parameters x and n, where x may be any number and n is any integer ≥ 0.
Hints:
- The base case: x0 = 1, for any x.
- For the recursive step, consider the relation between xn and xn−1. For example, x4 = x ⋅ x ⋅ x ⋅ x = x ⋅ (x ⋅ x ⋅ x) = x ⋅ x3.
Testing:
- Use your function to compute 36.
- Make sure the result is correct.
- Take a screenshot of the computation (just the top-level view, not expanded) and turn it in.
Turning in Your Work
Turn in the following in Oncourse Assignments:
- Summing function: one screenshot showing both functions when n = 5, x = 5.
- Power function: one screenshot showing the top-level computation of 36.
Grading and Assessment
(CA = Competency Assessment, all or nothing; pca = partial credit is available)
- 3 points (pca) for correctness of summing a function.
- 3 points (pca) for correctness of power function.
07-3 (SiffRecNum) RECURSION CA SUBTOTAL: 6 points, 5 to "pass." This is not recorded separately, and it is not "all or nothing." If you earn significantly less than passing, you may get a chance to resubmit; but do try to get it all right the first time!
- 2 points (pca) for two required test cases.
- 1 point (pca) for style.
Total: 9 points