Solutions to Homework I


COM1201 Algorithms and Data Structures II Spring 1997 Professor Fell

1. (->*& and all that) based on problem 2.17 in Ford and Topp
For the following code segment, show the values of x, y, *p, *q at the five places indicated.

	int	x = 5;	
	int y = 7;
	int *p = &x;
	int *q = &y;		// 1a.	x		y		p		q
					5		7		5		7
	(*p)--;		// 1b.	
					4		7		4		7 
	*q += *p;		// 1c.	
					4		11		4		11
	q = p;			// 1d.	
					4		11		4		4
	*q = 55; 		// 1e.	
					55		11		55		55

2. (arrays and pointers ) also based on problem 2.17 in Ford and Topp
For the following code segment, show the values of *Dptr and the contents of the array D[] at the six places indicated.

	double D[] = {3.1, 5.2, 4.4, 0.1, -5.0, 0.0, 9.8};
	double* Dptr = D;	
				// 2a.D[] = {3.1   5.2  4.4  0.1  -5  0    9.8} *Dptr=3.1
	*Dptr += 7.7;
				// 2b.D[] = {10.8  5.2  4.4  0.1  -5  0    9.8} *Dptr=10.8
	Dptr++;
				// 2c.D[] = {10.8  5.2  4.4  0.1  -5  0    9.8} *Dptr=5.2
	*Dptr++ = 9.9;
				// 2d.D[] = {10.8  9.9  4.4  0.1  -5  0    9.8} *Dptr=4.4
	Dptr +=2;
				// 2e.D[] = {10.8  9.9  4.4  0.1  -5  0    9.8} *Dptr=-5
	*++Dptr = 8.8;
				// 2f.D[] = {10.8  9.9  4.4  0.1  -5  8.8  9.8} *Dptr=8.8

3. (a bit of binary)
a. Compute the decimal value of each of the following binary numbers

	(i)	1001	(ii)	111111111	(iii)	101011	(iv)	1000001
		9		255			43		65
b. Find the binary value of each of the following decimal numbers
	(i)	19	(ii)	78	(iii)	511	(iv)	129
		10011		1001110		111111111	10000001

4. (a heap of hex)
a. Compute the decimal value of each of the following hex(adecimal) numbers

	(i)	1D	(ii)	12B	(iii)	1000	(iv)	FF
		29		299		4096		255
b. Compute the hex value of each of the following decimal numbers
	(i)	29	(ii)	257	(iii)	123	(iv)	11
		1D		101		7B		B
c. Compute the binary value of each of the following hex(adecimal) numbers
	(i)	AF	(ii)	BBCC		(iii)	1001		(iv)	EE
		1010 1111	1011 1011 1100 1100	1 0000 0000 0001	1110 1110
d. Compute the hex value of each of the following binary numbers
	(i)	10110011	(ii)	11111111	(iii)	10010001	
		B3			FF			91
	(iv)	1010010111000011
		A3C2
5.Write the declarations only for a class DynamicSet that has objects of template class T. The first programming assignment - to be started in lab Tuesday April 8, will involve implementing and using this class.
template	// key and data classes
class Node{
public:
	K			key;
	T			data;
	Node*	 	next;	// add Node* last;  if you want a doubly linked list	
};

template	// key and data classes
class DynamicSet{
private:
	typedef Node*	NodePtr;
	
	NodePtr	head;
	NodePtr	tail;
	
// create a new node with key = k, data = d, next = Null, and p pointing at the node
	void MakeNode(NodePtr& p, K	k, T d){};
	
// call this to print one node p in this list (key and data)
	void PrintNode(NodePtr& p){};		

public:
	// constructor
	DynamicSet(){ head = NULL; tail = NULL; }; // Creates empty set
	
	// destructor
	~DynamicSet(){};				// Delete all items in the set

//---------------------------- Updaters -----------------------------------------	
	void Delete(Node* & place){};
	// precondition:	p points to a Node in the list
	// postcondition:	the node p points at is removed from the list
	
	// insert data d into this list in sorted position
	void Insert(K k, T d){};
	// postcondition:	a node is created in the set with key k and data d
//----------------------------- Queries -----------------------------------------
	Node* & Search(K k){};
	// postcondition:	returns a pointer to node with key k, NULL if none exists

	Node* & Succ(Node* & place){};
	// precondition:	place points to a node in the set
	// postcondition:	returns pointer to node with next higher key, 
	//			returns NULL if none exists

	Node* & Pred(Node* & place){};
	// precondition:	place points to a node in the set
	// postcondition:	returns pointer to node with next lower key, 
	//			returns NULL if none exists


	Node* & Minimum(){};
	// postcondition:	returns pointer to node with lowest key, 
	//			returns NULL if set is empty

	Node* & Maximum(){};
	// postcondition:	returns pointer to node with highest key, 
	//			returns NULL if set is empty

};

Last Updated: April 6, 1997 11:04 pm by

Harriet Fell
College of Computer Science, Northeastern University
360 Huntington Avenue #161CN,
Boston, MA 02115
Internet: fell@ccs.neu.edu
Phone: (617) 373-2198 / Fax: (617) 373-5121
The URL for this document is: http://www.ccs.neu.edu/home/fell/COM1101/HW/HW2/hw2Solution.html