Date post: | 20-Dec-2015 |
Category: |
Documents |
View: | 216 times |
Download: | 0 times |
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.
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).
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)
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)
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).
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))