Transcript Lecture 2
ES 314 Advanced Programming
Sept 3
Lec 2
Goals:
•Complete the discussion of problem
•Review of C++
•Object-oriented design
•Arrays and pointers
1
Object-Oriented Design
• Coding without a solution design increases
debugging time
• A team of programmers for a large software
development project requires
– An overall plan
– Organization
– Communication
• Software engineering
– Provides techniques to facilitate the
development of computer programs
2
An Examination of Problem Solving
• Problem solving
– The process of taking the statement of a
problem and developing a computer
program that solves that problem
• Object-oriented analysis and design
(OOA / D)
– A process for problem solving
– A problem solution is a program consisting of a
system of interacting classes of objects
• Each object has characteristics and behaviors
related to the solution
• A class is a set of objects having the same type
3
Abstraction and Information Hiding
• Abstraction
– Separates the purpose of a module from
its implementation
– Specifications for each module are written
before implementation
– Functional abstraction
• Separates the purpose of a module from its
implementation
4
Abstraction and Information Hiding
– Data abstraction
• Focuses on the operations of data, not on the
implementation of the operations
– Abstract data type (ADT)
• A collection of data and a set of operations on
the data
• You can use an ADT’s operations without
knowing their implementations or how data is
stored, if you know the operations’ specifications
5
Abstraction and Information Hiding
– Data structure
• A construct that you can define within a
programming language to store a collection of
data
– Develop algorithms and ADTs in tandem
6
Abstraction and Information Hiding
• Information hiding
– Hide details within a module
– Ensure that no other module can tamper
with these hidden details
– Public view of a module
• Described by its specifications
– Private view of a module
• Implementation details that the specifications
should not describe
7
Principles of Object-Oriented Programming
• Object-oriented languages enable us
to build classes of objects
• A class combines
– Attributes (characteristics) of objects of a
single type
• Typically data
• Called data members
– Behaviors (operations)
• Typically operate on the data
• Called methods or member functions
8
What is a Good Solution?
• A solution is good if:
– The total cost it incurs over all phases of its life
cycle is minimal
• The cost of a solution includes:
– Computer resources that the program consumes
– Difficulties encountered by users
– Consequences of a program that does not
behave correctly
• Programs must be well structured and
documented
• Efficiency is one aspect of a solution’s cost
9
Key Issues in Programming
1.
2.
3.
4.
5.
6.
7.
Modularity
Style
Modifiability
Ease of Use
Fail-safe programming
Debugging
Testing
10
Modularity
• Modularity has a favorable impact on
–
–
–
–
–
Constructing programs
Debugging programs
Reading programs
Modifying programs
Eliminating redundant code
11
Style
1.
2.
3.
4.
Use of private data members
Proper use of reference arguments
Proper use of methods
Avoidance of global variables in
modules
5. Error handling
6. Readability
7. Documentation
12
Data Structures – key to software design
• Data structures play a key role in every type of
software.
•Data structure deals with how to store the
data internally while solving a problem in order
to
•Optimize the overall running time of a program
•Optimize the response time (for queries)
•Optimize the memory requirements
•Optimize other resources (e.g. band-width of a
network)
• Simplify software design
• make solution extendible, more robust
13
Abstract vs. concrete data structures
• Abstract data structure (sometimes called ADT ->
Abstract Data Type) is a collection of data with a set of
operations supported to manipulate the structure
• Examples:
– stack, queue insert, delete
– priority queue insert, deleteMin
– Dictionary insert, search, delete
• Concrete data structures are the implementations of
abstract data structures:
– Arrays, linked lists, trees, heaps, hash table
• A recurring theme: Find the best mapping between
abstract and concrete data structures.
14
Abstract Data Structure (ADT)
supporting operations
•Dictionary
•search
•insert
•Delete
•deleteMin
•Range search
•Successor
•Merge
primary operations
secondary operations
•Priority queue
•Insert
primary operations
•deleteMin
•Merge, split etc. Secondary operations
15
Linear data structures
• key properties of the (1-d) array:
• a sequence of items are stored in consecutive
memory locations.
• array provides a constant time access to k-th
element for any k.
(access the element by: Element[k].)
• inserting at the end is easy.
if the current size is S, then we can add x at the end
using the single instruction:
Element[S++] = x;
• deleting at the end is also easy.
• inserting or deleting at any other position is
expensive.
• Even searching is expensive (unless sorted).
16
Linked lists
• Linked lists:
order is important
– Storing a sequence of items in nonconsecutive locations of the memory.
– Not easy to search for a key (even if
sorted).
– Inserting next to a given item is easy.
– In doubly linked list, inserting before or after
a given item is easy.
– Don’t need to know the number of items in
advance. (dynamic memory)
17
stacks and queues
• stacks:
• insert and delete at the same end.
• equivalently, last element inserted will be
the first one to be deleted.
• very useful to solve many problems
•Processing arithmetic expressions
• queues:
• insert at one end, deletion at the other
end.
• equivalently, first element inserted is the first
one to be deleted.
18
Priority queues
• insert, deleteMin – main operations
• merge, split, etc. – secondary operations
• expected performance:
• number of operations performed for insert
and deletemin – should both be much
smaller than n, the number of keys in the
queue.
19
Hashing
• dictionary operations
• expected performance
• constant time on average for each of the
operations search, insert and delete.
20
Arrays and pointers
• variable name
• variable
• value
• address – a binary number used by the
operating system to identify a memory
cell of RAM
• It is important to know the precise
meanings of these terms
Memory Terminology (cont.)
Addresses
00110
01010
01110
10010
10110
15
x
Memory Terminology (cont.)
Addresses
Variable
name
00110
01010
01110
10010
10110
15
x
Memory Terminology (cont.)
Addresses
00110
01010
01110
10010
10110
15
x
A variable
(which is a
location)
Memory Terminology (cont.)
Addresses
00110
01010
01110
10010
10110
15
x
Value of the
variable
Memory Terminology (cont.)
Addresses
00110
01010
Address of
the
variable
01110
10010
10110
15
x
Array Example
Write a program that finds the largest number
in a given collection of keys.
Assume the numbers are stored in an array.
int max (int[] A, int size)
27
Array Example
Write a program that finds the largest number
in a given collection of keys.
Assume the numbers are stored in an array.
int max (int[] A, int size)
{
if (size == 0) return 0;
int temp = A[0];
for (int j = 1; j < size; ++j)
if (temp < A[j]) temp = A[j];
return temp;
}
28
Implementation using a vector
29
Notion of time complexity
int max (int[] A, int n)
{
if (n == 0) return 0;
int temp = A[0];
for (int j = 1; j < size; ++j)
if (temp < A[j]) temp = A[j];
return temp;
}
What is the total number of operations performed by
the above procedure (as a function of n)?
Assignment, comparison, return – each costs one unit.
30