CSCI 2270
Computer Science 2:
Data Structures
Spring 2003
Karl Winklmann
Recent news items :
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.
Monday, February 3, 2003
This page contains (pointers to) material covered recently that relates to Assignment 2.
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;
}
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);
}
sort (pic.elements, pic.elements + pic.n);
bool operator< ( const MovingObject& mo1, const MovingObject& mo2 )
{
return mo1.z > mo2.z; // yes, backwards
}
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 ();