Developing Algorithms - Lecture Slides | CS 1112, Study notes of Computer Science

Material Type: Notes; Class: Introduction to Computing Using MATLAB; Subject: Computer Science; University: Cornell University; Term: Fall 2008;

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-rgz
koofers-user-rgz 🇺🇸

10 documents

1 / 2

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS1112 Lecture 7 2008/9/18
Lecture slides 1
Previous Lecture:
Iteration using while
Today’s Lecture:
Developing algorithms
Nested loops
Announcements:
Project 2 due 9/22 (Mon) at 6pm
Prelim 1 on 9/25 (Thurs) 7:30pm. If you have a conflict,
tell us (email Kelly Patwell) now—no later than
tomorrow.
You’ll use your clicker for a quiz soon—register it!
Example: Nested Stars
Draw a black square
- Bigger than the biggest star
-Center at (0,0)
- Make side length 2.1
Draw a sequence of stars
-Stars get smaller
-Stars alternate in color
-1st star smaller than the sqr
-radius r=1 to start
-When to stop?
-when r small
September 18, 2008 Lecture 7 9
Example: Is it prime?
Write a program fragment to determine whether
a given integer nis prime. Assume n>1.
Reminder: rem(x,y) returns the remainder of x
divided by y.
Example: Are they prime?
Given integers a and b, sketch a program that
lists all the prime numbers in the range [a, b].
Assume a,b>1 and a<b.
September 18, 2008 Lecture 7 19
Example: Times Table
4942352821
4236302418
3530252015
2824201612
211815129
Write a script to print a times table for a
specified range.
76543
7
6
5
4
3
Row headings
Column headings
pf2

Partial preview of the text

Download Developing Algorithms - Lecture Slides | CS 1112 and more Study notes Computer Science in PDF only on Docsity!

CS1112 Lecture 7 2008/9/

Lecture slides 1

„ Previous Lecture: „ Iteration using while

„ Today’s Lecture: „ Developing algorithms „ Nested loops

„ Announcements: „ Project 2 due 9/22 (Mon) at 6pm „ Prelim 1 on 9/25 (Thurs) 7:30pm. If you have a conflict, tell us (email Kelly Patwell) now—no later than tomorrow. „ You’ll use your clicker for a quiz soon—register it!

Example: Nested Stars

Draw a black square

  • Bigger than the biggest star
  • Center at (0,0)
  • Make side length 2.

Draw a sequence of stars -Stars get smaller -Stars alternate in color -1 st^ star smaller than the sqr -radius r=1 to start -When to stop?

  • when r small

September 18, 2008 Lecture 7 9

Example: Is it prime?

„ Write a program fragment to determine whether

a given integer n is prime. Assume n>1.

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

divided by y.

Example: Are they prime?

„ Given integers a and b, sketch a program that

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

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

September 18, 2008 Lecture 7 19

Example: Times Table

Write a script to print a times table for a

specified range.

Row headings

Column headings

CS1112 Lecture 7 2008/9/

Lecture slides 2

Developing the

algorithm for the

times table

disp('Show the times table for a specified range')

lo= input('What is the lower bound? ');

hi= input('What is the upper bound? ');

September 18, 2008 Lecture 7 23

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!

September 18, 2008 Lecture 7 26

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!

September 18, 2008 Lecture 7 28

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