24
CSC 160 CSC 160 Computer Programming Computer Programming for Non-Majors for Non-Majors Chapter 2: Numbers, Chapter 2: Numbers, Expressions, Expressions, and Simple Programs and Simple Programs Prof. Adam M. Wittenstein Prof. Adam M. Wittenstein [email protected] [email protected] http://www.adelphi.edu/~wittensa/csc160/ http://www.adelphi.edu/~wittensa/csc160/

CSC 160 Computer Programming for Non-Majors Chapter 2: Numbers, Expressions, and Simple Programs Prof. Adam M. Wittenstein [email protected]/csc160

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

CSC 160CSC 160Computer ProgrammingComputer Programming

for Non-Majorsfor Non-Majors

Chapter 2: Numbers, Expressions, Chapter 2: Numbers, Expressions, and Simple Programsand Simple Programs

Prof. Adam M. WittensteinProf. Adam M. Wittenstein

[email protected]@adelphi.edu

http://www.adelphi.edu/~wittensa/csc160/http://www.adelphi.edu/~wittensa/csc160/

Data Types

● Data is either simple (atomic) or compound.● For the next few weeks, we will focus on

simple data: numbers, strings, and images.● The first computers worked only on numbers.● Let’s follow their lead and start with numbers.

Section 2.1: Numbers and Section 2.1: Numbers and ArithmeticArithmetic

Review: Math vs. Scheme

● In math, the expression 3 + 4 * 5 is ambiguous. On first look, we could either add or multiply. (We wind up multiplying because of PEMDAS.)

● In Scheme, we do not have this issue. From the parentheses, there is always one, and only one, next step.

● In math, we use infix notation: 3 + 4.● In Scheme, we use prefix notation: (+ 3 4).

Example 1: Write each mathematical expression in Scheme notation.

● 3 + 4● 3 + 4 * 5● (3 + 4) * 5

Example 1 Solution

● 3 + 4● 3 + 4 * 5● (3 + 4) * 5

● (+ 3 4)● (+ 3 (* 4 5))● (* (+ 3 4) 5)

A Note on Numbers

The book explanation may confuse some of you.That is okay. Here is what you need to know…● Pi (3.14…..) and Square Root of 2 (1.41….) are

two examples of numbers where the digits continue without a pattern.

● Since we cannot go on writing indefinitely, we round them off to just a few decimal places, say 3.14 or 1.4.

● When Scheme has rounded a number, it puts #i before the number.

A First Data Type: Numbers

So far we have seen one data type:● Number

– integer, e.g. 7, -12– fraction, e.g. 5/9– floating-point, e.g. 3.14159

Soon we will work with other types of data including:● String, e.g. “hello there”● Images, e.g. a rectangle, a picture of me (or you)

Section 2.2: Variables and Section 2.2: Variables and ProgramsPrograms

What is a variable?

● A variable, like x, is a placeholder in an expression that stands for some fixed unknown quantity.

● x can be used to represent any number. If we find a value for it, (such as 7), we can evaluate the expression

● 6 * x + 2

What is a variable?

● A variable, like x, is a placeholder in an expression that stands for some fixed unknown quantity.

● x can be used to represent any number. If we find a value for it, (such as 7), we can evaluate the expression

● 6 * 7 + 2 (remember to use the order of operations, also known as PEMDAS)

What is a variable?

● A variable, like x, is a placeholder in an expression that stands for some fixed unknown quantity.

● x can be used to represent any number. If we find a value for it, (such as 7), we can evaluate the expression

● 42 + 2

What is a variable?

● A variable, like x, is a placeholder in an expression that stands for some fixed unknown quantity.

● x can be used to represent any number. If we find a value for it, (such as 7), we can evaluate the expression

● 44

Example 2: area-of-disk

● Recall the mathematical formula: A = Πr2

● This means that area is 3.14 * r * r.

● For example, what is the area when r = 5?

● A = 3.14 x 5 x 5 = 78.5

● How do we write this in Scheme?

Example 2 Solution

● For r = 5, it is: (* 3.14 5 5).

● For any r, it is: (* 3.14 r r).

● So we can now write the Scheme function for area-of-disk:

(define (area-of-disk r) (* 3.14 (* r r)))

Example 2 Testing

● We can now use (area-of-disk 5) to calculate a disk with a radius of 5:

(area-of-disk 5)

Example 2 Testing

● We can now use (area-of-disk 5) to calculate a disk with a radius of 5:

(* 3.14 (* 5 5))

Example 2 Testing

● We can now use (area-of-disk 5) to calculate a disk with a radius of 5:

(* 3.14 25)

Example 2 Testing

● We can now use (area-of-disk 5) to calculate a disk with a radius of 5:

78.5

When you're Writing a Function…

● Function names cannot contain spaces.● You must spell the function name exactly the same

way in the program and when testing.● You must spell the parameter name exactly the

same way in function header and function body.● You cannot assume that the input will be any

specific number (like 2, or 7, or …)● Refer to the input only by the parameter name (e.g. days), so it works on whatever input is actually provided (stuck into the envelope).

Section 2.3: Word ProblemsSection 2.3: Word Problems

Why look at word problems?

● Programmers are usually given word problems and not mathematical expressions to solve.

● Sometimes the description is ambiguous and may contain irrelevant information.

● The first job is to extract the important information from the description.

Example 3: wage● Company XYZ & Co. pays all its employees $12 per

hour. Develop a program to determine an employee’s gross pay.

● The last sentence tells us that the goal is to find the gross pay.

● We know that gross = Rate * Hours or 12*h in this case.

● So the Scheme program (which is just one Scheme function) is:

(define (wage h) (* 12 h))

In summary…● We have reviewed the basic rules of writing

Scheme functions.● We have written and tested some Scheme

functions that involve numbers.

Next time…● Most of you have gotten error messages in

DrScheme. What do they mean?● How can you reduce the number of error

messages you get?