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.
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.
end
keyword. What value does n have now? The number of ways that you can select k objects from n objects is given by the binomial coefficient Thus,
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
Thus, the nth line of output will display n numbers. Make use of the function factorial()
.
Complete the script stepPyramid.m to draw a step pyramid. The base rectangle is L-by-H where H ≤ L. 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.
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.