Programming Techniques :: Records

Slides:



Advertisements
Similar presentations
Introduction to Python
Advertisements

Input, Output, and Processing
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 8: Fun with strings.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Extra Recitations Wednesday 19:40-22:30 FENS L055 (tomorrow!) Friday 13:40-16:30 FENS L063 Friday 17: :30 FENS L045 Friday 19:40-22:30 FENS G032.
OCR Computing GCSE © Hodder Education 2013 Slide 1 OCR GCSE Computing Python programming 3: Built-in functions.
Arrays Chapter 7.
ER Diagrams and Relational Model CS 174a (Winter 2015)
Introduction toData structures and Algorithms
CSc 110, Autumn 2016 Lecture 26: Sets and Dictionaries
Some Assignments  Write a program which prints the following information about at least 5 persons: NAME MAIL-ID EMPLOYEE-CODE PHONE Eg. Umesh
Topics Designing a Program Input, Processing, and Output
Outline lecture Revise arrays Entering into an array
Python: Experiencing IDLE, writing simple programs
Containers and Lists CIS 40 – Introduction to Programming in Python
19.10 Using Cookies A cookie is a piece of information that’s stored by a server in a text file on a client’s computer to maintain information about.
Additional Assembly Programming Concepts
Dr J Frost GCSE Sets Dr J Frost Last modified: 18th.
COMP 103 SORTING Lindsay Groves 2016-T2 Lecture 26
Design a Relational Database Identify Database Purpose
JavaScript: Functions.
Java Review: Reference Types
SQL – Application Persistence Design Patterns
Arrays in C.
Intro to PHP & Variables
8 Pointers.
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
The dirty secrets of objects
Hash tables Hash table: a list of some fixed size, that positions elements according to an algorithm called a hash function … hash function h(element)
The relational operators
Functions, Procedures, and Abstraction
Variables ICS2O.
Lesson 16: Functions with Return Values
Learning Outcomes –Lesson 4
© A+ Computer Science - Arrays and Lists © A+ Computer Science -
4. sequence data type Rocky K. C. Chang 16 September 2018
Coding Concepts (Data Structures)
ARRAYS 1 GCSE COMPUTER SCIENCE.
Topic 3-a Calling Convention 1/10/2019.
File Storage and Indexing
Conditional Execution
CSC 221: Computer Programming I Fall 2009
Introduction In today’s lesson we will look at: why Python?
ARRAYS 2 GCSE COMPUTER SCIENCE.
Overloading functions
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Chapter 9: More About Data, Arrays, and Files
John Barnden Professor of Artificial Intelligence
Chapter 11 Conditional Execution
Chapter 17 JavaScript Arrays
Computers and Scientific Thinking David Reed, Creighton University
Programming Techniques :: Searching Data
Programming Techniques :: File Handling
Programming Techniques :: String Manipulation
Programming Techniques :: Flow Diagrams and Pseudocode
Data Representation :: Binary & Hexadecimal
Running & Testing :: IDEs
Programming Techniques :: Logic & Truth Tables
Dictionary.
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Programming Techniques :: Data Types and Variables
Networks :: Wireless Networks
Programming Techniques :: Defensive Design
Programming Techniques :: Sorting Algorithms
Running & Testing Programs :: Translators
Data Representation :: Compression
Programming Techniques :: Arithmetic & Boolean Operators
Programming Techniques :: Computational Thinking
Presentation transcript:

Programming Techniques :: Records jamie@drfrostmaths.com www.drfrostmaths.com @DrFrostMaths Last modified: 24th June 2019

www.drfrostmaths.com ? Everything is completely free. Why not register? Registering on the DrFrostMaths platform allows you to save all the code and progress in the various Computer Science mini-tasks. It also gives you access to the maths platform allowing you to practise GCSE and A Level questions from Edexcel, OCR and AQA. With Computer Science questions by: Your code on any mini-tasks will be preserved. Note: The Tiffin/DFM Computer Science course uses JavaScript as its core language. Most code examples are therefore in JavaScript. Using these slides: Green question boxes can be clicked while in Presentation mode to reveal. Slides are intentionally designed to double up as revision notes for students, while being optimised for classroom usage. The Mini-Tasks on the DFM platform are purposely ordered to correspond to these slides, giving your flexibility over your lesson structure. ?

What are records/objects? (5,7) (3,4) (7,3) (6,0) (4,−1) Suppose we had a collection of coordinates that we wanted to store. Each coordinate consists of two values: an 𝑥 value and a 𝑦 value. We could store the 𝑥 and 𝑦 values in separate arrays: var xs = [3, 4, 5, 6, 7]; var ys = [4, -1, 7, 0, 3]; Why might this be a bad idea? The 𝒙 and 𝒚 values aren’t tightly coupled. For example, if we were to remove one of the values from say the middle of xs, then all the y values would be out of sync with the x values! If we wanted to pass a coordinate as an argument of a function, it would be nice to treat it as one value, e.g. draw(coord), rather than have to separate x and y, i.e. draw(x, y) ? ?

What are records/objects? (5,7) (3,4) (7,3) (6,0) (4,−1) var coord1 = { x: 3, y: 4 } A record is a special data type which consists of a number of ‘fields’, in this case the x and y. It allows us to group multiple related values into a single entity, just as a single coordinate consists of multiple values. We can access individual fields of a record. This would output 3. console.log(coord1.x)

Comparison of records in different languages Records consist of fields which are intended to store conventional values (strings, integers, arrays, …). Records are fixed in length; we can’t add extra fields once it’s been created. They vary quite substantially depending on whether the language is strongly-typed or weakly-typed: JavaScript PHP In JavaScript, we call these JSONs (JavaScript Object Notation), which we denote with { … } brackets. ‘Records’ in PHP are known as associative arrays. Normal arrays are actually mappings from the index/position to the value: Unlike records, objects can also consist of functionality, e.g. if we wanted a JSON to represent a game character on the screen: $vals = array(“Cat”, “Dog”, “Fish”); is the same as… var t = { name: “Thor”, position: {x: 30, y: 40 }, draw: function(canvas) { canvas.drawPolygon(...); … } $vals = array(0 => “Cat”, 1 => “Dog”, 2 => “Fish”); This means when we use $arr[1], we get the value corresponding to the ‘key’ of 1. But we can have any arbitrary keys: Unlike records, we can also add extra fields after initialisation: t.speed = 140 $coord1 = array(“x” => 3, “y” => 4); print $coord1[‘x’]; // outputs 3

Comparison of records in different languages Python C++/C Known as dictionaries. These are exactly the same as JSONs in JavaScript: we can change the fields after initialisation and there are no type constraints. Known as structures. As C++ is strongly-typed, we need to state the types of the fields within the structure, and the set of fields is fixed. It therefore conforms nicely to our definition of a ‘record’. struct product { int weight; double price; } ; product apple; apple.weight = 150; apple.price = 0.43; thisdict = {   "brand": "Ford",   "model": "Mustang",   "year": 1964 } thisdict.colour = “green”

Arrays of Records var coords = [{ x: 3, y: 4 }, { x: 4, y: -1}, (5,7) As the fields are consistent within each record, this is effectively a table of values: (3,4) (7,3) (6,0) 𝒙 𝒚 3 4 -1 5 7 6 (4,−1) var coords = [{ x: 3, y: 4 }, { x: 4, y: -1}, { x: 5, y: 7 }, { x: 6, y: 0}, { x: 7, y: 3 }]; We commonly combine records with arrays (e.g. an array of JSONs) if they have the same record structure. The above for example shows a collection of coordinates. The DrFrostMaths platform does this all over the place: I use the user database to generate a JSON for the points leaderboard, and subsequent code then generates the table on the page from this. var leaderboard = [{name: “Bob Hope”, points: 14294}, {name: “Kirstin Dunst”, points: 11903 }, … ]

Example Algorithm var people = [{ name: “Alice”, age: 14}, {name: “Bob”, age: 19}, {name: “Charles”, age: 14}, {name: “Denzel”, age: 13}, {name: “Edie”, age: 20}]; ? for(var i=0; i<people.length; i++) { if(people[i].age>=18)console.log(people[i].name); } Write code that prints out the names of all people who are at least 18 years of age.

Review ? ? ? ? Why might we use records? They allow us to store multiple pieces of information about the same thing, e.g. a person’s age, name and address. This can be represented as a single value, making code cleaner. If we had var c = { x: 32, y: -3}, what expression would we use to access its 𝒙 value? c.x What are the items within a record called? Fields Why do JSONs/dictionaries not satisfy the definition of a record? Records are fixed in their number of fields, whereas JSONs/dictionaries we can add extra fields after initialisation. JSONs allows fields which are functions (e.g. ‘draw’), whereas records only allow more conventional values. The fields in records have a data type, where JSONs/dictionaries, being part of weakly-typed languages (JavaScript/Python) don’t enforce type. ? ? ? ?

Coding Mini-Tasks Return to the DrFrostMaths site to complete the various mini-coding tasks on records.