Section 6.1: Structures (continued). Example 1: in-left-side? ; Purpose: To determine whether a point is in the leftmost 150 pixels of the screen. ; Contract.

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Warm Up Example 1 Check whether the ordered pair is a solution of 2x – 3y > -2 a.(0,0) 2(0)-3(0)> -2 0> -2 True b.(0,1) 2(0)-3(1)> -2 -3> -2 False.
Knapsack Problem Section 7.6. Problem Suppose we have n items U={u 1,..u n }, that we would like to insert into a knapsack of size C. Each item u i has.
Natural Numbers or, "All you ever need is compounds, variants, and recursion".
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. Outline Let* List and pairs manipulations –Insertion Sort Abstraction Barriers –Fractals –Mobile 2.
Velocity/Speed Lab. Objective To determine how fast you move! You have to find the speed you and your partners when you: Walk backwards Skip down the.
Graphing Linear Inequalities Section 3.4 MATH Mr. Keltner.
CSC 160 Computer Programming for Non-Majors Lecture #9: Booleans Prof. Adam M. Wittenstein
Section 4.2: Functions that Test Conditions (continued)
CSC 160 Computer Programming for Non-Majors Lecture #5c: Functions with Images Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #8: Animations I Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 6: Structures Prof. Adam M. Wittenstein
Section 4.4: Designing Conditional Functions. REVIEW: Design Recipe – V. 2  Figure out precisely what you need to do. 1. Understand the problem 2. Function.
CSC 160 Computer Programming for Non-Majors Draft Chapter: The UFO Example Prof. Adam M. Wittenstein
Section 2.5: Designing Programs. REVIEW: Design Recipe  Figure out precisely what you need to do.  Tell the computer how to do it.  Check that the.
מבוא מורחב למדעי המחשב בשפת Scheme תרגול 5. Outline Abstraction Barriers –Fractals –Mobile List and pairs manipulations –Insertion Sort 2.
CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 4: Conditional Expressions and Functions Prof. Adam M. Wittenstein
Programming with Images (continued). Another teachpack – “tiles.ss” Download from the website: “ Save.
Algebra 1 Chapter 3 Section 7.
Solving Equations: The Addition and Multiplication Properties
Do Now: Solve the following equations
TeachScheme, ReachJava Adelphi University Tuesday afternoon June 23, 2009.
Local Definitions, Scope, Functional Abstraction, and Polymorphism.
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 5 Sept 4th, 2001 Fall Semester.
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
TeachScheme, ReachJava Adelphi University Tuesday afternoon July 13, 2010.
Scheme: Compound Data Chapter 6 of HTDP Ms. Knudtzon September 19.
CSC 160 Computer Programming for Non-Majors Chapter 8: Scheme Language Review Prof. Adam M. Wittenstein
Compound Data. Last Lecture Booleans Only two elements Symbols Not to be confused with variables Strings Notions of equality on values.
TABLES AND VALUES Section 1.5. Open Sentence Equation.
QuadraticEquation class. Background A quadratic equation is a second-order polynomial equation in a single variable x, ax 2 + bx + c = 0 (with a≠0.)
Textbook Section 6-2.  Students can solve a system of equations using substitution.  Students can classify systems as consistent, inconsistent, dependent,
Combine Like terms Simplify 3x+2x= 3x+2y+2x+7y= 3x+5x-2= 14x+y-2x+4y-7= Slide 1- 2.
1 Topics Recursion sections 8.1 – Recursion A recursively defined sequence –First, certain initial values are specified –Later terms of the sequence.
TeachScheme, ReachJava Adelphi University Thursday morning July 15, 2010.
February ,  2/16: Exam 1 Makeup Papers Available  2/20: Exam 2 Review Sheet Available in Lecture  2/27: Lab 2 due by 11:59:59pm  3/2:
Quadratic Inequalities. Quadratics Before we get started let’s review. A quadratic equation is an equation that can be written in the form, where a, b.
Functional Programming Language 1 Scheme Language: part 2.
Section 4.5 Identity and Inverse Matrices Def: A constant matrix with 1’s on the diagonal and 0’s everywhere else.
CS 100Lecture 191 CS100J Lecture 19 n Previous Lecture –Two dimensional arrays. –Reasonable size problem (a past assignment). –Stepwise refinement. –Use.
Graphing a Linear Inequality
Section 1.1 Introduction to Graphing Copyright ©2013, 2009, 2006, 2001 Pearson Education, Inc.
Graphs of Equations Objective: To use many methods to sketch the graphs of equations.
3.4 Solving Equations with Variables on Both Sides Objective: Solve equations that have variables on both sides.
Functional Programming Language 1 Scheme Language: part 3.
Wholesale Order Form Template -
Areas Between Curves g f a b.
Objectives Solve compound inequalities in one variable involving absolute-value expressions. When an inequality contains an absolute-value expression,
CS100J Lecture 19 Previous Lecture This Lecture
Introduction to Graphing
Graphing Linear Inequalities
Section 1.5 Quadratic Equations
Inequalities in Two Variables
Solution Solution Checking Solutions of Inequalities
Chapter 1 Graphs, Functions, and Models.
Areas Between Curves g f a b.
Graphing Linear Inequalities
Quadratic Inequalities
Quadratic Inequalities
Warm Up Solve. 1. 2x + 9x – 3x + 8 = –4 = 6x + 22 – 4x 3. + = 5
Let's Play "What's the Question"
Graphing Linear Inequalities
SECTION 2-4 : SOLVING EQUATIONS WITH THE VARIABLE ON BOTH SIDES
February , 2009 CSE 113 B.
Introduction to Graphing
Revision I This presentation gives you some hints for solution of selected problems.
Graph Linear Inequalities in Two Variables
Types, Truth, and Expressions
Variables and Equations
Presentation transcript:

Section 6.1: Structures (continued)

Example 1: in-left-side? ; Purpose: To determine whether a point is in the leftmost 150 pixels of the screen. ; Contract : posn -> boolean "Examples of in-left-side?:" (in-left-side? (make-posn 5 7)) "should be" true (in-left-side? (make-posn 200 7)) "should be" false (in-left-side? (make-posn 150 7)) "should be" false

Example 1: in-left-side? ; Purpose: To determine whether a point is in the leftmost 150 pixels of the screen. ; Contract : posn -> boolean (define (in-left-side? where) … where … ) "Examples of in-left-side?:" (in-left-side? (make-posn 5 7)) "should be" true (in-left-side? (make-posn 200 7)) "should be" false (in-left-side? (make-posn 150 7)) "should be" false

Example 1: in-left-side? ; Purpose: To determine whether a point is in the leftmost 150 pixels of the screen. ; Contract : posn -> boolean (define (in-left-side? where) … where … ; a posn … (posn-x where) … ; a number … (posn-y where) … ; a number ) "Examples of in-left-side?:" (in-left-side? (make-posn 5 7)) "should be" true (in-left-side? (make-posn 200 7)) "should be" false (in-left-side? (make-posn 150 7)) "should be" false

Example 1: in-left-side? ; Purpose: To determine whether a point is in the leftmost 150 pixels of the screen. ; Contract : posn -> boolean (define (in-left-side? where) (< (posn-x where) 150) ; a boolean ) "Examples of in-left-side?:" (in-left-side? (make-posn 5 7)) "should be" true (in-left-side? (make-posn 200 7)) "should be" false (in-left-side? (make-posn 150 7)) "should be" false

Example 2: above-diagonal? Takes a point and determines whether it is above the diagonal. The green points return true. The red points return false.

Example 2: above-diagonal? ; Purpose: To determine if a point is above the diagonal. ; Contract: posn -> boolean "Examples of above-diagonal?:" (above-diagonal? (make-posn 5 7)) "should be" false (above-diagonal? (make-posn 200 7)) "should be" true (above-diagonal? (make-posn 7 7)) "should be" false

Example 2: above-diagonal? ; Purpose: To determine if a point is above the diagonal. ; Contract: posn -> boolean (define (above-diagonal? where) ; … where … a posn ) "Examples of above-diagonal?:" (above-diagonal? (make-posn 5 7)) "should be" false (above-diagonal? (make-posn 200 7)) "should be" true (above-diagonal? (make-posn 7 7)) "should be" false

Example 2: above-diagonal? ; Purpose: To determine if a point is above the diagonal. ; Contract: posn -> boolean (define (above-diagonal? where) ; … where … a posn ; … (posn-x where) … a number ; … (posn-y where) … a number ) "Examples of above-diagonal?:" (above-diagonal? (make-posn 5 7)) "should be" false (above-diagonal? (make-posn 200 7)) "should be" true (above-diagonal? (make-posn 7 7)) "should be" false

Example 2: above-diagonal? ; Purpose: To determine if a point is above the diagonal. ; Contract: posn -> boolean (define (above-diagonal? where) (> (posn-x where) (posn-y where)) ) "Examples of above-diagonal?:" (above-diagonal? (make-posn 5 7)) "should be" false (above-diagonal? (make-posn 200 7)) "should be" true (above-diagonal? (make-posn 7 7)) "should be" false

Example 3: distance-to-0 Takes a point and finds its distance from the origin. Hint: the formula is (sqrt((x 1 -x 2 ) 2 + (y 1 -y 2 ) 2 ))

Example 3: distance-to-0 ; Purpose: To determine how far a point is from the origin. ; Contract: posn -> number "Examples of distance-to-0” (distance-to-0 (make-posn 3 4)) "should be" 5 (distance-to-0 (make-posn 0 7)) "should be" 7 (distance-to-0 (make-posn 10 10)) "should be" 141.4…

Example 3: distance-to-0 ; Purpose: To determine how far a point is from the origin. ; Contract: posn -> number ; Skeleton: ; (define (distance-to-0 a-posn) ; …a-posn…) "Examples of distance-to-0” (distance-to-0 (make-posn 3 4)) "should be" 5 (distance-to-0 (make-posn 0 7)) "should be" 7 (distance-to-0 (make-posn 10 10)) "should be" 141.4…

Example 3: distance-to-0 ; Purpose: To determine how far a point is from the origin. ; Contract: posn -> number ; Template: ; (define (distance-to-0 a-posn) ; …a-posn… ; a posn ; … (posn-x a-posn)… ; a number ; … (posn-y a-posn)…) ; a number "Examples of distance-to-0” (distance-to-0 (make-posn 3 4)) "should be" 5 (distance-to-0 (make-posn 0 7)) "should be" 7 (distance-to-0 (make-posn 10 10)) "should be" 141.4…

Example 3: distance-to-0 ; Purpose: To determine how far a point is from the origin. ; Contract: posn -> number (define (distance-to-0 a-posn) (sqrt (+ (sqr (posn-x a-posn)) (sqr (posn-y a-posn)) ))) "Examples of distance-to-0” (distance-to-0 (make-posn 3 4)) "should be" 5 (distance-to-0 (make-posn 0 7)) "should be" 7 (distance-to-0 (make-posn 10 10)) "should be" 141.4…

Example 4: distance Takes two points and finds the distance between them. Hint: the formula is (sqrt((x 1 -x 2 ) 2 + (y 1 -y 2 ) 2 ))

Example 4: distance ; Purpose: To find the distance between two points. ; Contract: distance : posn posn -> number ; (define (distance here there) ; … here … a posn ; … there … a posn ;) "Examples of distance:" (distance (make-posn 4 5) (make-posn 4 5)) "should be 0" (distance (make-posn 4 5) (make-posn 1 5)) "should be 3" (distance (make-posn 4 5) (make-posn 4 17)) "should be 12" (distance (make-posn 4 5) (make-posn 7 9)) "should be 5"

Example 4: distance ; Purpose: To find the distance between two points. ; Contract: distance : posn posn -> number (define (distance here there) ; … here … a posn ; … there … a posn ; … (posn-x here) … a number ; … (posn-x there) … a number ; … (posn-y here) … a number ; … (posn-y there) … a number ) "Examples of distance:" (distance (make-posn 4 5) (make-posn 4 5)) "should be 0" (distance (make-posn 4 5) (make-posn 1 5)) "should be 3" (distance (make-posn 4 5) (make-posn 4 17)) "should be 12" (distance (make-posn 4 5) (make-posn 7 9)) "should be 5"

Example 4: distance ; Purpose: To find the distance between two points. ; Contract: distance : posn posn -> number (define (distance here there) (sqrt (+ (sqr (- (posn-x here) (posn-x there))) (sqr (- (posn-y here) (posn-y there))))) ) "Examples of distance:" (distance (make-posn 4 5) (make-posn 4 5)) "should be 0" (distance (make-posn 4 5) (make-posn 1 5)) "should be 3" (distance (make-posn 4 5) (make-posn 4 17)) "should be 12" (distance (make-posn 4 5) (make-posn 7 9)) "should be 5"

Example 5: Function re-use Define a function that takes in two points and returns their distance plus 2.

Example 5: Function re-use Define a function that takes in two points and returns their distance plus 2. Solution: (define (add2-to-distance here there) (+ 2 (distance here there) )

Example 6: Function re-use Define a function that takes in two points and returns their distance plus a variable “n”.

Example 6: Function re-use Define a function that takes in two points and returns their distance plus 2. Solution: (define (addn-to-distance here there n) (+ n (distance here there) )

In summary… You have seen how to work with a structure that is defined. It is possible to define your own structures, but we will skip this due to time constraints. Next time… Summary of Computer Programming and Scheme