Download presentation
Presentation is loading. Please wait.
1
Chapter 1: Data Structures 1A, B
Component 1 Chapter 1: Data Structures 1A, B
2
Learning Intentions 1A - Describe, interpret and manipulate data structures including arrays (up to two dimensions at As and up to three dimensions at A2), records, stacks, queues, trees, linked lists and hash tables. 1B - Describe the manipulation of records and arrays.
3
Introduction A Data structure is a collection of related data items held in a computer’s memory. Programmers need to ensure that they choose the right data structure to store their data to ensure the program runs, and runs efficiently. The different data structures you need to know about are: Arrays: One dimensional Arrays Two dimensional Arrays Three dimensional Arrays Records Stacks Queues Binary Trees Linked Lists Hash Tables
4
Why are Data Structures needed?
Data structures provide a convenient and efficient method of storing and organising data relating to a real world problem. It is often more efficient to deal with various elements as one item – for example the personal details of an employee, or the order details for a single purchase.
5
Types of Data Structure: Static (Fixed) and Dynamic (Variable)
Data structures can usually be categorised as either Static or Dynamic. Static structures are fixed length which means their length is set up when the program – they are usually easier to program because you know how long they are when you design your program. Because static data structures are a fixed size the programmer knows exactly how much space they will require on a disk – this can help memory management. Dynamic structures are variable length which means that their length can change whilst the program runs. These are usually more difficult to program because the programmer does not know how big the structure needs to be. Static (Fixed) Dynamic (Variable) Memory is allocated to the data structure dynamically i.e. as the program executes. Memory is allocated at compile time. Fixed size. Disadvantage: Because the memory allocation is dynamic, it is possible for the structure to 'overflow' should it exceed its allowed limit. It can also 'underflow' should it become empty. Advantage: The memory allocation is fixed and so there will be no problem with adding and removing data items. Advantage: Makes the most efficient use of memory as the data structure only uses as much memory as it needs Disadvantage: Can be very inefficient as the memory for the data structure has been set aside regardless of whether it is needed or not whilst the program is executing. Disadvantage: Harder to program as the software needs to keep track of its size and data item locations at all times Advantage: Easier to program as there is no need to check on data structure size at any point.
6
Static (Fixed) vs Dynamic (Variable) Length structures
Disk Size Uses the same amount of space on the disk because it has the same number of elements or fields. Truncation: If a value is too large it will be truncated to fit. Changes the number of bytes required on the disk because it has different numbers of elements of fields. Avoid truncation because can change number of bits. Programming Easier to program because the length and disk space required is known. More difficult to program because space required is unknown. Processing Quicker to process because the start and end point are always known. More difficult to process because start and end points have to be calculated when the program is run (at run time). Disk Space Can have blank space in the structure if all elements or fields are not filled. Saves space because length can change.
7
Arrays An Array is a data structure that can store multiple values of the same data type. For example an Array may store the unique words in a sentence, the ages of respondents to a survey, or the product codes of a shopping list: Names = [‘Connor’, ‘Glenn’, ‘Shriya’, Dylan’] Arrays are always grouped together under the same identifier. An identifier is the textual name (not starting with a number) used to identify variables, constants, and functions. Arrays are usually static data structures, which means the size of an Array needs to be known before the program is ran. Arrays are indexed starting from 0 – this means that each element in the Array is given it’s own, unique, index number: Names: 1 2 3 Connor Glenn Shriya Dylan
8
Manipulating Arrays Manipulating data typically means doing one or more of the following things: Outputting (printing) Outputting specific elements Inserting (at the start or in the middle) Appending (Adding to the end) Searching Sorting To print an array in Python you use the command word ‘print’ and place the identifier inbetween brackets: To print a specific element you add [index] to the end of the identifier: What would this code print? We’ll cover these later.
9
Manipulating Arrays Inserting into an Array uses the following code:
You first call the Array, then type .insert(i,a) a is the new data you want to insert i is the index Appending an Array (adding something to the end) is done by using the following code: You first call the Array, then types .append(value)
10
Programming Tasks In Python Arrays are often called ‘Lists’ using es.html to help complete the following tasks. When you write your code you must ensure you use a #comment to explain what each part of the code does. Create an Array with 10 different names, Print the entire Array Print only the even numbered elements (including 0) Create an Array with 5 integers of different values Print it Add a 6th integer to the end of the Array Add a 7th integer into 2nd position in the Array Sort the Array from largest to smallest Remove the last value Write a program that creates and prints an Array with the following values for a break in Snooker: ‘Red’, ‘Black’, ‘Red’, ‘Pink’, ‘Red’, ‘Black’, ‘Red’, ‘Blue’, ‘Miss’ Count how many Reds were potted Replace the Pink with a Brown
11
Two-dimensional Arrays
The Arrays we have looked at so far have been One Dimensional – this meants that an element inside of the Array has only 1 index: The element ‘Dylan’ has index 3: A Two-Dimensional Array stores data with 2 indexes (think of this like a spreadsheet, each element has a column and a row index): To create a two-dimensional Array in Python you create an Array and place another Array inside of each element: To manipulate 2D Array you simply refer to a longer index. For example print(aNames[1][2]) would print the name Edward. Whilst, Names.[3][0] = ‘Alistair’ would change Jack to Alistair 1 2 3 Connor Glenn Shriya Dylan Put the 2nd and 3rd row inside these Arrays
12
Programming Tasks Write a program that creates the following 2D array: Print Phil’s SAT2 test result Update John’s SAT3 test result to a B- Write a program that: creates the following 2D array: Change the program so that the user can enter an ‘X’ wherever they want Hint: you will need to ask the user for both parts of the element index. X O SAT1 SAT2 SAT3 John A+ A Eric A- B+ D Phil F D- Useful link
13
3D Arrays 3D arrays add another level of complexity – you could think of 3D arrays as stacks of spreadsheets where the headings of the columns and rows don’t change, but the values do – for example a Sales by month stack! 3D Arrays require a third index, which means when they are created it is An Array, within an Array, within an Array! ComputingClasses = [[[‘Sally’, ‘7HRA’],[‘Bert’, ‘7APO’]],[[‘Martin’, ‘7HRA’],[‘Susan’, ‘7ZEU’]]] Name Tutor Group Sally 7HRA Martin Bert 7APO Susan 7ZEU
14
3D Arrays – Key Exam Point
3D Arrays are more difficult to program and more difficult for a computer to process
15
Programming Tasks Create a 3D Array that creates the following Computing classes: Print the details for each class – to do this you will need to use a nested loop. Name Tutor Group Computing Class Sally 7HRA 7M Martin 7S Bert 7APO Susan 7ZEU John Eric 7HME
16
Records A record is a Data structure that contains multiple elements that may be different data types. For example a Student’s registration details: Firstname String Lastname House Number Integer Street Postcode Phone Number John Smith 47 Windmill Cresent WV3 8HU
17
Searching an Array Because Arrays are static they can’t have extra elements added to them – this means that if new data needs to be added the program needs to find an empty space in the Array! To search an Array a loop must be used (to ensure all elements are searched) Try running the following code – what does it do? aNames = ['Connor','Glenn','','Dylan'] for i in aNames: if i == '': print(aNames.index(i),'Empty') else: print(aNames.index(i),'Not empty') Programming tasks: Create an Array with 10 elements where index 3, 7, and 9 are empty. Insert an element into index 3. Insert a different element into index 7.
18
Arrays and Records – Grade D Theory
What is a Data Structure? Why are they required in computing? Describe the difference between fixed and variable length data structures. Consider: The size on disk Ease of programming, explain why Ease of processing, explain why Possible Truncation Using an example; describe an Array and what it is used for Using an example; describe a Record and what it is used for Explain the difference between Records and Arrays Write an Algorithm in Pseudocode that will create a two-dimensional array to store 5 students and their test scores Write an Algorithm in Pseudocode that will input 10 integers into an Array, search the Array of 10 integers and find the largest value, and output the largest value Draw a diagram of a 2 Dimensional Array for a Class with 5 students and their scores in 2 SAT Tests Explain a drawback of 3 Dimensional Arrays
19
Programming Challenge: Tic-Tac-Toe
Create a program that lets two users play a game of Tic-Tac-Toe. The game should: Allow each player to select a position Check if 3 Xs or Os are in a row Declare the winner! Ask the players if they want to play again
20
Homework Pre-Reading:
1c – Hash Tables, Records and Stacks 1d – Linked lists and Binary Trees Exam Questions (Lesson 1 Exam Questions) – These need to be self- assessed.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.