

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The slides from lecture 7 of the cs1112 course, held on february 10, 2009. The lecture covers developing algorithms, nested loops, and includes examples on nested stars, prime numbers, and the times table. Announcements for a computer lab session, project 2 deadline, and prelim 1 exam are also included.
Typology: Study notes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Previous Lecture: Iteration using while Introduce nested loops
Today’s Lecture: Developing algorithms Nested loops
Announcements: Section in the computer lab this week Project 2 due 2/12 (Thurs) at 11pm Prelim 1 on 2/19 (Thurs) 7:30pm. If you have a conflict, tell us (email Bill Hogan) now—no later than 2/12.
February 10, 2009 Lecture 7 5
Example: Nested Stars
February 10, 2009 Lecture 7 8
Knowing how to draw
How difficult is it to draw
February 10, 2009 Lecture 7 9
February 10, 2009 Lecture 7 10
x=0; y=0; % figure centered at (0,0) s= 2.1; % side length of square DrawRect(x-s/2,y-s/2,s,s,’k’) r= 1; k= 1; while r > 0.1 %r still big % draw a star if rem(k,2)==1 %odd number DrawStar(x,y,r,’m’) %magenta else DrawStar(x,y,r,’y’) %yellow end % reduce r r= r/1.2; k= k + 1; end
February 10, 2009 Lecture 7 15
disp('Show the times table for specified range') lo= input('What is the lower bound? '); hi= input('What is the upper bound? ');
February 10, 2009 Lecture 7 26
Here's the biggest rectangle you drew!
February 10, 2009 Lecture 7 29
A mode is the number in a sequence that appears the most number of times Develop an algorithm for calculating the mode of a user- entered sequence that is Non-negative Entered one-by-one in non-decreasing order Terminated by a negative number E.g., sequence 87, 92, 92, 98, 98, 98, 100 has a mode… Write the algorithm and then the code on your own for practice!
February 10, 2009 Lecture 7 31
Learns useful programming patterns and use them where appropriate Seeks inspiration by working through test data “by hand” Asks, “What am I doing?” at each step Sets up a variable for each piece of information maintained when working the problem by hand Decomposes the problem into manageable subtasks Refines the solution iteratively, solving simpler subproblems first Remembers to check the problem’s boundary conditions Validates the solution (program) by trying it on test data