Presentation is loading. Please wait.

Presentation is loading. Please wait.

Coding Concepts (Data Structures)

Similar presentations


Presentation on theme: "Coding Concepts (Data Structures)"— Presentation transcript:

1 Coding Concepts (Data Structures)
Topic 2: Programming Coding Concepts (Data Structures)

2 Programming: Data Structures
Whether we’re creating simple or complex programs, we always make use of data structures They are different ways of storing pieces of data The simplest data structure we’ve used (often) is the variable It is a data structure that stores a single value However, there are more Programming: Data Structures

3 Records/Dictionaries
Data Structures There are three complex data structures that we need to examine for this course We have used arrays (both explicitly and implicitly) already From creating arrays of numbers To using string values (which are arrays of characters) The other two need to be looked at in quite a bit of detail As they are very useful 1D Arrays 2D Arrays Records/Dictionaries Programming: Data Structures

4 Programming: Data Structures
1D Arrays Arrays are data structures that can store multiple values They’re like cells in a table, or spaces in a car park We have to say, beforehand, how many spaces we want We can then put whatever we’d like in those spaces Each space has its own ID, called an index It’s how we get to, meaning access, those spaces At any point, we can Read a space (and use its value any way we want) Write to a space (saving a value at that position) Programming: Data Structures

5 Programming: Data Structures
1D Arrays The D stands for Dimension Like ‘3D’ stands for ‘Three Dimensions’ Whenever we create an array, we can actually specify how many dimensions we want 1D arrays create a ‘line’ 2D arrays create a ‘rectangle’ 3D arrays create a ‘cuboid’ We’d use different dimensions for different uses A series of random numbers works best in a 1D array A table of information works best in a 2D array Positions of objects in space works best in a 3D array Programming: Data Structures

6 Programming: Data Structures
1D Arrays Creating an array is as simple as using a pair of square brackets [] Most programming languages recognise this as ‘array’ syntax For Python, we assign these square brackets to a ‘variable’ For C# and Java, we need to create a ‘array’ variable and either Give it an array with the values already in it Create a new array with a specific number of spaces Programming: Data Structures

7 Programming: Data Structures
1D Arrays Here are examples of creating arrays in the different programming languages Note that in C# and Java, we need to state the data-type of the contents of the array as well Python C# Java Programming: Data Structures

8 Programming: Data Structures
1D Arrays In the previous coding examples, we saw two ways of creating an array By specifying the actual values to put in By stating the number of spaces that we want to use The latter option works best when we don’t know what we’re putting in the array Like random values, or user-input values C# and Java have this support built in (we simply put the number inside the square brackets) Python doesn’t support this natively Instead we make a ‘default’ value and multiply it by the size we want Programming: Data Structures

9 1D Arrays One really important thing to remember about arrays
When we’ve made an array By giving it values or spaces The array will only have that many spaces from then on This is known as a static data-structure We CANNOT add or remove spaces after an array has been made! Programming: Data Structures

10 Programming: Data Structures
1D Arrays We can re-create an array if we wanted to That means keeping the array’s name But, change the number of spaces Or give specific values Note that this is just like creating the array from scratch Any values will be lost when doing this Programming: Data Structures

11 Create the following arrays in a program An array of 3 country names
An array of 3 integers (from 1 to 10) for the ‘cute factor’ for the following animals An array of 6 random integers (from 0 to 99) Aardvaark Axolotl Fennec Fox Programming: Data Structures

12 Programming: Data Structures
2D Arrays The next dimension up is the 2D array Rather than having a single line of values, we now have a rectangle The best way of imagining 2D arrays is the first dimension goes down to a specific row the second dimension goes across to a specific column Columns [0] [1] [2] 10 7 5 4 8 12 13 54 40 [0] Rows [1] [2] e.g. [0][2] = 5 Programming: Data Structures

13 Programming: Data Structures
2D Arrays We could use 2D arrays if we’re trying to Represent anything in a table format Save pixel data for bitmaps Store positions in a grid-based 2D game Adding this second dimension to arrays is rather simple Using the second dimension, however, takes a bit more work Programming: Data Structures

14 Programming: Data Structures
2D Arrays Here are some examples C# Python Java Programming: Data Structures

15 Programming: Data Structures
2D Arrays In Python and Java, we can think of 2D arrays as arrays of arrays Thus the use of two pairs of square brackets In fact, we can add another pair of square brackets for more dimensions C#, however, thinks of it as a table It only has one pair of square brackets But use a comma to separate each dimension When we’re specifying the values that go into a 2D array, we add nested brackets for each row Programming: Data Structures

16 Programming: Data Structures
Create the following 2D arrays in a program A String array with 2 rows and 4 columns (as a ‘nickname’ generator) The first row: names (e.g. “Max” or “Aisha”) The second row: adjectives (e.g. “Happy” or “Thoughtful”) An integer array to represent the 10 times- table Programming: Data Structures

17 Programming: Data Structures
Records The final data structure is the record Rather than storing values in indexed boxes, we store values by keys This allows us to create key-value pairs We give a value a key, and store it in the record We can then get the same value using the same key These are also known as dictionaries and associative arrays Programming: Data Structures

18 Programming: Data Structures
Records For example, we could store lots of different pieces of data on a single thing We could create a record for a student We can store their name (using the key “name”) We can store their age (using the key “age”) In Python, we can use different data-types for different values In C# and Java, we’re restricted to using a single data-type We also use different structures for making these In C#, we use the Dictionary class In Java, we use the HashMap class Programming: Data Structures

19 Programming: Data Structures
Records Here’s how we use records in the different programming languages Pay close attention to the {} in Python, [] in C#, and the function calls Java C# Python Java Programming: Data Structures

20 Programming: Data Structures
Create a record for a pet (real or made-up) Make the record itself Give it keys and values for the following The pet’s species The pet’s name The pet’s age Output this information to the console (in a nicely formatted manner) Programming: Data Structures

21 Programming: Data Structures
Is it possible to make a 1D array of records? See if you can work out how to create this yourself! Try an example like an array of pets Each index points to a single pet Each pet as the keys and values from the previous exercise For Python, we simply add another pair of square brackets (like for 2D arrays) for access, and multiple sets of curly-braces for the values: pets = [{“species”: “cat”, “name”: “Toby”, “age”: 5, {“species”: “dog”, “name”: “Sir Woofles”, “age”: 8}}] For C#, we add a pair of square brackets after the Dictionary definition (and then access like a 2D array) and have to initialise each element: Dictionary<string, string>[] pets = new Dictionary<string, string>[2]; pets[0] = new Dictionary<string, string>(); pets[0][“species”] = “cat”; For Java, we add a pair of square brackets after the HashMap definition (and then access like a 2D array) and have to initialise each element: HashMap<String, String>[] pets = new HashMap[2]; pets[0] = new HashMap<>(); pets[0].put(“species”, “cat”); Programming: Data Structures

22


Download ppt "Coding Concepts (Data Structures)"

Similar presentations


Ads by Google