
Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!

Due 10/27/08 before class
Please refer to the corresponding exercise sections in the textbook (Rosen, 6th edition).
4.5 • (4 points) Use the loop invariant (I) : power = 2k^ to show that the code below correctly computes 2n^ for any n ≥ 0. First, use induction to show that (I) is indeed a loop invariant, and then draw conclusions for the termination of the while loop.
int power_of_two(int n){ int power=1; int k=0; while(k<n){ // (I) power=2^k k++; power = power*2; } return power; }
4.3 (page 308)
4.4 (page 321)
int factorial(int n){ if(n==0) return 1; return factorial(n-1)*n; }