Escolar Documentos
Profissional Documentos
Cultura Documentos
Analysis
13 CS- I & II
Engr. Maria Shaikh
maria.sheikh@faculty.muet.edu.pk
Exams
3 Sessional Tests
Home Assignments / Presentation
Mid Term + Final Exams
8/16/2014
8/16/2014
Read Ahead
You are expected to read the lecture
notes before the lecture.
This will facilitate more productive
discussion during class.
Also please
proof read
Like in an
assignments
English class
& tests.
5
8/16/2014
Marium
Age
21
Sex
Female
Social Security No
1325-76899-09
Age
SSNO
Name
EMPLOYEE
8/16/2014
10
8/16/2014
11
8/16/2014
12
What is programming
The art and craft of writing programs
The art to control these idiot servants and
nave children
Engr. Maria Shaikh
13
Introduction to Programming
Programming is to solve problems using computers
How to do it at all ?
How to do it robustly ?
How to do it effectively ?
Programming consists of two steps:
Algorithmic design (the architects)
Coding (the construction workers)
Programming requires:
A programming language (C/C++/C#) to express your
ideas
A set of tools to design, edit, and debug your code
A compiler to translate your programs into machine code
A machine to run the executable code
Engr. Maria Shaikh
14
15
Good Programs
16
8/16/2014
17
Data Structures
Data structure introduction refers to a scheme for
organizing data, or in other words a data structure is an
arrangement of data in computer's memory in such a way
that it could make the data quickly available to the
processor for required calculations.
A data structure should be seen as a logical concept
that must address two fundamental concerns. First, how
the data will be stored, and second, what operations will
be performed on it?
8/16/2014
18
Data Structure
Definition
A data structure is a collection of data, generally
organized so that items can be stored and retrieved
by some fixed techniques.
A particular format of representing information.
Example
An array
Stored and retrieved based on an index assigned to
each item
19
LINEAR
NON
LINEAR
Engr. Maria Shaikh
Algorithm
Along with data structures introduction, in real life,
problem solving is done with help of data structures and
algorithms.
An algorithm is a step by step process to solve a
problem in a finite amount of time.
In programming, algorithms are implemented in form of
methods or functions or routines.
To get a problem solved we not only want algorithm but
also an efficient algorithm.
One criteria of efficiency is time taken by the
algorithm, another could be the memory it takes at run
time.
8/16/2014
21
Algorithm
The best algorithm is the one which has a fine
balance between time taken and memory consumption.
But, as we know the best exists rarely, and we
generally give more priority to the time taken by the
algorithm rather than the memory it consumes.
8/16/2014
22
Analysis
Generalized information gathering
i. How much time an operation will take?
ii. How much memory will be utilized?
iii. Which data Structure will be more
effective?
8/16/2014
23
24
8/16/2014
25
26
8/16/2014
27
8/16/2014
28
8/16/2014
29
8/16/2014
30
data items.
Stacks:
31
8/16/2014
32
8/16/2014
33
8/16/2014
34
Tree properties
one node is distinguished as the root
every node (except the root) is connected by an
edge from exactly one other node
A unique path traverses from the root to each
node
8/16/2014
35
Edges
8/16/2014
36
37
ALGORITHM
"An algorithm is a well defined list of steps to solve a
particular problem". "It is an abstract form of a
program."
A set of algorithms are always used to perform operations
on data structure. Data may be organized in many ways
and many levels so algorithm has to be designed in such a
manner so that it can perform the desired operation on
the stored data.
An algorithm is a well defined list or sequence of steps to
solve a particular problem. There may be more than one
algorithm to solve a particular problem. The choice of a
particular algorithm depends on the following
considerations:
8/16/2014
Engr. Maria Shaikh
38
ALGORITHM
Performance requirements i.e. time complexity
Memory requirements i.e. space complexity
Each algorithm is a list of well-defined instructions
for completing a task. Starting from an initial state,
the instructions describe a computation that
proceeds through a well-defined series of successive
states, eventually terminating in a final ending state.
8/16/2014
39
ALGORITHM COMPLEXITY
"The complexity of an algorithm is the
amount of time and the amount of space
that is consumed by algorithm to run
to completion". The complexity is of the
following types
Space Complexity: - It is also known as memory
requirement.
Time Complexity: - It is also known as
performance requirement.
8/16/2014
40
SPACE COMPLEXITY
"The space complexity of an algorithm is the
amount of memory it needs to run to
completion". The space needed by a program
consists of following componentsInstructions space- to store executable version
of program.
Data space- to store all constants, variables etc.
Environment stack space- it is used in case of
recursive program .
8/16/2014
41
SPACE COMPLEXITY
The total space needed by a program can be
divided in two parts- A fixed part that is
independent of particular problem, and
includes instructions space, space for
constants, variables and fixed size structure
variables A variable part that includes
structure variables whose size depends on the
particular problem being solved dynamically
allocated space and the recursion stack space.
8/16/2014
42
TIME COMPLEXITY
"The time complexity of an algorithm is the
amount of time it needs to run to
completion". Some of the reasons for studying
time complexity are
We may be interested to know in advance
that whether the program will provide a
satisfactory real time response.
There may be several possible solutions with
different time requirements or with
different time complexity.
8/16/2014
43
ALGORITHM: Time-Space
Trade Off
The best algorithm to solve a given problem is one that
requires less space in memory and takes less time to
complete its execution.
But in practice it is not always possible to achieve both
these objectives.
As we know there may be more then one approach to
solve a particular problem. One approach may take more
space but takes less time to complete its execution while
the other approach may take less space but takes more
time to complete its execution.
8/16/2014
44
ALGORITHM: Time-Space
Trade Off
We may have to sacrifice one at the
cost of the other. If space is our
constraint, then we have to choose a
program that requires less space at the
cost of more execution time. On the
other hand if time is our constraint
then we have to choose a program that
takes less time to complete its
execution at the cost of more space
8/16/2014
45
Variables
A variable are place holders to store data.
Variables in a computer program are analogous to
"Buckets" or "Envelopes" where information can be
maintained and referenced. On the outside of the bucket
is a name. When referring to the bucket, we use the name
of the bucket, not the data stored in the bucket.
Variables can be characterized as a 6-tuple of attributes:
Name:
identifier
Address: memory location(s)
Value:
particular value at a moment
Type:
range of possible values
Lifetime: when the variable accessible
Scope:
where in the program it can be accessed
Engr. Maria Shaikh
Data Types
A data type in a programming language is a set of data
with values having predefined characteristics.
Examples of data types are: integer, floating point
unit number, character, string, and pointer.
Usually, a limited number of such data types come
built into a language.
The language usually specifies the range of values for
a given data type, how the values are processed by
the computer, and how they are stored
8/16/2014
47
Type Checking
Generalize the concept of operands and operators to
include subprograms and assignments
Type checking is the activity of ensuring that the
operands of an operator are of compatible types
A Compatible type is one that is either legal for the
operator, or is allowed under language rules to be
implicitly converted, by compiler-generated code, to a
legal type.
This automatic conversion is called a coercion.
A type error is the application of an operator to an
operand of an inappropriate type
8/16/2014
48
Compatibility of a Variable
Type compatibility by name: means the two
variables have compatible types if they are in
either the same declaration or in declarations
that use the same type name.
Type compatibility by structure: means that
two variables have compatible types if their
types have identical structures
More flexible, but harder to implement
8/16/2014
49
50
Scope
Files, function bodies and compound
statements define a scope
A scope is a region of a program text in
which declarations are valid
The concept of locality states that
declarations are local to their scope
A name cannot be used outside of its scope
A name can only be defines ONCE in any scope
8/16/2014
51
File Scope
A C++ source file defines a file scope
Splitting a program into different files restricts
variables defined in the file to the file scope
Programs have a global scope
Variables in the global scope may be accessed from
any part of the program
By default any name declared in file scope is GLOBAL
The scope of a global name extends from its point of
declaration to the end of the file it is declared in
Try to avoid the use of global variable
8/16/2014
52
Local Scope
Names declared in a function body or compound
statement are local to that scope
{
int local_x ;
// local to this scope
// some statements
}
The name local _ x is valid from its point of
declaration to the end of the scope - the closing
bracket.
Outside of the compound statement local _ x is not
accessible.
8/16/2014
53
Nesting Scopes
Scopes can be nested
{
int x;
int y;
// some statements
{
int x;
// hide outer x
x = 1; // use the local x
y = 2; // y still visible
}
// more statements
}
Note - Names in an outer scope are accessible unless they are hidden by
a local declaration of the same name
8/16/2014
54
Lifetimes of Variables
A variable is created when its scope is
entered during the execution of a program.
A variable is destroyed when its scope is left
during the execution of a program.
{
8/16/2014
55
Lifetimes of Variables
When the program enters the scope
local_x is created.
When the program leaves the scope
local_x is destroyed.
When the program enters the scope
again a NEW variable called local_x is
created.
8/16/2014
56
Control Structures
Control
structures
Sequential
Iterative /
Repetitive
For Loop
While Loop
Do Loop
8/16/2014
Conditional
If-Statement
If-else
Switch Case
57
8/16/2014
58