ClassS04CS141/Prog1

ClassS04CS141 | ClassS04CS141 | recent changes | Preferences

Difference (from prior author revision) (major diff, minor diff)

Added: 0a1,2
The assignment is to implement two basic data structures:
a growable array (part 1) and a hash table (part 2).

Changed: 2c4,5

Part 1:



Turn in the full assignment
by the due date: April 12, 2004 at 5pm.

Changed: 4c7,8
* Implement growable array. Time for any sequence of N operations should be O(N+M), where M is the maximum index referenced.
In all cases, you are free to change the *.h files (in fact you may have to).
Just make sure the public part of the interface stays the same.

Changed: 6,8c10
Here is the interface I want you to use for the array class.
If you are uncomfortable with templates, first implement the class
assuming the VALUE type is "int".
For prog asst 1, you can use the standard template library or the C++ standard library to implement the member functions in the *.cc files if you want. This will require changing the private data and function members in the *.h files. That's okay, just make sure your implementation has the correct time complexity for its operations.

Changed: 10,39c12

// Array.h
#ifndef _ARRAY_H
#define _ARRAY_H

// growable array, indexed by non-negative integers 0,1,2,...
//
// VALUE's are passed around as parameters and copy-assigned freely
// VALUE must be able to be assigned a value of 0 (numeric or pointer)

template <class VALUE>
class Array {

VALUE *table;
int max_referenced;
int table_size;

void grow(); // replace the table with one twice as large
// copy the old contents into the new table
public:
Array(); // start with a small table full of zeros
VALUE & operator[](int i); // returns a reference to the i'th table element
// grows the table if necessary
// newly allocated elements start out 0
~Array(); // destructor
int size(); // returns size = 1+largest index referenced so far
};

#endif

Part 1 is here. (Part 2, below, subsumes part 1.)

Changed: 41c14
Here is a simple test routine:

Part 2:




Changed: 43,45c16
<pre>
// test_array.cc
#include <iostream.h>
0. Read Section 2.5.2. We suggest a bucket array structure for implementing the HashTable class.

Changed: 47,48c18
#include "Array.h"
#include "Array.cc"
1. Download and read the AssocList? class. The files are /AssocList.h and /AssocList.cc . This class implements the same functionality as the HashTable class will, but most operations take linear time. You will also need /Comparisons.h . Please do this before week 2 lab.

Changed: 50,55c20,23
main() {
Array<int> h;

h[1] = 2;
h[2] = 4;
h[3] = 6;
2. Modify your Array class from part 1:
: a. Make the class "const correct" by adding the const keyword and adding a const operator[].
: b. Add a copy constructor, an assignment operator (operator=), and a clear() member function that empties the array.
: c. Add the following functionality. Add a constructor that takes a VALUE argument, which is then to be used to initialize every new cell (instead of 0). Change the constructor that takes no arguments so that an Array constructed with that constructor does not initialize new cells at all.

Changed: 57,58c25,36
for (int i = 0; i < h.size(); ++i) {
cout << h[i] << endl;
: An example of a suitable Array.h file is /Array.h . Your Array.h file should agree with this one on all public parts of the interface.
: Also, add the following (or some equivalent) to your Array.cc for printing Arrays:
<pre>
template <class KEY>
ostream &
operator<<(
ostream & out,
const Array<KEY> & a) {
out << "[";
for (int i = 0; i < a.size(); ++i) {
if (i != 0) out << ", ";
out << a[i];

Added: 59a38,39
out << "]";
return out;

Changed: 63,66c43,45
Here is a simple Makefile:
<pre>
test_array: test_array.o Array.o
g++ -o test_array test_array.o Array.o
3. Implement the HashTable class. Download /Hash.h . Your job is to change Hash.h as necessary and implement Hash.cc. The private part of the HashTable interface in Hash.h is appropriate for an implementation that stores (KEY, VALUE) pairs in an Array<AssocList?<KEY,VALUE> >, where the i'th element of the Array holds the KEY,VALUE
pairs with KEY hashing to i. Your hash table should grow appropriately so that most buckets have O(1) elements (assuming a good hash function), and should take space O(N) where N is the maximum number of keys
ever stored in the hash table at one time.

Changed: 68,70c47,61
Array.o: Array.h Array.cc
g++ -c Array.cc
</pre>
4. Download the simple test routine /Test1.cc . Update your Makefile to make Test1 from Test1.cc with the appropriate dependencies.

Due date and turn in




Your assignment must be turned in by Monday, April 12 at 5pm.

You will turn in the following files: Array.h, Array.cc, AssocList.h?, AssocList.cc?, Hash.h, Hash.cc, Makefile .

Turn in will be here: https://www.cs.ucr.edu/ (not yet working).

Your AssocList.cc? and all *.h files may be essentially the same as the ones given out, although they need not be.

Remember to add the header with your identifying information to each file (/ReportTemplateHeader).

A few days before the due date, this page will be updated with information about turning in assignments.

Removed: 72d62

Part 2:




Removed: 74d63
* Implement hash table class. (UNDER CONSTRUCTION 3/31/04)

The assignment is to implement two basic data structures: a growable array (part 1) and a hash table (part 2).

Turn in the full assignment by the due date: April 12, 2004 at 5pm.

In all cases, you are free to change the *.h files (in fact you may have to). Just make sure the public part of the interface stays the same.

For prog asst 1, you can use the standard template library or the C++ standard library to implement the member functions in the *.cc files if you want. This will require changing the private data and function members in the *.h files. That's okay, just make sure your implementation has the correct time complexity for its operations.

Part 1 is here. (Part 2, below, subsumes part 1.)

Part 2:

0. Read Section 2.5.2. We suggest a bucket array structure for implementing the HashTable class.

1. Download and read the AssocList? class. The files are /AssocList.h and /AssocList.cc . This class implements the same functionality as the HashTable class will, but most operations take linear time. You will also need /Comparisons.h . Please do this before week 2 lab.

2. Modify your Array class from part 1:

a. Make the class "const correct" by adding the const keyword and adding a const operator[].
b. Add a copy constructor, an assignment operator (operator=), and a clear() member function that empties the array.
c. Add the following functionality. Add a constructor that takes a VALUE argument, which is then to be used to initialize every new cell (instead of 0). Change the constructor that takes no arguments so that an Array constructed with that constructor does not initialize new cells at all.

An example of a suitable Array.h file is /Array.h . Your Array.h file should agree with this one on all public parts of the interface.
Also, add the following (or some equivalent) to your Array.cc for printing Arrays:
template <class KEY>
ostream & 
operator<<(
	   ostream & out, 
	   const Array<KEY> & a) {
  out << "[";
  for (int i = 0;  i < a.size();  ++i) {
    if (i != 0) out << ", ";
    out << a[i];
  }
  out << "]";
  return out;
}

3. Implement the HashTable class. Download /Hash.h . Your job is to change Hash.h as necessary and implement Hash.cc. The private part of the HashTable interface in Hash.h is appropriate for an implementation that stores (KEY, VALUE) pairs in an Array<AssocList?<KEY,VALUE> >, where the i'th element of the Array holds the KEY,VALUE pairs with KEY hashing to i. Your hash table should grow appropriately so that most buckets have O(1) elements (assuming a good hash function), and should take space O(N) where N is the maximum number of keys ever stored in the hash table at one time.

4. Download the simple test routine /Test1.cc . Update your Makefile to make Test1 from Test1.cc with the appropriate dependencies.

Due date and turn in

Your assignment must be turned in by Monday, April 12 at 5pm.

You will turn in the following files: Array.h, Array.cc, AssocList.h?, AssocList.cc?, Hash.h, Hash.cc, Makefile .

Turn in will be here: https://www.cs.ucr.edu/ (not yet working).

Your AssocList.cc? and all *.h files may be essentially the same as the ones given out, although they need not be.

Remember to add the header with your identifying information to each file (/ReportTemplateHeader).

A few days before the due date, this page will be updated with information about turning in assignments.


ClassS04CS141 | ClassS04CS141 | recent changes | Preferences
This page is read-only | View other revisions
Last edited April 8, 2004 3:33 pm by Neal (diff)
Search: