1
Data Representation
Kiran Rana 08/20/2020
2 Scope of Topic
1. Primitive and complex data
2. Data and computer memory
3. Scaling
4. Choosing the right data type
Kiran Rana 08/20/2020
3 Variable
A variable is a container for a specific kind of data.
The value of variable can be changed through out the program.
Pseudocode to create and set a variable
data name as string
name = "John"
Kiran Rana 08/20/2020
4 Data Types
It determines the type of data that can be stored in variable.
1. Whole Number
Integer
data age as whole number
age = 21
2. Real Number
Float or Decimal values
data salary as real number
Kiran Rana 08/20/2020
5 Data Types (cont..)
3. String
Collection of characters
data name as string
name=“John”
4. Character
A datatype which contains a single Unicode character
data gender as character
Gender=‘M’
5. Boolean
A data type which contains either true or false. It can hold nothing else.
data click as Boolean
Kiran Rana 08/20/2020
6 Types of Data Types
Two kinds of data type in most modern programming languages.
1. Primitive data types
Primitive data types are the building blocks that are used to build all other data types.
e.g. whole numbers, real numbers , characters , Boolean.
Primitive data types are also known as value data types.
2. Complex data types
Complex data types are those made up of combinations of primitive data types.
e.g. Strings , Object , Array , Class , Scanner
Complex data types are also known as reference data types.
reference variables store addresses.
Kiran Rana 08/20/2020
7 Type
Primitive
Represents
Data TypesRange Default Values
bool Boolean value True or False False
char 16-bit Unicode character U +0000 to U +ffff '\0'
byte 8-bit unsigned integer 0 to 255 0
Short 16-bit unsigned integer -32,768 to 32,767 0
int 32-bit signed integer type -2,147,483,648 to 2,147,483,647 0
long 64-bit signed integer type -9,223,372,036,854,775,808 to 0L
9,223,372,036,854,775,807
float 32-bit single-precision floating -3.4 x 1038 to + 3.4 x 1038 0.0F
point type
double 64-bit double-precision floating (+/-)5.0 x 10-324 to (+/-)1.7 x 10308 0.0D
point type
decimal Kiran Rana
128-bit precise decimal values (-7.9 x 1028 to 7.9 x 1028) / 100to 0.0M 08/20/2020
with 28-29 significant digits 28
8 Default Variable Values
When we create a number in our pseudocode, the first thing we do in our desk-check is
set its value to .
This is its default value. It is a convention of our pseudocode
It is not something necessary that all programming languages set default value to 0.
Default Value for Boolean is false
Default Value for character is null or NA
Complex data types have no default value, they start off as null.
Kiran Rana 08/20/2020
9 Null
It is nothing.
You cannot use null in calculations
You cannot output null.
If you attempt to perform any kind of operation on a null, a computer program
will usually crash.
Kiran Rana 08/20/2020
10 Pseudocode Example
1 data num1 as whole number
2 data num2 as whole number
3 data sum as whole number
4 data usertext as string
5 num1 = 10
6 num2 = 20
7 sum = num1 + num2
8 usertext = "The answer is:"
9 output usertext
10 output sum
Kiran Rana 08/20/2020
11 Desk-Check with Null Values
Line Num1 Num2 Sum userText
1 0
2 0 0
3 0 0 0
4 0 0 0 Null
5 10 0 0 Null
6 10 20 0 Null
7 10 20 30 Null
8 10 20 30 “The answer is”
9 10 20 30 “The answer is”
10 10 20 30 “The answer is”
Kiran Rana 08/20/2020
12 Computer Memory
Every piece of data that is used in an algorithm must be stored somewhere.
It gets stored in the computer’s memory.
There are real physical constraints that impact on how we design algorithms.
We have finite amounts of computer memory.
We have finite amounts of CPU cycles.
Kiran Rana 08/20/2020
13 Sizes of Data Types
The size of every data type is different for each programming language.
Kiran Rana 08/20/2020
14 Scaling
Scaling is the degree to which an algorithm can work for larger sets of data.
Many algorithms suffer from 'scaling issues‘.
What works for ten items of data, may not work as well for a thousand
The decisions we make about the data we store will have an impact on how useful the
algorithm is for larger tasks.
Choosing the wrong data at the start will impact on scaling.
Kiran Rana 08/20/2020
15 Scaling Example
Consider a program which checks to see whether a key on a keyboard was pressed in
the past sixty seconds.
We could store that as a Boolean.
We could store that as an whole number.
We could store it as a string.
Kiran Rana 08/20/2020
16 Memory leak
Memory leak occurs when a computer program incorrectly manages memory
allocations
A memory leak can diminish the performance of the computer by reducing the
amount of available memory
Memory leaks are a common error in programming, especially when using
languages that have no built in automatic garbage collection, such as C and C++.
Memory leaks are caused by memory being used up but never freed when the
computer program is finished.
Kiran Rana 08/20/2020
17 Memory Leak
Kiran Rana 08/20/2020
18 Choosing the Right Data Type
Choosing the right data type is important, because it makes everything else easier.
You need to consider:
What kind of information you need to store
What kind of manipulations you are going to do to the data.
What kind of format will be used for output.
How often you might need to change the representation.
Kiran Rana 08/20/2020
19 Choosing the Right Data Type (cont..)
What is the best data type for...
A phone number?
An address?
The gender of a student?
The age of a person?
Kiran Rana 08/20/2020
20 Choosing the Right Data Type (cont..)
It is often dependent on context.
A phone number is usually best stored as a string.
An address is also best stored as a string.
A Boolean or a character or String might best represent gender.
The age can be a whole or real number.
Kiran Rana 08/20/2020
21 Phone Number
Why is a phone number best stored as a string?
It says number right there in the name!
It is to do with how the data gets manipulated and output.
You hardly ever do arithmetic on a phone number.
You often need to structure a phone number in chunks, such as 123-456-7890.
Phone numbers often have a leading 0. eg. 0123-456-7890
Kiran Rana 08/20/2020