Upload
pradip-kharbuja
View
1.040
Download
2
Embed Size (px)
DESCRIPTION
Software Development Technique - Topic 03
Citation preview
Topic 3 : Data Representation
Er. Pradip Kharbuja
Er. Pradip Kharbuja
Scope of Topic
1. Primitive and complex data
2. Data and computer memory
3. Scaling
4. Choosing the right data type
Er. Pradip Kharbuja
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 first_name as string
first_name = "John"
Er. Pradip Kharbuja
Data Representation
• While writing pseudocode or program first, decide what information you are going to need to store. This is data representation.
• Data representation is one of the most important things to get right when designing an algorithm.
• A good data structure will make algorithm much easier to manipulate.
• It is important to decide how your data is going to be structured.
Er. Pradip Kharbuja
Data Types
• It determines the type of data that can be stored in variable.
1. Whole Number
integer
2. Real Number
float or decimal values
Er. Pradip Kharbuja
Data Types
3. Character A data type which contains a single Unicode character
4. String collections of characters
5. Boolean A data type which contains either true or false. It can hold
nothing else.
Er. Pradip Kharbuja
Types of Data Types
two kinds of data type in most modern programming languages.
1. Primitive data types are the building blocks that are used to build all other data types.
• Whole numbers, real numbers, characters, boolean
• Primitive data types are also known as value data types.
Er. Pradip Kharbuja
Types of Data Types
2. Complex data types are those made up of combinations of primitive data types.
• Strings, Object, Array
• Complex data types are also known as reference data types.
Er. Pradip Kharbuja
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 0.
• 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 nullvalues.
Er. Pradip Kharbuja
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.
Er. Pradip Kharbuja
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.
Er. Pradip Kharbuja
Data and Computer Memory
• Pseudocode lets us ignore the implementation details that go along with an algorithm.
• One of those implementation details is how much space is taken up by different kinds of data.
• Data type is a wrapper around some part of the computer’s memory that determines the memory occupied by the variable.
Er. Pradip Kharbuja
Data and Computer Memory (Contd.)
• Because the data type is just a wrapper, different languages can handle data types differently.
• eg.
Some languages handle a String as a list of characters. eg. C, C++
Some languages handle a String as a custom data type. eg. Java, C#
Er. Pradip Kharbuja
Sizes of Data Types
Type Java C++
Whole number 4 bytes 4 bytes
Real number 8 bytes 4 bytes
Boolean 1 byte 1 byte
Character 2 bytes 1 byte
String Number of letters + 2 bytes Number of letters + 1 byte
Er. Pradip Kharbuja
• The size of every data type is different for each programming
language.
• So why the size of data types vary?
Range of Data Types
Type Java C++
Whole number -2,147,483,648 to
2,147,483,647
-2,147,483,648 to
2,147,483,647
Real number
Boolean true / false true / false
Character A character A character
String A series of character A series of character
Er. Pradip Kharbuja
-2^31 = -2,147,483,648
2^31 – 1 = 2,147,483,647
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.
Er. Pradip KharbujaS
Scaling Example
• A program needs to take age of a person.
• A program needs to check whether a person is online or offline
• Now the status can idle, invisible, busy.
• A program finds the product of two numbers.
• A program finds the product of two numbers of 10 digits
• A program stores the result of factorial.
Er. Pradip Kharbuja
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.
Er. Pradip Kharbuja
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.
Er. Pradip Kharbuja
Choosing the Right Data Type - 2
• What is the best data type for...
A phone number?
An address?
The gender of a student?
The age of a person?
Er. Pradip Kharbuja
Choosing the Right Data Type - 3
• 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.
Er. Pradip Kharbuja
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-7890Er. Pradip Kharbuja
Any Questions?End of Topic - 03
Er. Pradip Kharbuja