Download presentation
Presentation is loading. Please wait.
1
CSC 160 Computer Programming for Non-Majors Chapter 2: Numbers, Expressions, and Simple Programs Prof. Adam M. Wittenstein Wittenstein@adelphi.eduhttp://www.adelphi.edu/~wittensa/csc160/
2
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.
3
Section 2.1: Numbers and Arithmetic
4
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).
5
Example 1: Write each mathematical expression in Scheme notation. ● 3 + 4 ● 3 + 4 * 5 ● (3 + 4) * 5
6
Example 1 Solution ● 3 + 4 ● 3 + 4 * 5 ● (3 + 4) * 5 ● (+ 3 4) ● (+ 3 (* 4 5)) ● (* (+ 3 4) 5)
7
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.
8
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)
9
Section 2.2: Variables and Programs
10
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
11
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)
12
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
13
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
14
Example 2: area-of-disk ● Recall the mathematical formula: A = Πr 2 ● 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?
15
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)))
16
Example 2 Testing ● We can now use (area-of-disk 5) to calculate a disk with a radius of 5: (area-of-disk 5)
17
Example 2 Testing ● We can now use (area-of-disk 5) to calculate a disk with a radius of 5: (* 3.14 (* 5 5))
18
Example 2 Testing ● We can now use (area-of-disk 5) to calculate a disk with a radius of 5: (* 3.14 25)
19
Example 2 Testing ● We can now use (area-of-disk 5) to calculate a disk with a radius of 5: 78.5
20
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).
21
Section 2.3: Word Problems
22
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.
23
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))
24
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?
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.