CS 1112 Exercise 4

Due date: Sunday, March 7, 11:00pm EST.

The exercise must be submitted on CMS (Exercise 4).

Remember to keep your files organized! Start a directory (folder) for this exercise, Ex4. When downloading a file, do a right-click (Windows) or ctrl-click (Mac) on the file name and select save link as to control exactly where your file will go!

Objectives for this exercise: master while-loop and nested loops and practice program development: (1) reading a problem with abstract/mathematical notations, (2) understanding it by writing out small/short examples, (3) laying out the program “skeleton” by choosing appropriate loop and/or conditional structures, and (4) finally filling in the detailed computation.

Fibonacci numbers

You should read Insight §3.2 before doing the following exercise. Download the script Fibonacci:

% Fibonacci
clc
f_old= 0;
f_cur= 1;
n= 1;
% f_cur is the nth Fibonacci number
while n <= 10
    fprintf('%2d %10d\n', n, f_cur)
    % Update:
    f_new= f_old + f_cur;
    f_old= f_cur;
    f_cur= f_new;
    n= n + 1;
end

It displays the ten Fibonacci numbers f1, …, f10.

  1. Perform the following tasks using Matlab’s debugger and write your observations in a file lab04.txt:
    1. Add a breakpoint to the line where n is incremented, then run the script. When it pauses, you will see a green arrow next to that line. Look at the value of n in the Workspace; did Matlab pause before or after executing that line?
    2. Step the program once; the green arrow should move next to the end keyword. What value does n have now?
    3. Continue the program; it should stop at your breakpoint again. Which variables have changed?
    4. Use the Command Window to assign to n the value 10, then continue the program. How many more iterations of the loop body were run before the program exited?
    5. Remove the breakpoint. Discuss with your neighbor/group what you were able to use the debugger to do.
  2. Save the file as fib1mil.m (you will need the original file for part (c)). Modify the script so that it prints all Fibonacci numbers that are greater than ten thousand but less than one million.
  3. (finish questions 2 & 3 first) Start from the original file Fibonacci.m. Modify the script so that it prints the smallest n such that: |fn+1fn1+52|0.000001\left| \frac{f_{n+1}}{f_n} - \frac{1 + \sqrt{5}}{2} \right| \leq 0.000001

Binomial coefficients

The number of ways that you can select k objects from n objects is given by the binomial coefficient (nk)=n!k!(nk)!{n \choose k} = \frac{n!}{k!(n-k)!} Thus, (101)=10!1!9!=10{10 \choose 1} = \frac{10!}{1!9!}=10 (102)=10!2!8!=45{10 \choose 2} = \frac{10!}{2!8!}=45 (103)=10!3!7!=120{10 \choose 3} = \frac{10!}{3!7!}=120

Recall that if x houses a positive integer, then the value of floor(log10(x))+1 is the number of base-10 digits that are required to write the value of x. Write a script bin10 that produces ten lines of output. The nth line should display the number of digits required to write down each of the binomial coefficients (n1),(n2),,(nn){n \choose 1}, {n \choose 2}, \ldots, {n \choose n} Thus, the nth line of output will display n numbers. Make use of the function factorial().

Build your own step pyramid

Complete the script stepPyramid.m to draw a step pyramid. The base rectangle is L-by-H where HL. Each step has the same height H. The next rectangle up is 2/3 the length of the rectangle below, and so forth. The top step must have a length no less than H.

step pyramid

You will need function DrawRect()—download it from the Insight page of the course website and put it in your current directory (the directory from which you will run your script). Use a while-loop.

Submit your files lab04.txt, fib1mil.m, Fibonacci.m, bin10.m, and stepPyramid.m on CMS.