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

Ch 10 Data Types and Structure

Chapter 10 discusses data types and structures in programming, including primitive data types (INTEGER, REAL, CHARACTER, BOOLEAN) and more complex types like STRING, DATE, and CURRENCY. It introduces record data types for grouping different data types logically and explains arrays for organizing data into lists or matrices, including one-dimensional and two-dimensional arrays. The chapter also covers algorithms for linear searching and bubble sorting within arrays, emphasizing the use of loops and conditionals in these processes.

Uploaded by

awsawww1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Ch 10 Data Types and Structure

Chapter 10 discusses data types and structures in programming, including primitive data types (INTEGER, REAL, CHARACTER, BOOLEAN) and more complex types like STRING, DATE, and CURRENCY. It introduces record data types for grouping different data types logically and explains arrays for organizing data into lists or matrices, including one-dimensional and two-dimensional arrays. The chapter also covers algorithms for linear searching and bubble sorting within arrays, emphasizing the use of loops and conditionals in these processes.

Uploaded by

awsawww1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Ch.

10 Data Types and structures

Ch. 10 Data Types and structures


10.1 Data Types and Records
Data Type
A kind of data is called data type.

Primitive data types


Primitive data types are those variables that can be defined simply by commands built into the
programming language.
Data Type Value Description

INTEGER 5, 65, 59999 A signed whole number


REAL 0.3, 0.45, 3.67 A signed number with a
decimal point
CHARACTER A, h, *, space (“ “) A single character
BOOLEAN TRUE/FALSE The logical values TRUE and
FALSE

A value that will always be a whole number should be defined to be of type INTEGER, such as
when counting the iterations of a loop.
Further data types
Data Type Value Description

STRING “beacon”, “34335”, “fds564” A sequence of zero or more


characters.
DATE A date consisting of day, 23/12/2020
month and year, sometimes
including a time in hours,
minutes and seconds
CURRENCY Amount in real number with a $456.34
currency symbol

Note that there is a difference between the number 12 and the string “12”. The string data type is
known as a structured type because it is essentially a sequence of characters. A special case is the
empty string: a value of data type string, but with no characters stored in it. When we write a
date, such as 3 February 2018, we can also write this as a set of three numbers: 3/2/2018.
Sometimes we might wish to calculate with dates, such as taking one date away from another to
find out how many days, months and years are between these dates. To make it easier to do this,
DATE has been designed as a data type.

SAHIR MASROOR KHAN PH 03009543074 1


Ch. 10 Data Types and structures

Record data type


Sometimes variables of different data types are a logical group, such as data about a person
(name, date of birth, height, number of siblings, whether they are a full-time student).
Name is a STRING;
date of birth is a DATE;
height is a REAL;
number of siblings is an INTEGER;
whether they are a full-time student is a BOOLEAN.
We can declare a record type to suit our purposes. The record type is known as a user-defined
type, because the programmer can decide which variables (fields) to include as a record.
In pseudocode syntax (rule) of a record type is declared as:
TYPE <TypeIdentifier>
DECLARE <field identifier> : <data type>
.
.
ENDTYPE
We can now declare a variable of this record type:
DECLARE <variable identifier> : <record type>
And then access an individual field using the dot notation:
<variable identifier>.<field identifier>
Using the example above we can declare a Person record type:
TYPE PersonType
Name : STRING
DateOfBirth : DATE
Height : REAL
NumberOfSiblings : INTEGER
IsFullTimeStudent : BOOLEAN
ENDTYPE

SAHIR MASROOR KHAN PH 03009543074 2


Ch. 10 Data Types and structures

To declare a variable of this type we write:


DECLARE Person : PersonType
And now we can assign a value to a field of this Person record:
Person.Name ← "Fred"
Person.NumberOfSiblings ← 3
Person.IsFullTimeStudent ← TRUE
To output a field of a record:
OUTPUT Person.Name

Arrays
Sometimes we want to organise data values into a list or a table / matrix. In most
programming languages these structures are known as arrays. An array is an ordered set of
data items, usually of the same type, grouped together using a single identifier. Individual
array elements are addressed using an array index for each array dimension.
A list is a one-dimensional (1D) array and a table or matrix is a two-dimensional (2D) array.

Array index: row or column number of an individual array element


Upper bound: the highest number index of an array dimension
Lower bound: the smallest number index of an array dimension
One dimensional array
When we write a list on a piece of paper and number the individual items, we would normally
start the numbering with 1. You can view a 1D array like a numbered list of items. Many
programming languages number array elements from 0 (the lower bound). Depending on the
problem to be solved, it might make sense to ignore
element 0. The upper bound is the largest number used for numbering the elements of an array.
In pseudocode, a 1D array declaration is written as:
DECLARE <arrayIdentifier> : ARRAY[<lowerBound>:<upperBound>] OF <dataType>

SAHIR MASROOR KHAN PH 03009543074 3


Ch. 10 Data Types and structures

Here is a pseudocode example:


DECLARE List1 : ARRAY[1:3] OF STRING // 3 elements in this list
DECLARE List2 : ARRAY[0:5] OF INTEGER // 6 elements in this list
DECLARE List3 : ARRAY[1:100] OF INTEGER // 100 elements in this list
DECLARE List4 : ARRAY[0:25] OF CHAR // 26 elements in this list

Accessing 1D arrays
A specific element in an array is accessed using an index value. In pseudocode, this is written as:
<arrayIdentifier>[x]
The nth element within the array MyList is referred to as MyList[n] .
Here is a pseudocode example:
NList[25] ← 0 // set 25th element to zero
AList[3] ← 'D' // set 3rd element to letter D

We can use a loop to access each array element in turn. If the numbers input to the pseudocode
algorithm
below are 25, 34, 98, 7, 41, 19 and 5 then the algorithm will produce the result in Figure below.
FOR Index ← 0 TO 6
INPUT MyList[Index]
NEXT Index

Linear Search

SAHIR MASROOR KHAN PH 03009543074 4


Ch. 10 Data Types and structures

The problem to be solved:


Take a number as input. Search for this number in an existing 1D array of seven numbers.
Start at the first element of the array and check each element in turn until the search value is
found or the end of the array is reached. This method is called a linear search.
Identifier table:

MaxIndex ← 6
INPUT SearchValue
Found ← FALSE
Index ← –1
REPEAT
Index ← Index + 1
IF MyList[Index] = SearchValue THEN
Found ← TRUE
ENDIF
UNTIL FOUND = TRUE OR Index >= MaxIndex
IF Found = TRUE THEN
OUTPUT "Value found at location: " Index
ELSE
OUTPUT "Value not found"
ENDIF
The complex condition to the REPEAT...UNTIL loop allows us to exit the loop when the search
value is found. Using the variable Found makes the algorithm easier to understand. Found is
initialised (first set) to FALSE before entering the loop and set to TRUE if the value is found.

SAHIR MASROOR KHAN PH 03009543074 5


Ch. 10 Data Types and structures

If the value is not in the array, the loop terminates when Index is greater than or equal to
MaxIndex. That means we have come to the end of the array. Note that using MaxIndex in the
logic statement to terminate the loop makes it much easier to adapt the algorithm when the array
consists of a different number of elements. The algorithm only needs to be changed in the first
line, where MaxIndex is given a value.

Linear search using REPEAT-UNTIL loop


MaxIndex ← 6
INPUT SearchValue
Found ← FALSE
Index ← 0
REPEAT
IF MyList[Index] = SearchValue THEN
Found ← TRUE
ENDIF
Index ← Index + 1
UNTIL FOUND = TRUE OR Index >= MaxIndex
IF Found = TRUE THEN
OUTPUT "Value found at location: " Index
ELSE
OUTPUT "Value not found"
ENDIF

SAHIR MASROOR KHAN PH 03009543074 6


Ch. 10 Data Types and structures

Linear search using WHILE-ENDWHILE loop


MaxIndex ← 6
INPUT SearchValue
Found ← FALSE
Index ← 0
WHILE Found =False AND Index <= MaxIndex
IF MyList[Index] = SearchValue THEN
Found ← TRUE
ENDIF
Index ← Index + 1
ENDWHILE
IF Found = TRUE THEN
OUTPUT "Value found at location: " Index
ELSE
OUTPUT "Value not found"
ENDIF

SAHIR MASROOR KHAN PH 03009543074 7


Ch. 10 Data Types and structures

Sorting
Sorting elements in a 1D array
The simplest way to sort an unordered list of values is the following
method.
1 Compare the first and second values. If the first value is larger than the
second value, swap them.
2 Compare the second and third values. If the second value is larger than
the third value, swap them.
3 Compare the third and fourth values. If the third value is larger than
the fourth value, swap them.
4 Keep on comparing adjacent values, swapping them if necessary, until
the last two values in the list have been processed.
Figure below shows what happens to the values as we work down the
array, following this algorithm.

When we have completed the first pass through the entire array, the largest value is in
the correct position at the end of the array. The other values may or may not be in the
correct order.
We need to work through the array again and again. Aft er each pass through the array
the next largest value will be in its correct position, as shown in Figure below.

SAHIR MASROOR KHAN PH 03009543074 8


Ch. 10 Data Types and structures

In effect we perform a loop within a loop, a nested loop. This method is known as a
bubble sort. The name comes from the fact that smaller values slowly rise to the top,
like bubbles in a liquid.
Identifier Table:

SAHIR MASROOR KHAN PH 03009543074 9


Ch. 10 Data Types and structures

The algorithm in pseudocode is:


n ← MaxIndex – 1
FOR i ← 0 TO MaxIndex – 1
FOR j ← 0 TO n
IF MyList[j] > MyList[j + 1] THEN
Temp ← MyList[j]
MyList[j] ← MyList[j + 1]
MyList[j + 1] ← Temp
ENDIF
NEXT j
n ← n – 1 // this means the next time round the inner loop, we don't
// look at the values already in the correct positions.
NEXT i

VB Code
Dim Num(5) As Integer
Dim j, k, c, Tmp As Integer
Num(1) = 3
Num(2) = 2
Num(3) = 4
Num(4) = 9
Num(5) = 5
For j = 1 To 5
For k = 1 To 4
If Num(k) >= Num(k + 1) Then
Tmp = Num(k)
Num(k) = Num(k + 1)
Num(k + 1) = Tmp
End If
Next
Next
For c = 1 To 5
Console.WriteLine(Num(c))
Next
Console.ReadKey()

SAHIR MASROOR KHAN PH 03009543074 10


Ch. 10 Data Types and structures

The values to be sorted may already be in the correct order before the outer loop has been
through all its iterations. Look at the list of values in Figure above. It is only slightly different
from the first list we sorted.
After the third pass the values are all in the correct order but our algorithm will carry on
with three further passes through the array. This means we are making comparisons
when no further comparisons need to be made.
If we have gone through the whole of the inner loop (one pass) without swapping any
values, we know that the array elements must be in the correct order. We can therefore
replace the outer loop with a conditional loop.
We can use a variable NoMoreSwaps to store whether or not a swap has taken place during
the current pass. We initialise the variable NoMoreSwaps to TRUE. When we swap a pair of
values, we set NoMoreSwaps to FALSE. At the end of the pass through the array we can
check whether a swap has taken place.
The identifier table for this improved algorithm is shown in Table below.

SAHIR MASROOR KHAN PH 03009543074 11


Ch. 10 Data Types and structures

This improved algorithm in pseudocode is:


n ← MaxIndex – 1
REPEAT
NoMoreSwaps ← TRUE
FOR j ← 0 TO n
IF MyList[j] > MyList[j + 1] THEN
Temp ← MyList[j]
MyList[j] ← MyList[j + 1]
MyList[j + 1] ← Temp
NoMoreSwaps ← FALSE
ENDIF
NEXT j
n←n–1
UNTIL NoMoreSwaps = TRUE

Bubble sort: a sort method where adjacent pairs of values are compared and swapped.

SAHIR MASROOR KHAN PH 03009543074 12


Ch. 10 Data Types and structures

Bubble sort Pseudocode 2


FOR J  1 To 4
FOR K  J+1 To 5
IF MyList(J) > MyList(K) THEN
Temp  MyList(J)
MyList(J)  MyList(K)
MyList(K)  Temp
ENDIF
NEXT K
NEXT J
VB CODE
Dim Num(5) As Integer
Dim j, k, c, Tmp As Integer
Num(1) = 3
Num(2) = 7
Num(3) = 4
Num(4) = 9
Num(5) = 5
For j = 1 To 4
For k = j + 1 To 5
If Num(j) > Num(k) Then
Tmp = Num(j)
Num(j) = Num(k)
Num(k) = Tmp
End If
Next
Next
For c = 1 To 5
Console.WriteLine(Num(c))
Next
Console.ReadKey()

SAHIR MASROOR KHAN PH 03009543074 13


Ch. 10 Data Types and structures

Two-dimensional Array
When we write a table of data (a matrix) on a piece of paper and want to refer to individual
elements of the table, the convention is to give the row number first and then the column
number. When declaring a 2D array, the number of rows is given first, then the number of
columns. Again we have lower and upper bounds for each dimension.
In pseudocode, a 2D array declaration is written as:
DECLARE <identifier> : ARRAY[<lBound1>:<uBound1>, <lBound2>:<uBound2>] OF
<dataType>
The array elements in a 2D array can be numbered from 0. Sometimes it is more intuitive to
use rows from row 1 and columns from column 1.
To declare a 2D array to represent a game board of six rows and seven columns, the
pseudocode statement is:
Board : ARRAY[1:6,1:7] OF INTEGER
Accessing 2D arrays
A specific element in a table is accessed using an index pair. In pseudocode this is written as:
<arrayIdentifier>[x,y]
Pseudocode example:
Board[3,4] ← 0 // sets the element in row 3 and column 4 to zero

When we want to access each element of a 1D array, we use a loop to access each element in
turn. When working with a 2D array, we need a loop to access each row. Within each row we
need to access each column. This means we use a loop within a loop (nested loops).
In structured English our algorithm is:
For each row
For each column
Assign the initial value to the element at the current position

SAHIR MASROOR KHAN PH 03009543074 14


Ch. 10 Data Types and structures

Working with two-dimensional arrays and nested loops


Using pseudocode, the algorithm to set each element of array ThisTable to zero is:
FOR Row ← 0 TO MaxRowIndex
FOR Column ← 0 TO MaxColumnIndex
ThisTable[Row, Column] ← 0
NEXT Column
NEXT Row

When we want to output the contents of a 2D array, we again need nested loops. We
want to output all the values in one row of the array on the same line. At the end of the
row, we want to output a new line.
FOR Row ← 0 TO MaxRowIndex
FOR Column ← 0 TO MaxColumnIndex
OUTPUT ThisTable[Row, Column] // stay on same line
NEXT Column
OUTPUT Newline // move to next line for next row
NEXT Row

SAHIR MASROOR KHAN PH 03009543074 15


Ch. 10 Data Types and structures

Text Files
Data need to be stored permanently. One approach is to use a file. For example, any data held in
an array while your program is executing will be lost when the program stops. You can save the
data out to a file and read it back in when your program requires it on subsequent executions.
A text file consists of a sequence of characters formatted into lines. Each line is terminated by
an end-of-line marker. The text file is terminated by an end-of-file marker.

Writing to a text file


Writing to a text file usually means creating a text file.
The following pseudocode statements provide facilities for writing to a file:
OPENFILE <filename> FOR WRITE // open the file for writing
WRITEFILE <filename>, <stringValue> // write a line of text to the file
CLOSEFILE <filename> // close file

Reading from a text file


An existing file can be read by a program. The following pseudocode statements provide
facilities for reading from a file:
OPENFILE <filename> FOR READ // open file for reading
READFILE <filename>, <stringVariable> // read a line of text from the file
CLOSEFILE <filename> // close file

Appending to a text file


Sometimes we may wish to add data to an existing file rather than create a new file. This can
be done in Append mode. It adds the new data to the end of the existing file.
The following pseudocode statements provide facilities for appending to a file:
OPENFILE <filename> FOR APPEND // open file for append
WRITEFILE <filename>, <stringValue> // write a line of text to the file
CLOSEFILE <filename> // close file

SAHIR MASROOR KHAN PH 03009543074 16


Ch. 10 Data Types and structures

The end-of-file (EOF) marker


If we want to read a file from beginning to end, we can use a conditional loop. Text files
contain a special marker at the end of the file that we can test for. Testing for this special
end-of-file marker is a standard function in many programming languages. Every time this
function is called it will test for this marker. The function will return FALSE if the end of the file
is not yet reached and will return TRUE if the end-of-file marker has been reached.
In pseudocode we call this function EOF(). We can use the construct REPEAT...UNTIL EOF().
If it is possible that the file contains no data, it is better to use the construct WHILE NOT EOF().
For example, the following pseudocode statements read a text file and output its contents:
OPENFILE "Test.txt" FOR READ
WHILE NOT EOF("Test.txt") DO
READFILE "Test.txt", TextString
OUTPUT TextString
ENDWHILE
CLOSEFILE "Test.txt"

Abstract Data Types (ADTs)


An Abstract Data Type is a collection of data and a set of associated operations:
• create a new instance of the data structure
• find an element in the data structure
• insert a new element into the data structure
• delete an element from the data structure
• access all elements stored in the data structure in a systematic manner.

SAHIR MASROOR KHAN PH 03009543074 17


Ch. 10 Data Types and structures

Stacks
What are the features of a stack in the real world? To make a stack, we pile items on top of
each other. The item that is accessible is the one on top of the stack. If we try to find an item
in the stack and take it out, we are likely to cause the pile of items to collapse.

Figure above shows how we can represent a stack when we have added four items in this
order: A, B, C, D. Note that the slots are shown numbered from the bottom as these feels
more natural.
The BaseOfStackPointer will always point to the first slot in the stack. The
TopOfStackPointer will point to the last element pushed (added) onto the stack. When an
element is popped (removed) from the stack, the TopOfStackPointer will decrease to point
to the element now at the top of the stack. When the stack is empty, TopOfStackPointer
will have the value –1.
To implement this stack using a 1D array, we write:
DECLARE Stack:ARRAY[0:7] OF CHAR

SAHIR MASROOR KHAN PH 03009543074 18


Ch. 10 Data Types and structures

Queues
What are the features of a queue in the real world? When people form a queue, they join
the queue at the end. People leave the queue from the front of the queue. If it is an orderly
queue, no-one pushes in between and people don’t leave the queue from any other position.
Figure below shows how we can represent a queue when five items have joined the queue in
this order: A, B, C, D, E.

To implement a queue using an array, we can assume that the front of the queue is at
position 0. When the queue is empty, the EndOfQueuePointer will have the value –1. When
one value joins the queue, the EndOfQueuePointer will be incremented before adding
the value to the array element where the pointer is pointing to. When the item at the front
of the queue leaves, we need to move all the other items one slot forward and adjust
EndOfQueuePointer
This method involves a lot of moving of data. A more efficient way to make use of the slots
is the concept of a ‘circular’ queue. Pointers show where the front and end of the queue are.
Eventually the queue will ‘wrap around’ to the beginning. Figure below shows a circular queue
after 11 items have joined and five items have left the queue.

SAHIR MASROOR KHAN PH 03009543074 19


Ch. 10 Data Types and structures

Linked lists
We have already used an array as a linear list. In a linear list, the list items are stored
in consecutive locations. This is not always appropriate. Another method is to store an
individual list item in whatever location is available and link the individual item into an
ordered sequence using pointers.
An element of a list is called a node. A node can consist of several data items and a pointer,
which is a variable that stores the address of the node it points to.
A pointer that does not point at anything is called a null pointer. It is usually represented by
∅. A variable that stores the address of the first element is called a start pointer.

In Figure below, the data value in the node box represents the key field of that node. There are
likely to be many data items associated with each node. The arrows represent the pointers.
It does not show at which address a node is stored, so the diagram does not give the value of
the pointer, only where it conceptually links to.

Figure below shows how a new node, A, is inserted at the beginning of the list. The content of
StartPointer is copied into the new node’s pointer field and StartPointer is set to point
to the new node, A.

In Figure below, a new node, P, is inserted at the end of the list. The pointer field of node L

SAHIR MASROOR KHAN PH 03009543074 20


Ch. 10 Data Types and structures

points to the new node, P. The pointer field of the new node, P, contains the null pointer.

To delete the first node in the list (see Figure below), we copy the pointer field of the node to be deleted
into StartPointer.

To delete the last node in the list (see Figure below), we set the pointer field for the previous node to
the null pointer.

Sometimes the nodes are linked together in order of key field value to produce an ordered linked
list. This means a new node may need to be inserted or deleted from between two existing nodes.
To insert a new node, C, between existing nodes, B and D (see Figure below) we copy the
pointer field of node B into the pointer field of the new node, C. We change the pointer field of
node B to point to the new node, C.

SAHIR MASROOR KHAN PH 03009543074 21


Ch. 10 Data Types and structures

To delete a node, D, within the list (see Figure below), we copy the pointer field of the node to
be deleted, D, into the pointer field of node B.

Remember that, in real applications, the data would consist of much more than a key field
and one data item. This is why linked lists are preferable to linear lists. When list elements
need reordering, only pointers need changing in a linked list. In a linear list, all data items
would need to be moved.
Using linked lists saves time, however we need more storage space for the pointer fields.
To implement a linked list using arrays, we can use a 1D array to store the data and a 1D array to
store the pointer. Reading the array values across at the same index, one row represents a node.
A value is added to the next free element of the Data array and pointers are adjusted to
incorporate the node in the correct position within the linked list.
Figure below shows how two arrays can be used to implement the linked list.

Figure below shows how a new node is added to the beginning of a linked list implemented
using arrays. Note that the value “A” is added at index 3 but the start pointer is adjusted to
make this the new first element of the list.

SAHIR MASROOR KHAN PH 03009543074 22


Ch. 10 Data Types and structures

Figure below shows how a new node is added to the end of a linked list implemented using
arrays. Note that the value “P” is added at index 3. The node that previously contained the
null pointer (at index 0) has its pointer adjusted to point to the new node.

When deleting a node, only pointers need to be adjusted. The old data can remain in the
array, but it will no longer be accessible as no pointer will point to it.
Figure below shows how the start pointer is adjusted to effectively delete the first element of
the linked list. Note that the start pointer now contains the pointer value of the deleted node.

SAHIR MASROOR KHAN PH 03009543074 23


Ch. 10 Data Types and structures

Figure below shows how the pointer value of the penultimate node of the linked list is
changed to the null pointer.

When adding a node that needs to be inserted into the list, the data is added to any free
element of the Data array. The pointer of the new node is set to point to the index of the node
that comes after the insertion point. Note that this is the value of the pointer of the node
preceding the insertion point. The pointer of the node preceding the insertion point is set to
point to the new node.

SAHIR MASROOR KHAN PH 03009543074 24


Ch. 10 Data Types and structures

Again, when deleting a node, only pointers need to be adjusted. Figure below shows how the
pointer of the node to be deleted is copied into the pointer of the preceding node.

Unused nodes need to be easy to find. A suitable technique is to link the unused nodes to
form another linked list: the free list. Figure below shows our linked list and its free list.

When an array of nodes is first initialised to work as a linked list, the linked list will be empty.
So the start pointer will be the null pointer. All nodes need to be linked to form the free
list. Figure below shows an example of an implementation of a linked list before any data is
inserted into it.

SAHIR MASROOR KHAN PH 03009543074 25


Ch. 10 Data Types and structures

Assume “L”, “B” and “D” were added to the linked list and to be kept in alphabetical order.
Figure below shows how the values are stored in the Data array and the pointers of the linked
list and free list adjusted.

If the node containing “B” is to be deleted, the array element of that node needs to be linked
back into the free list. Figure below shows how this is done by adding the node to the front of
the free list.

SAHIR MASROOR KHAN PH 03009543074 26

You might also like