Name: ________________________________

Honor Code Acknowledgment: ___________________


Random Quiz # 1

CPS 100E, Fall 1996

Due: September 5


Problem 1 Just the Fact's Maam: (3 points)

The code below is from Program 5.7, page 176 in the Tapestry book. Fix the code so that the function call Factorial(-4) will return 24 (which is 4!), and so that, in general, the factorial of a negative number is computed based on the absolute value of the number.

You can alter the code in the space below.

long int Factorial(int num) // precondition: num >= 0 // postcondition returns num! { long int product = 1; int count = 0; while (count < num) // invariant: product == count! { count += 1; product *= count; } return product; }

Problem 2 Nicole Kidman: (3 points)

Write code that prompts the user for a number between 1 and 20, rolls a 20-sided die until the user-entered number "appears", and prints the number of times the 20-sided die is rolled.