User Defined Datatype
COMPUTER SCIENCE 9618 PAPER 3
User Defined Datatype
Data types are like tools in a toolbox, each designed for a
specific task in programming, dictating how data is stored
and manipulated. Just as you wouldn't use a hammer to
tighten a screw, each data type is suited to handle particular
kinds of data (e.g., numbers, text) efficiently and
appropriately.
What is meant by user defined datatype ?
A data type constructed by a programmer and is not a
primitive datatype ( basic data types )
A data type that references at least one other data type
and is derived from one or more existing data type.
The data types which are referenced can be primitive or
user defined.
To meet’s programmer requirement
Explain why user defined datatype are necessary ?
To create a new data type from existing data types.
To allow data types not available in a programming
language to be constructed that meets programmer’s
requirement
Datatypes
Non Composite Composite
Single data type that does not refer to Data type that refers to other data types
another data type and are constructed from another datatype
Examples : Enumerated, Real, String, Char, Examples : Record, List, Set, Array, Class,
Boolean, Integer Queue, Linked List, Dictionary
Composite data types are like toolkits, combining multiple
tools (e.g., lists, arrays, structs) to hold and manage
collections of related data, whereas non-composite
(primitive) data types are like single tools (e.g., int, float,
char) designed to handle one specific type of data.
Composite types bundle these individual tools together to
solve more complex tasks, while non-composite types focus
on simpler, specific tasks.
Explain what is meant by Composite Data Type ?
A user defined data type that is a collection of data that
can consist of multiple elements of different or the same
data types.
grouped under a single identifier
Explain what is meant by Non Composite Data Type ?
It can be defined without referencing another data type.
It can be primitive type available in a programming
language, or a user-defined datatype.
Enumerated Datatypes
A user defined non composite data type with a list of all
possible values that is ordered
Pseudocode To Declare Enumerated Datatype
TYPE name of Datatype = ( ____ , ____ , ____ , ____ )
Declare an enumerated data type with the identifier Months
which can have all 12 months.
TYPE Months = ( January , February , March, April, ...... )
Declare a variable currentmonth of Datatype Months
DECLARE currentmonth : Months
Assign August in currentmonth variable
currentmonth <--- August
Declare a variable previousmonth of Datatype Month
DECLARE previousmonth : Months
Print the previous month
previousmonth <--- currentmonth - 1
OUTPUT previousmonth
Exam Style Question
Enumerated
Non - Composite
DECLARE session : timeOfDay
session <--- afternoon
TYPE Parts = ( Monitor, CPU, SSD, HDD, LaserPrinter, Keyboard, Mouse )
TYPE Prime = ( 2, 3, 5, 7, 11, 13, 17 )
Pointer Datatypes
A user defined non composite datatype that stores memory
locations only and indicates the type of data stored in the
memory location.
Data
B1603
Sector A
Location
A pointer is like the address of a house. Just as the address
tells you where the house is located, a pointer tells you
where a variable's value is stored in memory. Instead of
holding the house (data) itself, the pointer holds the address
(memory location) where you can find the house (data).
Memory Location Content
8216
8217 “Ahmed”
we are going to
8218 Base String
make a variable
to store the
location 8217. 8219
So that variable
would need a
specific
datatype known
as pointer.
Pseudocode To Declare Pointer Datatype
^ : This symbol represents pointer
@ : This symbol represents the address is required not the data
TYPE name of Datatype = ^Base Datatype
Depends on the
data stored so it
could be String,
Integer, Real,
Boolean, Char
Question : We have Integer variables so create a data type
with the name IntegerPointer
TYPE IntegerPointer = ^INTEGER
Declare a variable in which you store the address of integer
values with the name myintegerpointer
DECLARE myintegerpointer : IntegerPointer
Question : You have a variable Number with value 10 in it
stored on 5216 location. Store the address in
myintegerpointer variable
myintegerpointer <---- @Number
( address of number not the value )
DEREFRENCING
You have the address and you want the value on that address
Change the data which is currently pointed by
myintegerpointer to 100
myintegerpointer ^ <--- 100
Record Datatypes
It’s a user defined datatype composite data type and a group
of multiple data types
If you want to store a complete record in a single variable
name we use the concept of record datatype
Pseudocode To Declare Record Datatype
TYPE name of Datatype
DECLARE Value1 : Datatype
DECLARE Value2 : Datatype
ENDTYPE
Question : We need to store information of a book under a
single identifier. Create a record datatype with the name
Book. The book should hold info about ISBN number (Integer),
Title (String), Genre (String).
TYPE Book
DECLARE ISBN : INTEGER
DECLARE Title : STRING
DECLARE Genre : STRING
ENDTYPE
Question : Declare a new record variable named MyBook
DECLARE MyBook : Book
Question : The value of ISBN is 124657, Title is “Papersdock”,
Genre is “Fiction”
MyBook.ISBN <--- 124657
MyBook.Title <--- “Papersdock”
MyBook.Genre <--- “Fiction”
Question : Why Pointer is Non - Composite
Because the value which is stored in a variable as location is
not an integer or string or any other data type.
Declaring A Range
Question : Declare a variable named Number which contains
0 till 99 numbers
DECLARE Number : 0 .. 9
Specific Datatype In Array
If Array is declared with the data type String that means any
string value can be stored in the array but if you want to
make it specific values only then you are suppose to specify
the values that could be assign to elements.
Question : Declare an Array that can only have “Taha” “Bano”
“Pappan”
DECLARE Names : Array [ 1 : 3 ] OF ( “Taha”, “Bano” , “ Pappan” )
Note : If there are more than one thing to store than declare
array and use Specific Datatype (with Quotation Marks)
Average always means real values. If you have single value then
you would need Enumerated (without Quotation Marks).
Enumerated types are ideal for situations where you have a
limited set of related values.
Set Datatypes
The SET is a user-defined composite data type that allows
you to define a collection of elements of a specific data type,
where each element is unique.
TYPE Name Of Set = SET OF data type
DEFINE Name of datatype ( val1, val2 , val3 ) : Name Of Set
TYPE LetterSet = SET OF CHAR
DEFINE Vowels ('A','E','I','O','U'): LetterSet
Exam Style Question
Write pseudocode statements to declare a set data type
HexDigits that holds the set of valid hexadecimal digit
characters: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F.
TYPE HexSet = SET OF CHAR
DEFINE Hexadecimal ( ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’, ‘A’,
‘B’, ‘C’, ‘D’, ‘E’, ‘F’ ) : HexSet
User Defined Data types
Question 1
Sir Taha Ali Papersdock +92 318 2248934
Question 2
Sir Taha Ali Papersdock +92 318 2248934
Question 3
Sir Taha Ali Papersdock +92 318 2248934
Question 4
Sir Taha Ali Papersdock +92 318 2248934
Question 5
Sir Taha Ali Papersdock +92 318 2248934
Question 6
Sir Taha Ali Papersdock +92 318 2248934
Question 7
Sir Taha Ali Papersdock +92 318 2248934
Sir Taha Ali Papersdock +92 318 2248934
Question 8
Question 9
Sir Taha Ali Papersdock +92 318 2248934
Sir Taha Ali Papersdock +92 318 2248934
Sir Taha Ali Papersdock +92 318 2248934
Question 10
Sir Taha Ali Papersdock +92 318 2248934
Sir Taha Ali Papersdock +92 318 2248934
Question 11
Sir Taha Ali Papersdock +92 318 2248934
Question 12
Sir Taha Ali Papersdock +92 318 2248934
Sir Taha Ali Papersdock +92 318 2248934
Question 13
Sir Taha Ali Papersdock +92 318 2248934
Question 14
Question 15
Sir Taha Ali Papersdock +92 318 2248934
Sir Taha Ali Papersdock +92 318 2248934
Question 16
Sir Taha Ali Papersdock +92 318 2248934
Question 17
Sir Taha Ali Papersdock +92 318 2248934
Sir Taha Ali Papersdock +92 318 2248934
Question 18
Sir Taha Ali Papersdock +92 318 2248934
Question 19
Sir Taha Ali Papersdock +92 318 2248934
Question 20
Question 21
Sir Taha Ali Papersdock +92 318 2248934
Question 22
Sir Taha Ali Papersdock +92 318 2248934
Answer
Answer 1
Answer 2
Answer 3
Sir Taha Ali Papersdock +92 318 2248934
Answer 4
Answer 5
Sir Taha Ali Papersdock +92 318 2248934
Answer 6
Answer 7
Sir Taha Ali Papersdock +92 318 2248934
Answer 8
Answer 9
Sir Taha Ali Papersdock +92 318 2248934
Answer 10
Answer 11
Answer 12
Sir Taha Ali Papersdock +92 318 2248934
Answer 13
Answer 14
Sir Taha Ali Papersdock +92 318 2248934
Answer 15
Answer 16
Answer 17
Sir Taha Ali Papersdock +92 318 2248934
Answer 18
Sir Taha Ali Papersdock +92 318 2248934
Answer 19
Answer 20
Answer 21
Answer 22
Sir Taha Ali Papersdock +92 318 2248934