Coding Concepts (Data Structures)

Slides:



Advertisements
Similar presentations
Lists Introduction to Computing Science and Programming I.
Advertisements

Java Unit 9: Arrays Declaring and Processing Arrays.
Lists in Python.
Python November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
Unit 2 Technology Systems
CSC 211 Java I for loops and arrays.
User-Written Functions
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Spring 2017
Matrix. Matrix Matrix Matrix (plural matrices) . a collection of numbers Matrix (plural matrices)  a collection of numbers arranged in a rectangle.
Python Variable Types.
Python – May 18 Quiz Relatives of the list: Tuple Dictionary Set
CMSC201 Computer Science I for Majors Lecture 22 – Binary (and More)
Loops BIS1523 – Lecture 10.
Working with Collections of Data
© 2016 Pearson Education, Ltd. All rights reserved.
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
Debugging and Random Numbers
Other Kinds of Arrays Chapter 11
Other Kinds of Arrays Chapter 11
A simple way to organize data
Cell Arrays Definition Creating Cell Arrays Referencing Cell Arrays
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
JavaScript: Functions.
Arrays in C.
Intro to PHP & Variables
Engineering Innovation Center
While Loops BIS1523 – Lecture 12.
Two-Dimensional Arrays
Week 6 Discussion Word Cloud.
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
IPC144 Introduction to Programming Using C Week 1 – Lesson 2
Python I/O.
Fundamentals of Programming
Organizing Memory in Java
Packages and Interfaces
Week 9 – Lesson 1 Arrays – Character Strings
CS190/295 Programming in Python for Life Sciences: Lecture 6
Coding Concepts (Sub- Programs)
Theory of Computation Turing Machines.
PC01 Term 3 Project Snake. PC01 Term 3 Project Snake.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Data Structures – 1D Lists
Topic 1: Problem Solving
Fundamentals of Data Structures
ARRAYS 1 GCSE COMPUTER SCIENCE.
We’re moving on to more recap from other programming languages
Coding Concepts (Basics)
Chapter 8 Data Structures: Cell Arrays and Structures
Coding Concepts (Standards and Testing)
Coding Concepts (Data- Types)
Data Types and Data Structures
Fundamentals of Data Structures
funCTIONs and Data Import/Export
CMSC201 Computer Science I for Majors Lecture 18 – String Formatting
Topics Introduction to Value-returning Functions: Generating Random Numbers Writing Your Own Value-Returning Functions The math Module Storing Functions.
ARRAYS 2 GCSE COMPUTER SCIENCE.
CIS16 Application Development and Programming using Visual Basic.net
Loops and Arrays in JavaScript
For loops Taken from notes by Dr. Neil Moore
Java SE 7 One and Multi Dimensional Arrays Module 6
Arrays Arrays A few types Structures of related data items
Review of Previous Lesson
Chapter 8 Data Structures: Cell Arrays and Structures, Sorting
CISC101 Reminders Assignment 3 due today.
Enclosing delimiters Python uses three style of special enclosing delimiters. These are what the Python documentation calls them: {} braces # Sometimes.
Dictionary.
Introduction to Computer Science
Presentation transcript:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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