Numbers, Expressions, Simple Programs Section 02 Numbers, Expressions, Simple Programs Prepared by: IT Group Last modified: Apr 10, 2009 1
Contents The Area of Disk Problem Exercises Theory Part Numbers Expressions Variables Program 2
1. The Area of Disk Problem Develop a program that computes the area of a disk with a given radius.
Solution 4
Step 1: Problem Description 5
Step 2: Function Name 6
Step 3: Test Cases When developing the test cases, we apply the following formula: 3.14 * r2 7
Step 4: Function Template 8
Step 5: Implementation 9
Step 6: Testing 10
2. Exercises 2.1. The Wage Problem 2.2. The Area of Triangle Problem 2.3. The Three Digits Conversion Problem
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
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.
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 1 2 3 is 321.
3. Theory Part Numbers Expressions Variables Program
3.1. Numbers A positive integer: 5 A negative integer: -5 Fractions: 2/3, 17/3 Real Number: 2.33, 1.56 16
3.2. Expressions Expressions in Scheme Advanced Expressions Nested Expressions
3.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) 18
3.2.2. 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
3.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)) 20
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
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
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 23