Quiz | Posted (Thursdays) |
Given in class (Thursdays) |
||
---|---|---|---|---|
3 | Oct | 10 | Oct |
You will fare better on the quiz if you try working the problems before looking at the solutions. If you don't understand the question or its answer, please get help.
Directions: In Lab 4, you wrote recursive procedures to implement several arithmetic functions. In these problems, you will implement similar functions using iteration (loops). For each of the following specifications,
expt
with the following specification. Use repeated multiplication.
Do not use the built-in exponentiation method.
(Yes, this was done in class. Try to do it without looking at
your notes.)
PARAMETERS: integers n and k, with k >= 0 RETURN VALUE: the value of n to the power k EXAMPLES: expt(3,2) is 9 expt(5,0) is 1 expt(2,5) is 32
harmonicSum
with the following specification.
PARAMETERS: a positive integer, n RETURN VALUE: the sum 1 + 1/2 + 1/3 + ... + 1/(n-1) + 1/n
geometricSum
with the following specification.
PARAMETERS: a non-negative integer, k RETURN VALUE: the sum 1 + 1/2 + 1/4 + 1/8 + ... + 1/(2^k)
mult
with the following specification. Use repeated addition.
Do not use the multiplication operator.
PARAMETERS: integers j and k RETURN VALUE: the product j*k
sumDownBy2
with the following specification.
PARAMETERS: a positive integer n RETURN VALUE: the sum of the positive integers n + (n-2) + (n-4) + ... EXAMPLES: sumDownBy2(7) is 7+5+3+1 = 16 sumDownBy2(8) is 8+6+4+2 = 20
sumOdd1toN
with the following specification.
PARAMETERS: a positive integer n RETURN VALUE: the sum of all the odd integers between 1 and n, inclusive EXAMPLES: sumOdd1toN(7) is 1+3+5+7 = 16 sumOdd1toN(8) is 1+3+5+7 = 16 sumOdd1toN(1) is 1
lcm
with the following specification.
PARAMETERS: positive integers j and k RETURN VALUE: the least common multiple (LCM) of j and k EXAMPLES: lcm(3,5) is 15 lcm(6,8) is 24