Presentation is loading. Please wait.

Presentation is loading. Please wait.

Numbers, Expressions, Simple Programs

Similar presentations


Presentation on theme: "Numbers, Expressions, Simple Programs"— Presentation transcript:

1 Numbers, Expressions, Simple Programs
Section 02 Numbers, Expressions, Simple Programs Prepared by: IT Group Last modified: Apr 10, 2009 1

2 Contents The Area of Disk Problem Exercises Theory Part Numbers
Expressions Variables Program 2

3 1. The Area of Disk Problem
Develop a program that computes the area of a disk with a given radius.

4 Solution 4

5 Step 1: Problem Description
5

6 Step 2: Function Name 6

7 Step 3: Test Cases When developing the test cases, we apply the following formula: 3.14 * r2 7

8 Step 4: Function Template
8

9 Step 5: Implementation 9

10 Step 6: Testing 10

11 2. Exercises 2.1. The Wage Problem 2.2. The Area of Triangle Problem
2.3. The Three Digits Conversion Problem

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

13 2.2. The Area of Triangle Problem
Develop the program that consumes the length of a triangle's side and the perpendicular height. The program produces the area of the triangle.

14 2.3. The Three Digits Conversion Problem
Develop a program that converts 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 converting is 321.

15 3. Theory Part Numbers Expressions Variables Program

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

17 3.2. Expressions Expressions in Scheme Advanced Expressions
Nested Expressions

18 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) 18

19 Advanced Expressions Scheme provides some advanced mathematical expressions: (sqrt A): computes (A)1/2 = (expt A B): computes AB (remainder A B): computes the remainder of the integer division A/B (quotient A B) : Returns the integer portion of a division 19

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

21 3.3. Variables A variable is a placeholder that stands for an unknown quantity. For example, a disk has the approximate area: 3.14 * r2 r is a variable which stands for any positive number. If r = 5 then the area of disk = 3.14 * 52 = 78.5

22 3.4. 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: The naming for a function is very important 22

23 References How to Design Programs - Section 02
Matthias Felleisen, Robert Bruce Findler, Matthew Flatt,Shriram Krishnamurthi 23


Download ppt "Numbers, Expressions, Simple Programs"

Similar presentations


Ads by Google