Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to Computer Science Lab 1: Numbers and Arithmetic.

Similar presentations


Presentation on theme: "Introduction to Computer Science Lab 1: Numbers and Arithmetic."— Presentation transcript:

1 Introduction to Computer Science Lab 1: Numbers and Arithmetic

2 Numbers and Arithmetic Simple Expressions (+ 5 5) (+ -5 5) (+ 5 -5) (- 5 5) (* 3 4) (/ 8 12)

3 Numbers and Arithmetic As in arithmetic or algebra, we can nest expressions: (* (+ 2 2) (/ (* (+ 3 5) (/ 30 10)) 2))

4 Numbers and Arithmetic (* (+ 2 2) (/ (* (+ 3 5) (/ 30 10)) 2)) = (* 4 (/ (* 8 3) 2)) = (* 4 (/ 24 2)) = (* 4 12) = 48

5 Numbers and Arithmetic advanced mathematical operations on numbers 1. (sqrt A) computes (A) expt. 1/2; 2. (expt A B) computes A expt. B; 3. (remainder A B) computes the remainder of the integer division A/B; 4. (log A) computes the natural logarithm of A; and 5. (sin A) computes the sine of A radians.

6 Variables and Programs In algebra we learn to formulate dependencies between quantities using VARIABLE EXPRESSIONS A variable is a placeholder that stands for an unknown quantity. For example, a disk of radius r has the approximate area pi * r * r

7 Variables and Programs Expressions that contain variables are rules that describe how to compute a number when we are given values for the variables A program is such a rule. It is a rule that tells us and the computer how to produce data from some other data (define (area-of-disk r) (* 3.14 (* r r)))

8 Variables and Programs The two lines say that area-of-disk is a rule, that it consumes a single INPUT, called r, and that the result, or OUTPUT, is going to be (* 3.14 (* r r)) once we know what number r represents.

9 Variables and Programs (area-of-disk 5) = (* 3.14 (* 5 5)) = (* 3.14 25) = 78.5

10 Variables and Programs Many programs consume more than one input. Say we wish to define a program that computes the area of a ring, that is, a disk with a hole in the center:

11 Variables and Programs (define (area-of-ring outer inner) (- (area-of-disk outer) (area-of-disk inner)))

12 Variables and Programs ;; 1. Contract: area-of-ring : number number -> number ;; 2. Purpose: to compute the area of a ring whose radius is ;; outer and whose hole has a radius of inner ;; 4. Definition (define (area-of-ring outer inner) (- (area-of-disk outer) (area-of-disk inner))) ;; 3. Tests: (check-expect (area-of-ring 5 3) 50.24)


Download ppt "Introduction to Computer Science Lab 1: Numbers and Arithmetic."

Similar presentations


Ads by Google