Presentation is loading. Please wait.

Presentation is loading. Please wait.

Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008.

Similar presentations


Presentation on theme: "Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008."— Presentation transcript:

1 Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008

2 Contents 1.Numbers 2.Expressions 3.Variables 4.Programs 5.The Wage Problem 6.Solution 7.Exercises

3 1. Numbers A positive integer: 5 A negative integer: -5 Fractions: 2/3, 17/3 Real Number : 2.33, 1.56

4 2. Expressions 2.1. Expressions in Scheme 2.2. Supported Expressions 2.3. Nested Expressions 2.4. Hands-On exercises

5 2.1. Expressions in Scheme Scheme expression uses the prefix notations: (operation A... B) Examples: (+ 12 8) (+ 5 5) (+ -5 5) (+ 5 -5) (- 5 5) (* 3 4) (/ 8 12)

6 2.2. Supported Expressions Scheme supports some basic expressions:  (sqrt A): computes (A) 1/2 =  (expt A B): computes A B  (remainder A B): computes the remainder of the integer division A/B  (quotient A B) : Returns the integer portion of a division  …

7 2.3. Nested Expressions As in arithmetic or algebra, expressions can be nested: (2 + 2) * (3 – 5) Express above expression in Scheme? (* (+ 2 2) (- 3 5))

8 2.4. Hands-On Exercises 3 + 4 * 5 ( 2 + 2) * (3 + 5) * 30 ( 3 + 5) * (5 + 7) * 30 / (10 + 2), 5 2, (2 + 1) 3, 7 (2 + 3) (remainder 15 4), (remainder 27 3) (quotient 15 4), (quotient 27 3) Hands-on: Use DrScheme to evaluate the following expressions.

9 3. Variables A variable is a placeholder that stands for an unknown quantity. For example, a disk has the approximate area: 3.14 * r 2 r is a variable which stands for any positive number. r = 5 3.14 * 5 2 = 3.14 * 25 = 78.5

10 4. Programs 4.1. Define and Execute a Program 4.2. Hands-On Exercise

11 4.1 Define and Execute a Program A program is such a rule that tells us and the computer how to produce data from some other data. Program definition in Scheme: Execute a Program:

12 4.2. Hands-on Exercise A.Define the program dollarToEuro, which consumes a number of dollars and produces the euro equivalent. The current exchange rate is 1.5 B.Define a program getCircleArea. It consumes the radius of a circle and produces the area of the circle.

13 5. The Wage Problem The New Age Inc. company pays all its employees $12 per hour. A typical employee works between 20 and 65 hours per week. Develop a program that determines the wage of an employee from the number of hours of work.

14 6. Solution

15 Step 1: Problem description

16 Step 2: Function name

17 Step 3: Writing test cases

18 Step 4: Template

19 Step 5: Implementation

20 Step 6: Testing

21 7. Exercises Exercise 2.1: Rewrite program that estimates the Wage Problem above. Let’s try by yourself to solve the problem in six steps

22 7. Exercises (cont.) Exercise 2.2: Define the program triangle. It consumes the length of a triangle's side and the perpendicular height. The program produces the area of the triangle

23 7. Exercises (cont.) Exercise 2.3: Calculating the area of a ring with given inner and outer radius

24 7. Exercises (cont.) Exercise 2.4: Define the program convert3. It consumes three digits, starting with the least significant digit, followed by the next most significant one, and so on. The program produces the corresponding number. For example, the expected value of (convert3 1 2 3) is 321.

25 References How to Design Programs - Section 02 Matthias Felleisen, Robert Bruce Findler, Matthew Flatt,Shriram Krishnamurthi http://htdp.org/2003-09-26/Book/curriculum-Z-H-5.html#node_chap_2


Download ppt "Section 02 Numbers, Expressions, Simple Programs Version 1.1.0 Prepared by: IT Group Last modified: Apr 04, 2008."

Similar presentations


Ads by Google