CSC 160 Computer Programming for Non-Majors Chapter 6: Structures Prof. Adam M. Wittenstein

Slides:



Advertisements
Similar presentations
Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Advertisements

Conditional statements and Boolean expressions. The if-statement in Java (1) The if-statement is a conditional statement The statement is executed only.
Functions and Visual Effects Chapter 6 Part 3. Built-in Functions We have been using built-in functions so far 2.
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
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 Lecture #11: Conditionals II Prof. Adam M. Wittenstein
CSC 160 Computer Programming for Non-Majors Syntax Rules Prof. Adam M. Wittenstein
Section 4.4: Designing Conditional Functions. REVIEW: Design Recipe – V. 2  Figure out precisely what you need to do. 1. Understand the problem 2. Function.
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 6.1: Structures (continued). Example 1: in-left-side? ; Purpose: To determine whether a point is in the leftmost 150 pixels of the screen. ; Contract.
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 Section 1.3: Animating the UFOs Flight Prof. Adam M. Wittenstein
CIS101 Introduction to Computing Week 11. Agenda Your questions Copy and Paste Assignment Practice Test JavaScript: Functions and Selection Lesson 06,
CSC 160 Computer Programming for Non-Majors Lecture #10: Conditionals I Prof. Adam M. Wittenstein
Flow control 1: if-statements (Liang 72-80) if(radius < 0) { System.out.println(“cannot get area: radius below zero”); } else { double area = radius *
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
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
CSC 142 J 1 CSC 142 Arrays [Reading: chapter 10].
CS CS1321: Introduction to Programming Georgia Institute of Technology College of Computing Lecture 5 Sept 4th, 2001 Fall Semester.
CC0002NI – Computer Programming Computer Programming Er. Saroj Sharan Regmi Week 7.
Arbitrarily Long Data Structures: Lists and Recursion CMSC Introduction to Computer Programming October 4, 2002.
TeachScheme, ReachJava Adelphi University Tuesday afternoon July 13, 2010.
Scheme: Compound Data Chapter 6 of HTDP Ms. Knudtzon September 19.
CS116 Tutorial 4 Mutation. Review set! begin equal? vs. eq? Memory diagrams.
Evaluate the following: Recall: a logarithm is an exponent. So in each case, we are looking for the exponent of 2 to get a number. In the first example,
CSC 160 Computer Programming for Non-Majors Chapter 8: Scheme Language Review Prof. Adam M. Wittenstein
1 Week 2: Variables and Assignment Statements READING: 1.4 – 1.6 EECS Introduction to Computing for the Physical Sciences.
Chapter 1: Test Your Proficiency Directions: Select a section to work on. Work out each problem on a piece of paper. Click to check your answer. For detailed.
Compound Data. Last Lecture Booleans Only two elements Symbols Not to be confused with variables Strings Notions of equality on values.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Computer Science 101 Circuit Design - Examples. Sum of Products Algorithm Identify each row of the output that has a 1. Identify each row of the output.
Combine Like terms Simplify 3x+2x= 3x+2y+2x+7y= 3x+5x-2= 14x+y-2x+4y-7= Slide 1- 2.
Functional Programming Language 1 Scheme Language: part 2.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Introduction to JavaScript CSc 2320 Fall 2014 Disclaimer: All words, pictures are adopted from “Simple JavaScript”by Kevin Yank and Cameron Adams and also.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Today… Operators, Cont. Operator Precedence Conditional Statement Syntax. Winter 2016CISC101 - Prof. McLeod1.
Functional Programming Language 1 Scheme Language: part 3.
Basic concepts of C++ Presented by Prof. Satyajit De
EasyCode Foundations Vocabulary Terms.
1-1 Logic and Syntax A computer program is a solution to a problem.
Representation, Syntax, Paradigms, Types
If Statement Functions in Excel
Boolean Bingo!.
Programming Right from the Start with Visual Basic .NET 1/e
Selection Statements.
Representation, Syntax, Paradigms, Types
CSC 142 Arrays [Reading: chapter 12].
Representation, Syntax, Paradigms, Types
Vocabulary Memory Cards--Sample
Presentation transcript:

CSC 160 Computer Programming for Non-Majors Chapter 6: Structures Prof. Adam M. Wittenstein

Atomic vs. Compound Data So far our input has been a single number. Usually the input is a piece of data that represents an object with many properties. For example, a person’s personnel record will contain date of birth, salary, name, social security number, etc. Scheme provides many ways to do this. In this chapter, we will see one way to do this – Structures.

Section 6.1: Structures

Posns: A First Structure Why carry a dozen loose oranges when I could put them in a bag? Bag: one object that contains several others posn: one object that contains two others (x coordinate and y coordinate) Posns are actually a predefined data type in Scheme, just like numbers and images.

Built-in functions involving posns make-posn : number number -> posn posn-x : posn -> number posn-y : posn -> number posn? : object -> boolean

Working with posns (define here (make-posn 5 12)) (posn-x here) "should be" 5 (posn-y here) "should be" 12 (posn? here) "should be" true (posn? 7) "should be" false

Common mistakes with posns (posn-x 5) posn-x expects to be given a posn; it returns a number. (posn 5 12) There is no posn function, but there is a make-posn function. (make-posn here (5 12)) make-posn does not define a new variable; it only creates a posn. What you do with that posn (return it, store it in a variable, throw it away…) is up to you. here-x The way to extract the x-coordinate of here is by using the posn-x function: (posn-x here)

Exercise Evaluate the following expressions: (distance-to-0 (make-posn 3 4)) (distance-to-0 (make-posn 3 4)) (distance-to-0 (make-posn (* 2 3) (* 2 4))) (distance-to-0 (make-posn (* 2 3) (* 2 4))) (distance-to-0 (make-posn 12 (- 6 1))) (distance-to-0 (make-posn 12 (- 6 1))) by hand. Show all steps. Assume that sqr performs its computation in a single step. Check the results with DrScheme's stepper.

Exercise Define an appropriately named variable for the posn (0, 0) Define an appropriately named variable for the posn (0, 0) Recall the Scheme syntax rule for this: (define VARIABLE-NAME its-value)

In summary… You have seen how to work with a structure that is defined. Next time… Writing functions with posns.