0% found this document useful (0 votes)
14 views10 pages

10.1 - 2 - 3 Data Types & Structures

The document provides an overview of data types and structures in programming, specifically focusing on data types such as INTEGER, REAL, CHAR, STRING, BOOLEAN, DATE, and how to declare variables and constants in pseudocode. It also covers records, arrays (1D and 2D), and methods for searching and sorting arrays, including linear search and bubble sort. Additionally, it includes activities for practice in declaring variables and populating arrays.

Uploaded by

avinashnapaul
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)
14 views10 pages

10.1 - 2 - 3 Data Types & Structures

The document provides an overview of data types and structures in programming, specifically focusing on data types such as INTEGER, REAL, CHAR, STRING, BOOLEAN, DATE, and how to declare variables and constants in pseudocode. It also covers records, arrays (1D and 2D), and methods for searching and sorting arrays, including linear search and bubble sort. Additionally, it includes activities for practice in declaring variables and populating arrays.

Uploaded by

avinashnapaul
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/ 10

9618 Computer Science P2 – Data Types and Structures N.

A 2020

Data Types and Structures


Data Types

Data Type: A classification determining the type of value an item of data can take and how it can be used.

Data Type Description VB.NET


INTEGER Whole number, positive or negative Integer
REAL Positive or negative number with a decimal point Decimal, Single
CHAR Single alphanumeric character Char
STRING Sequence of alphanumerical characters String
BOOLEAN Logical values, True and False Boolean
DATE Value to represent a date Date

Declaring Variables/Identifiers

Variable: A storage location for a value that can change during the execution of a program. It has an identifier.

Identifier: A unique name given to a variable/data item.

Before data can be used in pseudocodes and programs, the type needs to be decided. This is done by declaring the data type
for each item to be used.

Pseudocode structure:

DECLARE <IDENTIFIER> : <data type>

Examples:

DECLARE Name : STRING


DECLARE DateOfBirth : DATE
DECLARE Gender : CHAR
DECLARE Married : BOOLEAN
DECLARE Weight : REAL
DECLARE HeightInCm : INTEGER

Sample Pseudocode:

DECLARE Sum : INTEGER


DECLARE Count : INTEGER
DECLARE Number : INTEGER
DECLARE Average : REAL

Sum ← 0
FOR Count ← 1 TO 10
PRINT “Enter number: “
INPUT Number
Sum ← Sum + Number
NEXT

Average ← Sum/10
PRINT “The average is: “ , Average

Page 1 of 10
9618 Computer Science P2 – Data Types and Structures N.A 2020

Declaring a constant

Pseudocode structure:

CONSTANT <identifier> = <value>

Example:

CONSTANT pi = 3.14
CONSTANT esc = “Exit”

Activity: Write declaration statements in pseudocode for each item.

1. Your name
2. The number of children in a class
3. The time taken to run a race
4. Whether a door is open or closed

Records

Record data type is a composite data type comprising several related items that may be of different data types.

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 full-time student). Each data item may be of different data types.

We can declare a record type known as a user-defined type because we can decide which variables to include as a record.

Pseudocode Structure:

TYPE <TypeName>
DECLARE <identifier> : <data type>
DECLARE <identifier> : <data type>
DECLARE <identifier> : <data type>


ENDTYPE

Pseudocode example

 A Person record type could be defined as:

TYPE TPersonRecord
Name : STRING
DateOfBirth : DATE
Height : REAL
NoOfSiblings : INTEGER
IsFullTimeStud : BOOLEAN
ENDTYPE

Page 2 of 10
9618 Computer Science P2 – Data Types and Structures N.A 2020

 The data type, TPersonRecord, is now available and an identifier may be declared as:

Pseudocode Structure:

DECLARE <variable identifier> : <record type>

Example:

DECLARE Person : TPersonRecord

 We can now assign a value to a field of this Person record using the dot notation

Pseudocode Structure:

<identifier>.<item identifier> ← value

Example:

Person.Name ← “Ajmal”
Person.DateOfBirth ← #14/08/2003#
Person.Height ← 1.75
Person.NoOfSiblings ← 2
Person.IsFullTimeStud ← TRUE

 Output a field of a record

Pseudocode Structure:

OUTPUT <identifier>.<item identifier>

Example:

OUTPUT Person.Name

Activity: Write the declaration of a record type to store the details of a book: Title, Year of publication,
Price, ISBN.

Write the statements required to assign the values “Computer Science”, 2019, $44.95, “9781108733755”
to the fields respectively.

Page 3 of 10
9618 Computer Science P2 – Data Types and Structures N.A 2020

Arrays

 An array is a data structure containing several elements of the same data type.
 The elements can be accessed using the same identifier name.
 The position of each element in array is identified using the array’s index.
 An array index is the row or column number of an individual array element.
 The index of the first element in an array is the lower bound.
 The index of the last element is the upper bound.

Declaring a 1D array

A 1D array can be referred to as a linear list.

Pseudocode structure:

DECLARE <ArrayIdentifier> : ARRAY[<LowerBound> : <UpperBound>] OF <DataType>

Examples:

DECLARE MyList1 : ARRAY [1:3]OF STRING

DECLARE MyList2 : ARRAY [1:5] OF INTEGER

DECLARE MyList3 : ARRAY [0:9] OF CHAR //10 elements in this list

A specific element in array is accessed using an index value. In pseudocode, this gives:

<ArrayIdentifier> [Index] ← value

<ArrayIdentifier> [Index] ← “value” //string data type

Examples:

MyList1[1] ← “Yash” //set the 1st element to “Yash”


MyList2[3] ← 18 //set the 3rd element to number 18
MyList3[0] ← „M‟ //set element zero to letter M

Example of a 1D Array

Index MyList
[0] 27
[1] 19 The lower bound is [0] and the upper bound is [8]. Nine elements are available in the array named
[2] 36 MyList.
[3] 42
[4] 16 MyList[8] refers to the 9th element.
[5] 89
[6] 21
[7] 16
[8] 55

Page 4 of 10
9618 Computer Science P2 – Data Types and Structures N.A 2020

Assigning a group of array elements

To access each array element in turn (populate the array), a loop can be used, e.g.

FOR Index ← 0 TO 8
INPUT MyList[Index]
NEXT Index

Initialising an array

To assign an initial value to all the array elements,

FOR Index ← 0 TO 8
MyList[Index] ← “ “ //for a string data type, Null value
NEXT
………………………………………………………………………………………………………………………………………………….

FOR Index ← 0 TO 8
MyList[Index] ← 0 //for an integer data type
NEXT

Activity

1. Define two arrays, one for your friends’ names and one for their ages.
2. Write statements in pseudocode to populate the two arrays that you’ve created.

Declaring a 2D Array

 A 2D array can be referred to as a table, with rows and columns.


 The number of rows is given first and then the number of columns when declaring a 2D array.

Pseudocode structure:

DECLARE <identifier> : ARRAY [ <LB1> : <UB1> , <LB2> : <UB2> ] OF <DataType>

Rows Columns

Examples:

DECLARE MyScores : ARRAY [0:8 , 0:2] OF INTEGER

DECLARE Board : ARRAY [1:3 , 1:3] OF CHAR

Page 5 of 10
9618 Computer Science P2 – Data Types and Structures N.A 2020

Example of a 2D Array
MyScores Array
Column Index
[r,0] [r,1] [r,2]
[0,c] 27 31 17 ← Lower Bound Row
[1,c] 19 67 48
[2,c] 36 98 29
Row [3,c] 42 22 95
Index [4,c] 16 35 61
[5,c] 89 46 47
[6,c] 21 71 28
[7,c] 16 23 13
[8,c] 55 11 77 ← Upper Bound Row
↑ ↑
Lower Upper
Bound Bound
Column Column

To access an element individually in a 2D array,

<ArrayIdentifier> [row,column]← value

Example:

MyScores[7,0] ← 16

To access each element in a 2D array, a nested loop is used:

 We need a loop to access each row


 Within each row, we need to access each column.

Structured English:

FOR each row


FOR each column
Assign initial value to the element at the current position

Pseudocode Structure (to set each element of the array to zero):

FOR Row ← 0 TO MaxRowIndex


FOR Column ← 0 TO MaxColumnIndex
ThisTable [Row, Column] ← 0
NEXT Column
NEXT Row

Page 6 of 10
9618 Computer Science P2 – Data Types and Structures N.A 2020

To output the contents of a 2D array (all the values in one row on the same 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

Activity

1. Write statements in pseudocode to populate the array MyScores above using FOR…TO…NEXT loop.

Note:

Initialising means declaring the array (its identifier name, subscript range and data type) and also populating each array cell
with a value.

Searching a 1D array (Linear Search)

Linear Search: Checking each element of an array in turn for a required value.

Structured English:

Look at each element in the array


If the element is the required one
Output its position
If the end of the list is reached without finding item
Output “Element not found”

Pseudocode:

DECLARE MyList : ARRAY[1:8] OF INTEGER


DECLARE UpperBound : INTEGER
DECLARE lowerBound : INTEGER
DECLARE Index : INTEGER
DECLARE SearchItem : INTEGER
DECLARE Found : BOOLEAN

UpperBound ← 8
LowerBound ← 1

OUTPUT “Please input item to search for: “


INPUT SearchItem

Page 7 of 10
9618 Computer Science P2 – Data Types and Structures N.A 2020
Found ← FALSE
Index ← LowerBound

REPEAT
IF MyList[Index] = SearchItem // OR IF SearchItem = MyList[Index]
THEN
Found ← TRUE
OUTPUT “Item was FOUND at position “ , Index
ELSE
Index ← Index + 1
ENDIF
UNTIL (Found = TRUE) OR (Index > UpperBound)
IF Found = FALSE
THEN
OUTPUT “Item NOT FOUND”
ENDIF

Sorting elements in a 1D array

 Sorting means arranging data into a specific order, e.g. in ascending or descending order.
 One method of sorting is bubble sort.
 Bubble sort is a sort method where adjacent pairs of values are compared and swapped. (Smaller values slowly rise
to the top).

How to sort values?

i. Compare the first and second values.


 If the first value is larger than the second value, swap them.
ii. Compare the second and third values.
 If the second value is larger than the third value, swap them.
iii. Compare the third and fourth values.
 If the third value is larger than the fourth value, swap them.
iv. Keep on comparing adjacent values, swapping them if necessary, until the last two values in the list have been
processed.

Figure 1 Swapping values working down the array

Page 8 of 10
9618 Computer Science P2 – Data Types and Structures N.A 2020

 Once the first pass is completed, the largest value is in the correct position at the end of the array.
 We need to work through the array again and again.
 After each pass, the next largest value will be in its correct position.

Figure 2 States of the array after each pass

Algorithm for Bubble Sort

Figure 3 Identifier table for bubble sort algorithm

N ← MaxIndex – 1
REPEAT
NoMoreSwaps ← TRUE
FOR j ← 0 T0 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

Page 9 of 10
9618 Computer Science P2 – Data Types and Structures N.A 2020

Activity: Answer questions from Textbook and/or Past Examination Papers

Page 10 of 10

You might also like