The Program Life Cycle. - Concordia University Wisconsin
Download
Report
Transcript The Program Life Cycle. - Concordia University Wisconsin
Day 7.
1. Templates: the concept.
2. Template methods and classes.
3. Intro to data structures.
4. Stacks.
5. Base conversion.
1. Templates: the concept.
A. Templates are a relatively new feature.
B. Introduced into C++ in the 90’s, they have
since been used to form whole libraries
e.g. STL, the standard template library.
C. The idea of templates is to create generic
algorithms which are data-independent,
i.e. the same code can be re-used for any
data type.
Templates: the concept (cont).
D. The motivation: before templates many
applications required essentially the same
algorithms, e.g. Search, Sort, Update etc.,
but these had to be re-coded over and over
again because the data was of different
types. Code for searching integers would
not work for searching string, for example.
Templates: the concept (cont).
E. The answer. Templates suggest yet
another form of code re-use—define code
that can be re-used by multiple data types.
To do this we write code using a generic
place holder T for the data type. Then,
when an application wants to use it, it
provides a particular data type e.g. float.
Templates: the concept (cont).
The compiler then generates a version of the
code, which substitutes float for every
occurrence of T.
In the end, there does need to be a version of
the algorithm tuned to a specific data
type—but this code is generated
automatically by the compiler. This is an
example of high-level code generation.
2. Template methods and classes.
Using templates, we can define generic, dataindependent methods, that can be used to generate
multiple versions of the function, e.g. from text, p. 15:
static void Swap<T> (ref T val1, ref T val2) {
T temp;
temp = val1;
val1 = val2;
val2 = temp;
}
Template methods.
Note how <T> indicates a placeholder for any data
type.
This means that we can substitute any data type for T
when we call Swap, e.g.
Swap <int> (ref num1, ref num2);
Swap<string> (ref str1, ref str2);
Template classes.
Classes can also be defined using a template, so we
can maintain a collection of any type of element.
public class Stack<T> {
}
In this way the same code can be used for stacks of
any type of element.
More on this later.
3. Intro to Data Structures.
Recall our 3 basic definitions:
Def. 1. A data structure is an organized
collection of data with an access method.
Def. 2 An access method is a mechanism for
accessing individual data elements within
the data structure.
Def. 3. An Abstract Data Type is the
abstract, logical idea of a data structure.
Intro to Data Structures (cont.).
There are also 3 levels on which one can view
a data structure:
A) The abstract level: the logical idea or
concept (the ADT).
B) The application level: what sort of
application is the data structure useful for?
C) The implementation level: how can the
data structure be coded, pros and cons.
Intro to Data Structures (cont.).
A) The abstract level focuses on the
mathematical / logical concept,
independent of any specific H/W or S/W.
B) The application level focuses on problems
where it is / is not natural to think in terms
of a stack / queue etc. Do we need to
process data in order (queue) or in reverse
order (stack)?
Intro to Data Structures (cont.).
C) The implementation level focuses on how
we simulate the data structure in a
programming language, such as C#.
For example, do we use a static container
(e.g. an array) or dynamic data (e.g. a
dynamic linked list)? What are the
tradeoffs in development time / execution
time, ease of use / robustness?
4. Stacks.
The abstract level.
This focuses on the logical organization of the
data, its access methods, properties and
other characteristics.
LIFO organization, with Top the only access
point, accessed by Push and Pop methods;
properties include Empty and Full.
Stacks (cont.).
Characteristics:
1. time ordered (top of stack is most recent
addition, bottom is oldest);
2. elements are homogeneous though may
encapsulate complexity e.g. in objects.
3. dynamic (can shrink / grow).
4. lower limit (empty) but no upper limit.
Stacks (cont.).
We will look in detail at the application level
next time.
The implementation level.
There is no keyword “stack” and logical
stacks cannot exist in the computer
because they have no upper bound,
whereas all CPUs are bounded by
memory.
Stacks (cont.).
Therefore, we must simulate a stack.
We can do this using an array. Even though the
array is static, we can make it big enough (we
hope) to handle the actual amount of data
needed, and then allow the stack to shrink or
grow inside the array. We also need a separate
variable to store the location of the current top of
the stack.
See diagram.
Stacks (cont.).
We must then define functions:
1) Constructor sets Stack to empty, so
make Top =???
2) Observers: boolean methods empty and
full. Why important?
Can’t pop if…...
Can’t push if……
Stacks (cont.).
3) Push (if not Full)
Top++;
Stack[Top] = NewElement; // 2 steps
Or
Stack[++Top] = NewElement; // 1 step
4) Pop??
Template based stacks.
Wouldn’t it be cool if you could define the
stack code using templates, so it can be reused regardless of what data type it is a
stack of?
We can!
See Tempstak.cs and Stack_Driver.cs.
Work through code.
5. Base conversion.
One application of stacks is in converting
numbers from one base to another, e.g. from
base 10 to base 8 (octal).
What is the algorithm by hand?
1) Repeatedly divide by the base (8);
2) Read remainders from bottom to top.
Example.
What about this algorithm suggests the use of
a stack?
How could we use a stack to implement it?
E.g. convert 167 base 10 to base 8;
Convert 91 base 10 to base 2.
Program 3.
Hand-out program 3, due next Tuesday.
You can use the template stack code
Tempstak.cs if you wish.
Then focus on a reliable algorithm in your
driver.