## Tips for Quiz 5

One way to calculate the factorial of an integer is using iteration (=loop):

int fact(int n) {
int f = 1;
for ( int i = 2; i <= n; i++ )
f *= i;
return f;
}

Alternatively, one can use a recursive definition, such as fact(n) := n * fact(n-1), yielding the following function definition.

int fact(int n) {
if ( n < 2 ) // check when to terminate recursion
return 1;
return n * fact(n – 1); // recursive call
}

In this exercise we calculate GCD (Greatest Common Divisor) using Euclid’s algorithm, which uses the property that if p > q then the GCD of p and q is the same as the GCD of p – q and q. If p < q, then the GCD is the same as the GCD of p and q – p, and if p = q, the GCD is p. (the rest of the Quiz problem is hidden)

## Tips for Quiz 4

One limitation of arrays is that their sizes are fixed --- their sizes must be specified in the code. One way to overcome this limitation is to use what is called

*dynamic memory allocation*. The following code shows how to create a variable-sized array and use it. Once memory is allocated to p (the 4th line), p can be used just like an array. The syntax of this line might not be familiar to you, but don’t worry, you won’t have to change anything (so just memorize the lines 2 ~ 4).

main () {
int size;
cin >> size;
int * p = new int [size]; // allocate an array of integers for p
for ( int i = 0; i < size; i++ )
p[i] = 0;
delete [ ] p; // deallocate p because p is no longer needed
return 0;
}

## Tips for Quiz 3

- Loop
- Nested loop
- Output formatting using iomanip
- String
- Escape sequence
- Above all, test against many cases you can think of, before submission.

--

JongeunLee - 2011-10-19