Presentation is loading. Please wait.

Presentation is loading. Please wait.

Programming Techniques :: Records

Similar presentations


Presentation on theme: "Programming Techniques :: Records"— Presentation transcript:

1 Programming Techniques :: Records
Last modified: 24th June 2019

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

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

4 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)

5 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”, => “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

6 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”

7 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: }, … ]

8 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.

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

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


Download ppt "Programming Techniques :: Records"

Similar presentations


Ads by Google