CSCI 2270 Computer Science 2: Data Structures
Spring 2003
Karl Winklmann
 

Recent news items :

  1. Exemptions from final exam

    Anyone who has earned all 900 possible points so far can afford to get zero points on the final and would still get an A (not an A-) in the course. Such people can therefore afford not to show up for the final.

    Extending this exemption a bit further, we will assume that anyone who has earned 875 or more points so far would easily get 25 points or more on the final and hence get an A for the course. Such people can therefore also afford not to take the final and still get an A.

    Posted: Wednesday, April 30, 2003.

  1. A sample final exam is posted. Also solutions.

    Posted: Friday, April 25, 2003.

 

Notes for Assignment 2

Monday, February 3, 2003

 


Previous page | Next page | Schedule and syllabus | Home Page | Bulletin Board | Current notes | Assad's Page | Programs | Table of contents | News archive | Dora

 

This page contains (pointers to) material covered recently that relates to Assignment 2.

Pointers and dynamic arrays

These are covered in the textbook, Ch. 4. Make sure you understand what these operations do: The code for resizing an array (really allocating a new array and deleting an existing one) was part of the function that adds a new element to the picture:
void Picture::add( const MovingObject& mo )
{
    MovingObject* oldElements;

    if (n == capacity) // array is full, increase its size
    {
        oldElements = elements;
        elements = new MovingObject [2 * capacity];
        copy (oldElements, oldElements + capacity, elements);
        capacity *= 2;
        delete [] oldElements;
    }

    // now insert the new MovingObject
    elements [n++] = mo;
}

Assignment operators, copy constructors, destructors

These three you have to write for all classes that use dynamically allocated memory, i.e., use the new operator. This means all data structures more sophisticated than arrays of constant size. The exception are those rare cases when “shallow” copies are what you want; in those cases you still need a destructor to avoid memory leaks but you can get away without writing an assignment operator and a copy constructor.

Be sure you understand the difference between “shallow” copies (two pointers pointing to one and the same array) and “deep”copies (two pointers pointing to two arrays whose contents are equal).

Code for those three operations in Assignment 2 that was written in class (but the variable names may be different here) included:

// constructors, destructor

Picture::Picture( ) 
{
    elements = new MovingObject [8];; 
                     // array of picture 'elements' (each is a 
                     // snapshot of a moving object)
    capacity = 8;
    n = 0; // number of elements
}

// copy constructor
Picture::Picture( const Picture& pic )
{
    elements = NULL; // to prevent the following assignment
                     // from applying 'delete' to an
                     // uninitialized pointer
    *this = pic;
}

// destructor
Picture::~Picture( )
{
    delete [] elements;
}

// assignment operator
void Picture::operator =( const Picture& pic )
{
    if (this == &pic) // self-assignment 'A=A', nothing to do
        return;

    delete [] elements;
    
    elements = new MovingObject [pic.capacity];
    capacity = pic.capacity;
    n = pic.n;

    copy (pic.elements, pic.elements + pic.capacity, elements);
}

Using the sort function

You can do
sort (pic.elements, pic.elements + pic.n);
if you define a less-than operator:
bool operator< ( const MovingObject& mo1, const MovingObject& mo2 )
{
    return mo1.z > mo2.z; // yes, backwards
}

Reading from a text file

Be sure to understand the distinction between the name of a file (what shows up in a directory listing) and the file variable (here testFile) used in a program.

Code discussed in class includes:

 #include <fstream>
 ...
 ifstream testFile;
 string testFileName;
 ...
     ... // get the file name from the user

     testFile.open (testFileName.c_str ());
     while (!testFile) // 'open' didn't succeed
     {
         ... // have a word with the user
     }
                       
     while (testFile.peek () != EOF)
     {
         testFile >> x >> y >> z >> dx >> dy >> dz;
         ... // throw the thing
     }
 
     testFile.close ();
 


© Karl Winklmann               7:10 PM, Friday, May 2, 2003