CSC 160 Computer Programming for Non-Majors Lecture #4: Defining Variables Prof. Adam M. Wittenstein

Slides:



Advertisements
Similar presentations
Use the order of operations to evaluate expressions. Objective The student will be able to: Designed by Skip Tyler, Varina High School.
Advertisements

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 Images: Another Form of Symbolic Data Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #5c: Functions with Images Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Lecture #8: Animations I 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 2: Numbers, Expressions, and Simple Programs Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Chapter 6: Structures Prof. Adam M. Wittenstein
Chapter 1 (1.3,1.4) The End of the Beginning. 1.3 Multiply and Divide Multiplication = Repeated addition of the same number 2 times 3 = The.
CSC 160 Computer Programming for Non-Majors Lecture #11: Conditionals II Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Syntax Rules Prof. Adam M. Wittenstein
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
CSC 160 Computer Programming for Non-Majors Draft Chapter: The UFO Example 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
CSC 160 Computer Programming for Non-Majors Section 1.2: Drawing a UFO Prof. Adam M. Wittenstein
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 #5b: Designing Programs 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
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
Programming with Images (continued). Another teachpack – “tiles.ss” Download from the website: “ Save.
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
Chapter 1 Equations, Inequalities, and Mathematical Models 1.3 Formulas and Applications.
Rational and Irrational Numbers Learning Outcomes  I can distinguish between rational and irrational numbers  I can see the significance of recurring.
ME 2304: 3D Geometry & Vector Calculus Dr. Faraz Junejo Double Integrals.
Chapter 1 Review.
1 Essential HTML coding By Fadi Safieddine (Week 2)
Relations & Functions (x,y)y) DR ID 1. Relations & Functions Test administrator: Before administration begins, show students the front of this card and.
PYTHON: PART 2 Catherine and Annie. VARIABLES  That last program was a little simple. You probably want something a little more challenging.  Let’s.
Factors: Divisibility Rules, Exponents, Prime Factorization and Greatest Common Factor (GCF) Mr. Martin.
CSC 160 Computer Programming for Non-Majors Chapter 8: Scheme Language Review Prof. Adam M. Wittenstein
January 13, 2014 Multivariate Expressions and Equations.
Numbers, Expressions, and Simple Programs. Today’s Goals Discipline! Order! Developing programs requires care! Programs are useful but delicate entities.
9 TH GRADE ALGEBRA I Semester I Study Guide: Part II Name: ________________________________ Parent Signature: ___________________________________________.
© 2007 D. J. ForemanJS-1 A Light Introduction for Non-programmers JavaScript for Beginners.
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
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.
Programming in Karel Eric Roberts CS 106A January 6, 2016.
EQ: How can you evaluate negative exponents?
Good Afternoon! Our objective today will be to review all of the material we have covered in Unit 1. WARM-UP: Can you use mental math to solve these problems?
1 Project 2: Using Variables and Expressions. 222 Project 2 Overview For this project you will work with three programs Circle Paint Ideal_Weight What.
ALGEBRA VOCABULARY. Vocabulary: Expression Definition: A math phrase built from constants, variables and operations EXAMPLE: 3X - 2Y.
Learn Alberta - video (Unit 10) Review Expressions/Equations/Variables.
Ch Variables in Algebra Objective: To recognize and evaluate variable expressions and basic formulas.
1.7 Simplifying Expressions Essential Questions: 1)What is the distributive property? 2)How do you simplify expressions?
VARIABLES AND EXPRESSIONS. Definitions: Algebra – Is a language of symbols, including variables. Variable - Is a letter or symbol that represents a quantity.
Go Back > Question 1 8x 3 ? A cube has side length 2x. What is its volume? 2a.
 1) -5+6  2) 7+ (-8)  3)  4) 8(-3)  5) -9(2)  6) 18/2  7 (-9)/3 1 Algebra II.
GEOMETRY Volume of Cylinders, Cones, Spheres 8 th Math Presented by Mr. Laws.
SOLVING ALGEBRAIC EXPRESSIONS
BASIC ELEMENTS OF A COMPUTER PROGRAM
Inequalities and their Graphs
Inequalities and their Graphs
Algebra.
Rulers Rules Image is from:
Inequalities and their Graphs
Creating Pattern Rules from Models
A Reading -Study Strategy
– 3.4 ANSWER 3.5 ANSWER 1.17.
What if Craig doesn’t have a calculator?
Distributing Expressions
Presentation transcript:

CSC 160 Computer Programming for Non-Majors Lecture #4: Defining Variables Prof. Adam M. Wittenstein

Two more language rules Today we will see our first keyword: define. It is used in our next two syntax rules: - Syntax Rule #2: Defining Variables - Syntax Rule #3: Defining Functions

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

Why use variable definitions? In Scheme programming, a variable is just a named constant. (For example, 7, 3.14, and “adam” are constants.) When a number appears repeatedly, it makes enormous sense to give a name and use it by name. Pi is an obvious example. (define PI 3.14) or (define PI ) depending on how much accuracy we need. This can be done with words (and other data types) as well. For example, (define NAME “adam”).

Syntax Rule #2: Defining a Variable Whether you have numbers, words, or any other type of data, variables are defined in the same general way: Whether you have numbers, words, or any other type of data, variables are defined in the same general way: (define VAR-NAME its-value) Examples: Examples: (define PI 3.14) (define NAME “adam”)

Exercise 1: Applying Variables 3x - 6/x 3x - 6/x - Test with X = 2; right answer 3 - Test with X = 2; right answer 3 - Type (define X 2) so DrScheme knows what x is. - Type (define X 2) so DrScheme knows what x is. √(b 2 -4ac) √(b 2 -4ac) Remember to write define statements. Remember to write define statements. Test with A=2, B=-7, C=3; right answer 5 Test with A=2, B=-7, C=3; right answer 5

Defining a variable as a picture Just as we put 3.14 or “adam” into a variable, we can do the same thing with an image. Here is an example with a picture. (define ME …) Fill in … with a picture.

ME is defined as the picture (define ME ) Being able to define variables as pictures is especially helpful because if you want to use the same picture several times (in the same file), you only have to find and insert it once.

Exercise 2: Applying Variables Find a picture on your computer, or a website, and define it as a variable. Find a picture on your computer, or a website, and define it as a variable. Use your first name in caps as the variable’s name. Use your first name in caps as the variable’s name. Find the width and height of the picture. Find the width and height of the picture. Put a solid blue circle on top of the picture. Put a solid blue circle on top of the picture.

How can we square the number 5? We can do (* 5 5). We can do (expt 5 2). We can do (sqr 5). Recall that *, expt, and sqr are all predefined functions.

How can we cube the number 5? We can do (* 5 5 5). We can do (expt 5 3). Note that the cube function is not predefined.

Example: Finding cubes of a number a)0 b)5 c)17 d) e)The result of (d)

One way a)(* 0 0 0) “should be 0” b)(* 5 5 5) “should be 125” c)(* ) “should be a few thousand” d)(* ) “should be about thirty digits long” e)(* yecch….) “should be about ninety digits long”

Better: define variables (define BIG ) (* BIG BIG BIG) (define BIGGER (* BIG BIG BIG)) (* BIGGER BIGGER BIGGER)

In summary… We can set a variable equal to a specific value. As we go through programming this semester, we will see several ways that variables are helpful. One example is not having to insert the same picture several times (in the same file). Next time… A better way to find the cube of a number – Defining functions.