CSC 160 Computer Programming for Non-Majors Chapter 2: Numbers, Expressions, and Simple Programs Prof. Adam M. Wittenstein

Slides:



Advertisements
Similar presentations
Surds Surds are a special type of number that you need to understand and do calculations with. The are examples of exact values and are often appear in.
Advertisements

The Order of Operations
CSC 160 Computer Programming for Non-Majors Lecture #3a: Stepper, Words, Sentences (continued) Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #9: Booleans Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #4: Defining Variables Prof. Adam M. Wittenstein
Computer Science 1620 Arithmetic. C++ Math we have seen how to use numbers in our program to represent data however, we can also manipulate this data.
CSC 160 Computer Programming for Non-Majors Lecture #5c: Functions with Images Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #5: Defining Functions Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 6: Structures Prof. Adam M. Wittenstein
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
CSC 160 Computer Programming for Non-Majors Lecture #3: Calling Functions Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #3c: Working with Pictures (continued) Prof. Adam M. Wittenstein
Section 1.1: Flying a UFO (continued). Conversion recipe 1)Expand abbreviations 2)Parenthesize all subexpressions that have an operator. Should end up.
CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
Python Programming Chapter 2: Variables, expressions, and statements Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
CSC 160 Computer Programming for Non-Majors Chapter 4: Conditional Expressions and Functions Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #7: Variables Revisited Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 3: Programs are Functions Plus Variable Definitions Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #2: What are Functions? Prof. Adam M. Wittenstein
Ch. 3.1 – Measurements and Their Uncertainty
Prepared by Doron Shahar
CSC 160 Computer Programming for Non-Majors Lecture #3a: Stepper, Words, Sentences Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #5a: Defining Functions on Words and Sentences Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #6: Function Composition Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #3b: Working with Pictures Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #5 (continued): More on Writing Functions Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #12: Syntax and Semantics Prof. Adam M. Wittenstein
Elementary Algebra Exam 4 Material Exponential Expressions & Polynomials.
Identity and Equality Properties. © William James Calhoun To recognize and use the properties of identity and equality, and to determine the multiplicative.
Fractional Exponents and Radicals
Properties of Logarithms
Real Numbers and Algebra
2/4/2003CSCI 150: Introduction to Computer Science1 Introduction to Computer Science CSCI 150 Section 002 Session 6 Dr. Richard J. Bonneau IONA Technologies.
Chapter 3 Scientific Measurement 3.1 Using and Expressing Measurements
Chapter P Prerequisites: Fundamental Concepts of Algebra
Variables and Exponents
Physics 521 Math Review SCIENTIFIC NOTATION Scientific Notation is based on exponential notation (where decimal places are expressed as a power of 10).
Functions Copyright © J. Mercer, A function is a number-machine that transforms numbers from one set called the domain into a set of new numbers.
Section 1Chapter 5. 1 Copyright © 2012, 2008, 2004 Pearson Education, Inc. Objectives Integer Exponents and Scientific Notation Use the product.
Scientific Measurement. Measurements are fundamental to the experimental sciences.  Measurement: A quantity that has both a number and a unit.  Scientific.
Multiplying & Dividing Real Numbers MATH 018 Combined Algebra S. Rook.
Copyright © 2014, 2010, 2006 Pearson Education, Inc. 1 Chapter 1 Introduction to Functions and Graphs.
Copyright © 2010 Pearson Education, Inc. All rights reserved Sec
CSC 221 Computer Organization and Assembly Language
CSC 160 Computer Programming for Non-Majors Chapter 8: Scheme Language Review Prof. Adam M. Wittenstein
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
3.1 Using and Expressing Measurements > 1 Copyright © Pearson Education, Inc., or its affiliates. All Rights Reserved. Chapter 3 Scientific Measurement.
Functions CSE 1310 – Introduction to Computers and Programming Vassilis Athitsos University of Texas at Arlington 1.
CONFIDENTIAL1 Warm-up: 1.) = 2.) = 3.)9 - 6 ÷ 3 = 4.)4 + 5 x 8 = 5.) = Today we will learn How to Evaluate Expressions.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1 Chapter 3.
Real numbers In algebra, we work with the set of real numbers, which we can model using a number line. Real numbers describe real-world quantities such.
Functional Programming Language 1. A program is: Composition of operations on data Characteristics (in pure form): –Name values, not memory locations.
Solve each equation and check the solution. 08 September 2015 Warm-Up.
Introduction to Computer Science Lab 1: Numbers and Arithmetic.
Section 02 Numbers, Expressions, Simple Programs Version Prepared by: IT Group Last modified: Apr 04, 2008.
Fractional Exponents. Careful! Calculate the following in your calculator: 2 ^ ( 1 ÷ 2 ) Not Exact.
The Science of Physics Chapter #1 Ms. Hanan Anabusi.
Set up clickers Significant Figures Finish up measurement lab Go over homework/quizzes Scientific Notation Practice Problems Homework tonight will be to.
Equivalent expressions are expressions that are the same, even though they may look a little different.
3.1 Using and Expressing Measurements > 1 Copyright © Pearson Education, Inc., or its affiliates. All Rights Reserved. Chapter 3 Scientific Measurement.
Section 7.6 Functions Math in Our World. Learning Objectives  Identify functions.  Write functions in function notation.  Evaluate functions.  Find.
Week 1 Real Numbers and Their Properties (Section 1.6, 1.7, 1.8)
Functions 2. Chapter Overview Perhaps the most useful mathematical idea for modeling the real world is the concept of function, which we study in this.
3.1 Using and Expressing Measurements > 1 Copyright © Pearson Education, Inc., or its affiliates. All Rights Reserved. Chapter 3 Scientific Measurement.
Numbers, Expressions, Simple Programs
Module XIII, Lesson 3 Online Algebra
represents the Empty set, or the set that contains nothing
Week 2 Section 2.4, 2.5, 2.6 and section 2.7 Srabasti dutta.
Presentation transcript:

CSC 160 Computer Programming for Non-Majors Chapter 2: Numbers, Expressions, and Simple Programs Prof. Adam M. Wittenstein

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 Arithmetic

Review: Math vs. Scheme ● In math, the expression * 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: ● In Scheme, we use prefix notation: (+ 3 4).

Example 1: Write each mathematical expression in Scheme notation. ● ● * 5 ● (3 + 4) * 5

Example 1 Solution ● ● * 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 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 Programs

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 * (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 ●

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 = Π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?

Example 2 Solution ● For r = 5, it is: (* ). ● 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: (* )

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 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?