2.3 Additional Programming Techniques Flashcards
What is an array
An array is a static data structure that can hold a fixed number of data elements. Each data element must be of the same data type i.e. real, integer, string.
How are elements in an array identified
The elements in an array are identified by a number that indicates their position in the array. This number is known as the index. The first element in an array always has an index of 0.
How do we traverse through an array
To traverse (‘move through’) an array a for loop can be used to display each data element in order
For examples
for name in ScoobyGang :
print (name)
Inserting a value
In an array the size is fixed so you cannot insert new values, but you can change the value of elements that already exist. Overwriting the fourth element (Daphne) with a new value (Laura) will change it from Daphne to Laura.
ScoobyGang[3] = “Laura”
Deleting a value
In an array the size is fixed so you cannot delete values, but you can overwrite them as blank. Overwriting the second element (Shaggy) with a blank space makes it appear deleted.
ScoobyGang[1] = “ “
Searching for an array
For large arrays a for loop is needed to search through each element for a specific value. This example checks each name to see if it is equal to Velma.
for name in ScoobyGang :
if name == “Velma” :
print (name)
2D- Array
Often the data we want to process comes in the form of a table. The data in a two dimensional array must still all be of the same data type, but can have multiple rows and columns.
Each value in the array is represented by an index still, but now the index has two values. For example [3] [0] is ‘Daphne’. We measure row first, then column.
Searching for a two-dimensional array
To print a specific data element you can just use the index number like Daphne above. To search for a specific value you will need two for loops, one for the row and another for the values of each row.
The example to the right is looking for the value of ‘Velma’ and when it is round it prints the associated data from the whole row.
for row in ScoobyGang:
for name in row:
if name == “Velma”
print (row)
Records
Unlike arrays, records can store data of different data types.
Each record is made up of information about one person or thing.
Each piece of information in the record is called a field (each row name).
Records should have a key field - this is unique data that identifies each record. For example Student ID is a good key field for a record on students as no two students can have the same Student ID.
SQL
SQL (structured query language) is a language that can be used to search for data in a database.
What is the format of an SQL statement
SELECT field1, field2, field3
FROM table
WHERE criteria