# Homework 0

Due: 2019/01/29 at the beginning of class.

Homework should be submitted in hardcopy in class, stapled, clearly
labeled with your name.

Homework turned in after class will not be accepted.

Homework turned in during class but after I have collected it will lose 20%.

Do your own work for this assignment; do not work with others. Consult the book and your professor for help if you need it. Please write neatly, or preferably type your answers. Use good grammar, correct spelling, and complete sentences.

Each question is worth 10 points.

- Define what is meant by the run-time stack, and the run-time heap. Explain how each of them are used.
- When the following function runs, what variables and memory live on the
run-time stack? What variables and memory live on the on the run-time heap?
The things you should consider are underlined.
class Node { public: Node(int v, Node *n = NULL) { value = v; next = n; } private: Node *next; int value; }; Node *createList(int numberElements) { Node *head = NULL; for (int i = 0; i < numberElements; i++) { int x; cin >> x; head = new Node(x, head); } return head; }

- Using proof by induction, prove that the sum of integers 1, 2, ..., n is equal to n(n+1)/2.
- Name and define the four fundamental rules of recursion.
- Will the following C++ function terminate for all inputs? Prove your
answer. You should assume that the int type does not wrap around. That is, it
will not overflow; the int type can represent any integer, positive or
negative.
void printToN(int n) { for (int i = 0; i != n + 1; i++) { cout << i << endl; } }

- What are the two things that define an Abstract Data Type (ADT)? Give the answer we discussed in class.
- Your book identifies a set of guidelines for passing parameters that are (a) small and need not be modified, (b) large and must not be modified, and (c) anything that may be modified. However, these differ from the conventions we will use in this class. How are they different?
- What are the "big five" functions that any C++ class must have if it allocates its own memory (on the heap)? Explain why are they needed (collectively).

Copyright © Greg Hamerly.

Computer Science Department

Baylor University