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.

Slides:



Advertisements
Similar presentations
Scheme: Functions Chapter 3 of HTDP Ms. Knudtzon September 12.
Advertisements

Factoring Test Review. #1: Factor Completely Answer: Guess and Check.
VARIABLES AND DEBUGGING Beginning Programming. Assignment Statements  Used to hold values in a variable  Calculates a result and stores it in a variable.
Chapter 10: Estimating with Confidence
Hypothesis Testing I 2/8/12 More on bootstrapping Random chance
Announcements Wednesday's test will be postponed until Thursday – July 7 th, See me if this won’t work for you. Project 1 will be due Monday – July.
Measurement: errors, accuracy, and precision
CSC 160 Computer Programming for Non-Majors Lecture #5c: Functions with Images Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 6: Structures Prof. Adam M. Wittenstein
Class 5: Thurs., Sep. 23 Example of using regression to make predictions and understand the likely errors in the predictions: salaries of teachers and.
CSC 160 Computer Programming for Non-Majors Lecture #11: Conditionals II Prof. Adam M. Wittenstein
Section 2.4: Errors. Common errors ● Mismatched parentheses ● Omitting space after operator or between numbers ● Putting operator between operands.
Ten debugging techniques. The execution process Execution proceeds in a standard series of steps Compute values of subexpressions first Then call value.
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.
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.
Accuracy of Prediction How accurate are predictions based on a correlation?
Section 4.3: Conditions and Conditional Functions.
CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #5b: Designing Programs Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 3: Programs are Functions Plus Variable Definitions Prof. Adam M. Wittenstein
Main task -write me a program
Chapter 10: Estimating with Confidence
The graphing calculator can be a great checking tool or a fast way to answer a multiple choice question. Example – suppose you graphed the following and.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.1 Chapter 4 Loops.
Fundamentals of Python: From First Programs Through Data Structures
Lecture 15 Practice & exploration Subfunctions: Functions within functions “Guessing Game”: A hands-on exercise in evolutionary design © 2007 Daniel Valentine.
1 I.Introduction to Algorithm and Programming Algoritma dan Pemrograman – Teknik Informatika UK Petra 2009.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Fundamentals of Python: First Programs
Chapter 12: How Long Can This Go On?
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Which program is better? Why? (define (prime? n) (= n (smallest-divisor n))) (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n d)
Testing and Debugging Version 1.0. All kinds of things can go wrong when you are developing a program. The compiler discovers syntax errors in your code.
Design Recipe Team Challenge! 1)Every team submits their answer in writing. 2)Every team can gain points for every question. 3)Wrong answers are worth.
Introduction to Inferece BPS chapter 14 © 2010 W.H. Freeman and Company.
Software Testing and Quality Assurance Practical Considerations (4) 1.
Georgia Institute of Technology More on Creating Classes part 2 Barb Ericson Georgia Institute of Technology Oct 2005.
Sampling distributions rule of thumb…. Some important points about sample distributions… If we obtain a sample that meets the rules of thumb, then…
Functional Programming Language 1 Scheme Language: part 2.
Chapter 6 Test Prep 6-3 through 6-5: Solving Quadratic Equations 6-6: Graphing Quadratic Functions Application Problems Choose a section to work on. At.
Intermediate 2 Computing Unit 2 - Software Development.
An importer of Herbs and Spices claims that average weight of packets of Saffron is 20 grams. However packets are actually filled to an average weight,
Chapter 5 Part 1 Test Prep 5-1: Monomials 5-2: Polynomials 5-3: Division of Polynomials 5-4: Factoring Choose a section to work on. At any time you may.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
An Introduction to Programming with C++ Sixth Edition
1 Introduction 1. Why Data Structures? 2. What AreData Structure? 3. Phases of Software Development 4. Precondition and Postcondition 5. Examples.
Debugging and Printing George Mason University. Today’s topics Review of Chapter 3: Printing and Debugging Go over examples and questions debugging in.
Example A population has a mean of 200 and a standard deviation of 50. A random sample of size 100 will be taken and the sample mean x̄ will be used to.
Consider a principal P invested at rate r compounded annually for n years: Compound Interest After the first year: so that the total is now 1.
Unit 6 Job Satisfaction (Grammar)  Past simple vs. present perfect : both used to talk about past actions.  Past simple : is used when the time of the.
Lecture #5 מבוא מורחב.
Bingo Bingo Summary of questions Answers.
Creating Functions with Parameters
Higher-Order Procedures
Your turn (Review) What does a lambda expression return when it is evaluated? the value of a lambda expression is a procedure What three things are in.
Purpose of Looks Like - Sounds Like Exercise
Lecture #5 מבוא מורחב.
goteachmaths.co.uk Spot the Mistake – Fractions to Percent
Introduction to TouchDevelop
Chapter 5 Loops.
Writing Functions( ) (Part 4)
Confidence intervals for the difference between two means: Paired samples Section 10.3.
Lecture 12: Message passing The Environment Model
Exercise Solution First questions What's output What's input
Mobile Computing With Android ACST 4550 Toast
Let's Play "What's the Question"
The first number is posted telling what random number was selected, I did this for testing purposes, in the real thing it would not be there. Since the.
SECTION 4-3 Check Registers pp
U3L8 Creating Functions with Parameters
Presentation transcript:

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 contract 3. NEW STEP – Data Analysis 4. Write examples (in Scheme notation)  Tell the computer how to do it. 5. Write a skeleton 6. NEW STEP – Choose a template 7. Fill in the function body  Check that the computer does it right. 8. Testing and debugging

REVIEW: Question 2 - “interest-rate” (define (interest-rate amount) (cond [(<= amount 1000).040] [(<= amount 5000).045] [(<= amount 10000).055] [( > amount 10000).060]))

Question 3: Testing a Program Suppose one of your classmates wrote these examples for interes-rate. Test the program with each of these examples: (interest-rate 4000) ->.045 (interest-rate 20000) ->.060 (interest-rate 6000) ->.040

Question 3: Testing a Program Testing and Debugging -- As always, type each of your examples into the interactions window. --Normally, if you get an error message or unexpected answer, debug the program to find your mistake.

Question 3: Testing a Program Which example did not return the expected answer? Is there a mistake in the program? What step in the design recipe did he / she make the mistake?

Question 3: Testing a Program Which example did not return the expected answer? the third one Is there a mistake in the program? What step in the design recipe did he / she make the mistake?

Question 3: Testing a Program Which example did not return the expected answer? the third one Is there a mistake in the program? no What step in the design recipe did he / she make the mistake?

Question 3: Testing a Program Which example did not return the expected answer? the third one Is there a mistake in the program? no What step in the design recipe did he / she make the mistake? writing the example So we see that it is possible that the mistake is in the example rather than the actual Scheme function.

Exercise 5.1.2: check-guess Develop the function check-guess. It consumes two numbers, guess and target. Depending on how guess relates to target, the function produces one of the following three answers: “Too Small”, “Perfect”, or “Too Large”.

;Purpose: To check the guess of a random number. ;Contract: number number -> string Exercise 5.1.2: check-guess

;Purpose: To check the guess of a random number. ;Contract: number number -> string ;Data Analysis: ;We take in 2 numbers and determine which of 3 intervals it ;is in. ;We print out a different string for each of the intervals. Exercise 5.1.2: check-guess

;Purpose: To check the guess of a random number. ;Contract: number number -> string ;Examples: (check-guess 5 10) “should be” “Too Small” (check-guess 12 6) “should be” “Too Large” (check-guess -3 -3) “should be” “Perfect” Exercise 5.1.2: check-guess

;Purpose: To check the guess of a random number. ;Contract: number number -> string ;Skeleton: ;(define (check-guess guess target) ; (… guess … target …)) ;Examples: (check-guess 5 10) “should be” “Too Small” (check-guess 12 6) “should be” “Too Large” (check-guess -3 -3) “should be” “Perfect” Exercise 5.1.2: check-guess

;Purpose: To check the guess of a random number. ;Contract: number number -> string ;Template (define (check-guess guess target) (cond [question … answer …] [question … answer …])) ;Examples: (check-guess 5 10) “should be” “Too Small” (check-guess 12 6) “should be” “Too Large” (check-guess -3 -3) “should be” “Perfect” Exercise 5.1.2: check-guess

;Purpose: To check the guess of a random number. ;Contract: number number -> string ;Fill in the Questions (define (check-guess guess target) (cond [(= guess target) …answer…] [(> guess target) …answer…] [(< guess target) …answer…])) ;Examples: (check-guess 5 10) “should be” “Too Small” (check-guess 12 6) “should be” “Too Large” (check-guess -3 -3) “should be” “Perfect” Exercise 5.1.2: check-guess

;Purpose: To check the guess of a random number. ;Contract: number number -> string ;Function body (define (check-guess guess target) (cond [(= guess target) “Perfect”] [(> guess target) “Too Large”] [(< guess target) “Too Small”])) ;Examples: (check-guess 5 10) “should be” “Too Small” (check-guess 12 6) “should be” “Too Large” (check-guess -3 -3) “should be” “Perfect” Exercise 5.1.2: check-guess

In summary… Both the interest-rate and check-guess functions need to determine which of several conditions holds for the input. We call them conditional functions. On to compound data… --Posns: A First Structure --Creating Your Own Structures Next time…