CS1112 Lecture 7 - Developing Algorithms and Nested Loops, Study notes of Computer Science

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

Pre 2010

Uploaded on 08/30/2009

koofers-user-1wh-1
koofers-user-1wh-1 🇺🇸

5

(1)

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS1112 Lecture 7 2009/2/10
Lecture slides 1
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
Pattern for doing something ntimes
n= _____
for k= 1:n
% code to do
% that something
end
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
Example: Are they prime?
Given integers aand b, write a program that lists
all the prime numbers in the range [a, b].
Assume a>1, b>1 and a<b.
pf2

Partial preview of the text

Download CS1112 Lecture 7 - Developing Algorithms and Nested Loops and more Study notes Computer Science in PDF only on Docsity!

CS1112 Lecture 7 2009/2/

Lecture slides 1

„ 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

Pattern for doing something n times

n= _____

for k= 1:n

% code to do

% that something

end

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

Example: Are they prime?

„ Given integers a and b, write a program that lists

all the prime numbers in the range [a, b].

„ Assume a>1, b>1 and a<b.

CS1112 Lecture 7 2009/2/

Lecture slides 2

February 10, 2009 Lecture 7 15

Example: Are they prime?

Subproblem: Is it prime?

„ Write a program fragment to determine whether

a given integer n is prime, n>1.

„ Reminder: rem(x,y) returns the remainder of x

divided by y.

Developing the

algorithm for the

times table

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

Find the biggest rectangle

„ Draw 5 rectangles

that the user specifies

using mouse clicks

„ Color the biggest one

red

Here's the biggest rectangle you drew!

February 10, 2009 Lecture 7 29

Find the mode in a sequence

„ 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

The savvy programmer…

„ 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