Lecture 7: Developing Algorithms with Nested Loops, Study notes of Computer Science

The notes from lecture 7 of an introductory computer science course. The lecture covers developing algorithms using nested loops, with examples of nested stars and a times table. The students are also introduced to the concept of prime numbers and writing a program to determine if a number is prime.

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-6xy
koofers-user-6xy 🇺🇸

8 documents

1 / 22

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16

Partial preview of the text

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

„^ 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^

Quiz 1 „^ 2 questions „^ A quiz counts as an exercise and you can miss“several” without lowering your score. E.g., ifthere will be 15 exercises in total you can miss 3(about 20%). „^ Answer the quiz using your registered clicker. „^ Honor system: Use only your clicker and don’tconsult your neighbors or notes in any way.

Special Introductory Offer:Just 1 correct answer

for full credit!

February 10, 2009

Lecture 7^

Example: Nested Stars

Draw a black square-Bigger than the biggest star(at least 2 times radius of star)- Center at (0,0)Draw a sequence of stars-Stars alternate in color-Stars get smaller- radius r=1 to startst^ -1star smaller than the sqr-When to stop?- when r is small

February 10, 2009

Lecture 7^

nestedStars.m

February 10, 2009

Lecture 7^

Pattern for doing something

n^ times

n=^

_____

for

k=

1:n

%^ code

to

do

%^ that

something

end

February 10, 2009

Lecture 7^

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=^

while

r^ >

0.^

%r^ still

big

%^ draw

a^ star if^ rem(k,2)==

%odd

number

DrawStar(x,y,r,’m’)

%magenta

elseDrawStar(x,y,r,’y’)

%yellow

end%^ reduce

r r=^ r/1.2;k=^ k^

+^ 1;

end

February 10, 2009

Lecture 7^

Pattern for doing something

n^ times

n=^

_____

for

k=

1:n

%^ code

to

do

%^ that

something

end

Example: Are they prime? „^ Given integers a and b, write a program that listsall the prime numbers in the range [a, b]. „^ Assume a>1, b>1 and a<b.

February 10, 2009

Lecture 7^

Example: Are they prime?Subproblem: Is it prime? „^ Write a program fragment to determine whethera given integer n is prime, n>1. „^ Reminder: rem(x,y) returns the remainder of xdivided by y.

February 10, 2009

Lecture 7^

%Given n, display whether it is primedivisor= 2;while

( rem(n,divisor)~=0 )divisor= divisor + 1;

endif (divisor==n)

fprintf(‘%d is prime\n’, n)

else

fprintf(‘%d is composite\n’, n)

end

for^

n = a:b

end

February 10, 2009

Lecture 7^

Example: Times Table

Write a script to print a times table for aspecified range.

Row headings

Column headings

February 10, 2009

Lecture 7^

Developing thealgorithm for thetimes table

„^ Look for patterns^ „^

Each entry is row#

×^ col#

„^ Row#, col# increase regularly „ ⇒^ Loop!!! „ What kind of loop? „^ for-loop—since the range of theheadings will be specified andincrement regularly „^ for each row#, get the productswith all the col#s. Then go to nextrow# and get products with allcol#s, … „^ ⇒^ Nested loops! „ Details:

what will be the print

format? Don’t forget to start newlines. Also need initial input to specifythe range.

disp('Show the times table for specified range')lo= input('What is the lower bound? ');hi= input('What is the upper bound? ');