0% found this document useful (0 votes)
12 views

Data Structures 2

Uploaded by

jude
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Data Structures 2

Uploaded by

jude
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

AS Computer Science

Unit 1 & 2: Data Structures

Term Definition One-dimensional array example Insertion


[0] [1] [2] [3] [4] [5] [6] [7] Add data to an element at a given index:
Data A data structure is a specific way
structures of organising data within memory 37 11 42 6 26 56 4 76
1 myArray [4,1] = “Sian”
so it can be processed efficiently.
2 myArray [4,2] = “Jones”
• 8 elements
Arrays An array is a data structure that
• The index always starts at position [0]. This would store “Sian Jones” at index 4 of the
contains groups of elements.
Normally these elements are all array.
• Each element can be accessed using its
of the same data type. index.
Deletion
Records Records are composed of fields, • The element at index [4] is 26.
Deleting data from an element at a given index:
each of which contains one item
of information about the same Two-dimensional array example: 1 myArray[1,1] = “”
subject, e.g. one person. A set of Index i j 2 myArray [1,2] = “”
records constitutes a file.
0 Fred Smith
This would leave the memory at index 6 blank.
1 John Davies
Data types 2 Sue Evans
3 Pamela Wilson Using an array to search data
Data types include:
• integer: whole numbers, e.g. 10, 23 Arrays can be used to search for a data item
• 4 elements from a list. The data from the file can be read
• real: numbers with decimal or fractional parts, into an array and then the data can be searched
• The index always starts at 0.
e.g. 3.142, 99.9 using the index until the required item is found.
• Each element can be accessed using its
• character: individual characters, e.g. M, F Declare linearSearch(dataList, searchItem)
index.
• string: series of characters, e.g. Fred, Smith position = 0
• The element at index [1,2] is Davies.
found = false
• Boolean: e.g. TRUE or FALSE
• The element at index [3,1] is Pamela.
while position < len(dataList) and found = false
Arrays normally include elements of the same data if dataList[position] = searchItem then
type. Traversing found = true
else
Records include a mixture of data types. Print the contents of the array above: position = position + 1
end if
1 for i = 0 to 3 end while

Arrays 2 for j = 1 to 2 testList = [1,3,21,45,57,17,34,65]


linearSearch(testList, 45)
3 output myArray [i, j] linearSearch(testList, 20)
Arrays are data structures comprising a collection 4 next j
of elements (values or variables). Each element is 5 next i
referenced by an index.
AS Computer Science
Unit 1 & 2: Data Structures

Records Writing data records to a file Fixed and variable records

A record is a group of related data held Data can be written to a file via the computer There are two main types of records; fixed length and
within the same structure. A record is user interface. variable length records.
a grouping of fields within a table that
reference one particular object.
Fixed length records
An example of the structure of a pupil record
is:
Each record in a file will be the same length with the
size of each field remaining consistent.
Data
Fieldname Length Example
type The Pupil records will all have pupilIDs that are 5
PupilID String 5 P0001 characters in length with the same format P9999.
The firstname field has been set at 20 characters. If a
Firstname String 20 Fred
pupil’s name is shorter that 20 characters a series of
Surname String 20 Smith blanks (padding) will be stored in that field, e.g. if Fred
is stored in a record, there will be four characters and
House No Integer 10 16 blanks.
Postcode String 8 CF62 6YX

Gender Character 1 M Variable length records


pupilID= pupilID.txt
Attendance Real 94.3
Each record in a file can be of a different length which
firstname = firstname.txt will be dependent on the length of the data to be
A record comprises multiple data types. stored in that record. Each field is separated from other
Each record will be of the same structure surname = surname.txt fields by special characters known as delimiters.
(same fields).
form = form.txt
Each record will have a key field which will
be a unique identifier for that record. In DoB = DoB.txt
Advantages and disadvantages of fixed and
general, searches will be carried out on the variable length records
key field. file.open (”PupilDetails.txt”)
Variable length records save storage space but are
file.write (pupilID + firstname + more difficult to manipulate through code.
Reading data records from a file surname + form+ DoB)
Fixed length records can take up considerable space
file.close() which is used to store padding. However, as the length
When data records are read from a file, they of each record is known, they are easier to manipulate
can be read into a two-dimensional array to messagebox(”Confirmation”,”Pupil when programming.
allow them to be manipulated (searched or details successfully saved”)
sorted).

You might also like