Homework 2
Due: September 25, 2018 at the beginning of class
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. Please write neatly, or preferably type your answers. Use good grammar, spelling, and complete sentences. Any code you write should "just work" if I typed it in and compiled and ran it.
 (10 points each, 30 points total) Analyze these functions. For each
function, give the complete T(n) (or T(m,n), or whatever is appropriate),
then find the tightest time complexity you can, in bigOh notation. You
need not show every step in getting from T(n) to bigOh. You should assume
all inputs are > 0. Make sure you understand what the code is doing
before you analyze it.

int f1(int n) { int i = 1; // c1 for (int j = 1; // c2 j <= n; // c3 j++) { // c4 i = i * j; // c5 } return i; // c6 }

int f2(int m, unsigned int n) { for (int i = 0; // c1 i < 2 * m; // c2 i++) { // c3 for (int j = n; // c4 j > 0; // c5 j) { // c6 return j; // c7 } } return 0; // c8 }

void f3(int n) { for (int i = 0; // c1 i < n; // c2 i++) { // c3 for (int j = 10; // c4 j >= 0; // c5 j) { // c6 cout << "i = " << i; // c7 cout << ", j = " << j << endl; // c8 } } }

 (10 points) Exercise 2.11 in your textbook. For this problem and the next one, consider setting up an equation T(n1)/t1 = T(n2)/t2. Think about what this equation means.
 (10 points) Exercise 2.12 in your textbook. For part (b), you will need to use some guessandcheck to find the answer.
 (10 points) Exercise 3.9 in your textbook. (This problem is near and dear to your professor's heart, because he learned it the hard way when he was in college. He was writing a program that was misbehaving, but it wasn't clear why.)
 (10 points) Exercise 3.27 in your textbook (here "stack space" means the "space on the runtime stack" — the stack that stores function calls and local variables).
Copyright © Greg Hamerly.
Computer Science Department
Baylor University