ARRAYS 2 GCSE COMPUTER SCIENCE.

Slides:



Advertisements
Similar presentations
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Advertisements

Chapter 8 Arrays and Strings
Lists in Python.
By the end of this session you should be able to...
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Two-Dimensional Arrays That’s 2-D Arrays Girls & Boys! One-Dimensional Arrays on Steroids!
Count Controlled Loops (Nested) Ain’t no sunshine when she’s gone …
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
For Loop GCSE Computer Science – Python. For Loop The for loop iterates over the items in a sequence, which can be a string or a list (we will discuss.
UCT Department of Computer Science Computer Science 1015F Iteration
Trace Tables In today’s lesson we will look at:
CSC 211 Java I for loops and arrays.
3.1 Fundamentals of algorithms
Pseudocode Key Revision Points.
IGCSE 4 Cambridge Data types and arrays Computer Science Section 2
Python: Experiencing IDLE, writing simple programs
EGR 2261 Unit 10 Two-dimensional Arrays
Two-Dimensional Arrays
Computer Programming BCT 1113
Containers and Lists CIS 40 – Introduction to Programming in Python
CMSC201 Computer Science I for Majors Lecture 12 – Lists (cont)
CS1371 Introduction to Computing for Engineers
2-D Lists Taken from notes by Dr. Neil Moore
JavaScript: Functions.
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Starter Write a program that asks the user if it is raining today.
While Loops BIS1523 – Lecture 12.
Two-Dimensional Arrays
Arrays … The Sequel Applications and Extensions
Repeating Instructions And Advance collection
CSS161: Fundamentals of Computing
Arrays An Array is an ordered collection of variables
Python I/O.
File Handling Programming Guides.
Hidden Markov Models Part 2: Algorithms
Java Programming Arrays
Transforming Data (Python®)
Data Structures – 1D Lists
Coding Concepts (Data Structures)
Fundamentals of Data Structures
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction To Programming Information Technology , 1’st Semester
ARRAYS 1 GCSE COMPUTER SCIENCE.
Multidimensional Arrays
Beginning C for Engineers
Programming We have seen various examples of programming languages
3.1 Iteration Loops For … To … Next 18/01/2019.
Nested Loops & The Step Parameter
How many objects can you create by drawing in the circles?
Building Java Programs
CIS16 Application Development and Programming using Visual Basic.net
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Python Lists.
EECE.2160 ECE Application Programming
EECE.2160 ECE Application Programming
15-110: Principles of Computing
Review of Previous Lesson
EECE.2160 ECE Application Programming
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Just Basic Lessons Mr. Kalmes.
Arrays & Loops.
Arrays & Loops.
2-D Lists Taken from notes by Dr. Neil Moore
Ps Module 7 – Part II 2D Arrays and LISTS 8/29/2019 CSE 1321 Module 7.
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Week 7 - Monday CS 121.
Tuple.
Introduction to Computer Science
Presentation transcript:

ARRAYS 2 GCSE COMPUTER SCIENCE

Objectives By the end of this session you should be able to: Recall what a data structure is Recall what an array is an why arrays are useful Understand and be able to program with two dimensional arrays

RECAP previous In the previous session we looked at one dimensional arrays which look at lot like lists* or single column tables They are called one dimensional because although they might contain many rows they only contain one column *in Python the closest thing we have to an array is a list

1D Array cities Index Content 1 London 2 New York 3 Paris 4 Munich A one dimensional array is like a table with many rows but only one column containing data

2D Data What if we wanted to hold more than one piece of data about each city? cityInfo Index 1 2 3 London UK Tower of London New York USA Empire State Building Paris France Eiffel Tower 4 Munich Germany Allianz Arena

2D Array The solution is to create a two dimensional array We’ve seen already that we can create an array of strings or an array of numbers (e.g. ints) We could also create an arrays of arrays…

ARRAY OF ARRAYS Array of strings – each element of the array dogs is a string Array of arrays– each element of the array dogs is an array of strings

1D vs. 2D One dimensional array Two dimensional array Has height (rows) only Has height (rows) and width (columns) dogs Index Max 1 Sid 2 Nancy 3 Jack dogs Index 1 Max Heeler Sid Cross 2 Nancy 3 Jack Jack Russell Note: A two dimensional array can have more than two columns – it is still a 2 dimensional shape

Rows & Columns If we imagine a 2D array as a table: Each array inside the main array represents a row 1 2 3 4 1 2 Each item inside that array (separated by commas) represents a column This 4 x 2 array has 4 rows and 2 columns

CREATING A 2D ARRAY We seen already how easy it is to create a 2D array by initialising the array when we create it If we know the shape of the array but don’t know what data the it is going to contain yet we can just initialise it with empty strings (or zeroes if its’s going to hold number data)

ACCESSING ELEMENTS Recap that for a 1D array elements are accessed through their index number. Given this array… What does the following pseudocode print out? myHoliday  cities[3] OUTPUT myHoliday

myDog=dogs[2][1] Accessing 2d elements Elements in a two dimensional array are accessed in a similar way However we have to specify a row index and a column index myDog=dogs[2][1] What type of dog is myDog?

Python Syntax myDog=dogs[2][1] row column 1 2 3 1

TASK Can you represent this partially completed noughts and crosses board as a two dimensional array in Python? Call the array board Xs and Os are represented by strings e.g. “X” For blanks insert an empty string e.g. “”

AQA 2D Array Recall that in AQA pseudocode the first index value of an array is always 1 and not zero When creating a 1D array in Python we had to add a “dummy” None value at index[0] so our program matched the AQA pseudocode and the first data item in the array was at index[1] The same applies to 2D arrays but we have to add a “dummy” column as well as a dummy row

Python vs. AQA What are the dimensions of this array? Remember we ignore the None type In AQA pseudocode this would be written as a  [[0,0,0],[0,0,0],[0,0,0],[0,0,0]]

TASK Create this array in Python so it matches AQA pseudocode indexing e.g. cityInfo[1][1]  “London” cityInfo Index 1 2 3 London UK Tower of London New York USA Empire State Building Paris France Eiffel Tower 4 Munich Germany Allianz Arena

LOOPING THROUGH 2D ARRAYS Recall that we can loop through a one dimensional array using the iterator as the array index

LOOPING THROUGH 2D ARRAYS We can access the rows of a 2D array in the same way

LOOPING THROUGH 2D ARRAYS How would you adapt the code on the previous slide so that it just prints out the dog names? Task Load up your cityInfo array and, using a loop, print out a list of just the main attractions

LOOP THE LOOP What about if you want to loop through all the items/elements in an array? We could use the nested loop structure we saw in earlier lessons: The iterator for the outer loop corresponds to the row index The iterator for the inner loop corresponds to the column index Complete the pseudocode exercise on the next slide

TASK Create the following algorithm in Python An array is filled with random numbers generated in groups of 4. Find out exactly how many are divisible by three divisibleByThree  0 randoms  [[1,4,6,7],[7,4,5,9],[8,4,6,3],[5,4,2,6],[1,6,5,3]] FOR i  1 to 5 FOR j  1 to 4 IF randoms[i][j] % 3 == 0 divisibleByThree  divisibleByThree + 1 ENDIF ENDFOR OUTPUT “Divisible by 3 = ” + divisibleByThree

extension Extend your code to: Count the number of values in the array Sum the values in the array Output the count, total and number of values not divisible by three Adapt your code so that the user can look for numbers divisible by 3 or divisible by 2

SUMMARY A data structure is a collection of different data elements, which are stored together in a structured form Two dimensional arrays are useful when we want to group together more than one piece of data about an element A two dimensional array has many rows and two or more columns Elements of a two dimensional array can be accessed by reference to their row and column index number (starting at 0 in Python but 1 in AQA pseudocode) e.g. cityInfo[4][2]

   SELF EVALUATION Can you define/describe the following: Data structure   Array   1D vs 2D array   2D array elements   How AQA array indexes differ from Python   Now check your skills I can create a 2D array in Python    I can access individual elements of a 2D array through their index numbers     I can program a single loop that iterates through a 2D array’s rows I can program a nested loop that iterates through all elements of a 2D array

FURTHER READING/REVISION http://www.teach-ict.com/gcse_computing/ocr/216_programming/handling_data/miniweb/pg10.htm http://www.bbc.co.uk/education/guides/z4tf9j6/revision/1 http://www.tutorialspoint.com/python/python_lists.htm http://learnpythonthehardway.org/book/ex32.html Note: These are the same links as Arrays1 but worth revisiting here