Reasons for this approach:
1. Program easy to read.
2. Program easy to debug.
3. More than one person can work on it.
4. Easy to maintain. (80% of all programming maintenance. Maintenance is not
as trivial as it sounds)
5. Allows the creation of libraries.
Note that classes provide a higher degree of modularization. Much bigger brush strokes with classes.
Discuss evils of global variables, goto, etc.
An Abstract Data Type (ADT) is a mathematical abstraction. It consists
of the definition of a data structure and a set of operations. There is
no description of implementation in an ADT. Why have ADT? For the sake
of having well-defined data structures. ADT also provide a framework
from which general programming elements may be created. Discuss STL.
It also provides a basis for mathematical analysis of the various data
structures.
Note: even though an ADT is independent from its implementation, it is clear that its implementation should be done with a class. Why?
Think of how algebra abstracts basic
mathematics
The general form of a list is:
A1, A2 , A3 , . . . ,AN
This is a list of size N. A list of size 0 is called an empty list.
Definition: For legal subscripts Ai precedes Ai+1 and Ai+1 follows Ai.
The position of Ai is i.
Operations on a list.
PrintList, MakeEmpty, Find (returns the position of the first occurrence of a key), Insert (inserts a key at a given position.), Delete (deletes a key from a given position), FindKth (returns the element at a specified position.), SizeList. Could add more. See text.
Create a class to implement lists using arrays. We will
use an array of doubles.
It would be good to have a general solution. But, with what you know right now, this cannot be done. When you know templates, it will be trivial to turn our solution in to one that works for a variety of data types.
Note: C++ classes are a lot better for this example than using functions. So. this is what we will use. We will first talk through the functions without coding and then we will look at the code that I have written.
The solution
is in the following files ArrayList.h,
ArrayList.cpp,
testList.cpp
Finish the implementation of the ArrayList class by creating the definition of the FindKth and Delete member functions. and enhance the implementation of the list by implementing and testing the following function:
int Replace( double a, double b );
The function
will replace all the occurrences of a by b in the list x.
Please look at the various functions presented in the text.