COM1201 Spring 1997 Professor Fell QUIZ 6

1(10 points) Given the function:

int see(int k, int A[], int lower, int upper){
// precondition:  lower and upper are valid indexes in A
// postcondition:

	cout << lower << "   " << upper <<  endl;
	if ((upper - lower) < 0) return -1;
	int N = (lower + upper)/2;
	if (A[N] == k) return N;
	if (A[N] > k) return see(k, A, lower, N - 1);
	return see(k, A, N + 1, upper);
}

a. Show the output caused by the following code. (Note: the function see() has output!)
int A[10] = {9, 11, 14, 23, 34, 39, 42, 61, 75, 88};
cout << see(34, A, 0, 9) << endl;
cout << see(75, A, 0, 9) << endl;
cout << see( 6, A, 0, 9) << endl;
cout << see(25, A, 0, 9) << endl;

b. Write the postcondition for the function


2 (5 points)
The picture at the right is the 14th level of the Gosper C Curve. It is given by the grammar:

Turning angle: 90
S -> F+F
F -> F+F-

Show the string and corresponding drawing for levels 1 and 2.

Don't worry about the orientation.

 

 

 

 

 

 

 

3 (5 points) Given the function:

void makeLL(Node* & head, Node* & tail){
	long L = 0;
	Node* extra;
	while (ReadingLong("enter int: ",L)){
		extra = new Node;
		extra->data = L;
		if (L % 2) {
			extra->next = head;
			head = extra;
			if (!tail) tail = head;
		}
		else 
		{
			extra->next = Null;
			tail->next = extra;
			tail = extra;
			if (!head) head = tail;
		};
	};
};
Make a NEAT drawing of the linked list that is created by the following code:
Node* head = Null;
Node* tail = Null;
makeLL(head, tail);
if the user interaction is as follows:
enter int:  3
enter int:  5
enter int:  6
enter int:  4
enter int:  7
enter int:         // return was pressed here


Last Updated: May 11, 1997 11:11 am 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/QUIZ/Q6/quiz6.html