DSA Tutorial2 Data Abstraction
DSA Tutorial2 Data Abstraction
By Omieno K. Kelvin
Data Types
o A data type is a collection of objects and a set of operations that act on those objects.
o Natural data types
Books
Students
Beers
o Simple computer data types (often predefined in a programming language)
int
float
char
o Aggregate computer data types (often user defined)
Arrays, e.g.,
const int MAX_STRING_LENGTH = 80;
typedef char string[MAX_STRING_LENGTH + 1];
structs, e.g.,
typedef struct {
string StudentName;
int NumberOfBooks;
char Grade;
} student_record;
An Array Example
Let’s look at some sample programs that show how an array can be used. In case you’re making the transition
to OOP, we’ll start with an old-fashioned procedural version, and then show the equivalent object-oriented
approach. Listing 2.1 shows the old-fashioned version, called array.cpp.
//array.cpp
//demonstrates arrays
#include <iostream>
using namespace std;
////////////////////////////////////////////////////////////////
int main()
{
int arr[100]; //array
int nElems = 0; //number of items
int j; //loop counter
int searchKey; //key of item to search for
//--------------------------------------------------------------
arr[0] = 77; //insert 10 items
arr[1] = 99;
arr[2] = 44;
arr[3] = 55;
arr[4] = 22;
arr[5] = 88;
arr[6] = 11;
arr[7] = 00;
arr[8] = 66;
arr[9] = 33;
nElems = 10; //now 10 items in array
//--------------------------------------------------------------
for(j=0; j<nElems; j++) //display items
cout << arr[j] << “ “;
cout << endl;
//--------------------------------------------------------------
searchKey = 66; //find item with key 66
for(j=0; j<nElems; j++) //for each element,
if(arr[j] == searchKey) //found item?
break; //yes, exit before end
if(j == nElems) //at the end?
cout << “Can’t find “ << searchKey << endl; //yes
else
cout << “Found “ << searchKey << endl; //no
//--------------------------------------------------------------
searchKey = 55; //delete item with key 55
cout << “Deleting “ << searchKey << endl;
for(j=0; j<nElems; j++) //look for it
if(arr[j] == searchKey)
break;
for(int k=j; k<nElems; k++) //move higher ones down
arr[k] = arr[k+1];
nElems--; //decrement size
//--------------------------------------------------------------
for(j=0; j<nElems; j++) //display items
cout << arr[j] << “ “;
cout << endl;
return 0;
} //end main()
In this program, we create an array called arr, place 10 data items (kids’ numbers) in it,
search for the item with value 66 (the shortstop, Louisa), display all the items, remove the
item with value 55 (Freddy, who had a dentist appointment), and then display the remaining
nine items.
The output of the program looks like this:
77 99 44 55 22 88 11 0 66 33
Found 66
77 99 44 22 88 11 0 66 33
The data we’re storing in this array is type int. We’ve chosen a basic type to simplify the
coding. Generally the items stored in a data structure consist of several data members, so
they are represented by objects rather than basic types. We’ll see an example of this in the
next hour.
Deleting an Item
Deletion begins with a search for the specified item. For simplicity we assume (perhaps rashly) that
the item is present. When we find it, we move all the items with higher index values down one
element to fill in the “hole” left by the deleted element, and we decrement nElems. In a real program,
we would also take appropriate action if the item to be deleted could not be found.
Displaying the Array Contents
Displaying all the elements is straightforward: we step through the array, accessing each one with
arr[j] and displaying it.
Summary
In this tutorial, you’ve learned the following:
Unordered arrays offer fast insertion but slow searching and deletion.
Wrapping an array in a class protects the array from being inadvertently altered.
A class interface comprises the member functions (and occasionally data members) that the class user
can access.
A class interface can be designed to make things simpler for the class user (although possibly harder
for the class designer).
Exercises
On average, how many items must be moved to insert a new item into an unsorted array with N
items?
2. On average, how many items must be moved to delete an item from an unsorted array with N
items?
3. On average, how many items must be examined to find a particular item in an unsorted array with
N items?
Implementing a one dimensional array ADT