homework question

code for Problem 2 in hw4.pdf