Wyo C++ Name
-
Ch. 18 Worksheet #1
Write out the following functions on lined paper.
1.
int sumEvens(int num)
{
// precondition: num is an even integer where
2 <= num <= INT_MAX
// postcondition: sumEvens returns the sum of
all even,
// positive
integers less than and equal to num
2. Write an iterative function (i.e. one that is not recursive and that uses a loop) named sumEvens that follows the given preconditions and postconditions. Note that all recursive functions can be written as iterative functions. We often use recursive functions when it is easier to understand and solve a given task recursively rather than iteratively. We must keep in mind though that execution speed and memory usage must be optimized with whichever solution is chosen.
int sumEvens(int num)
{
// precondition: num is an even integer where
2 <= num <= INT_MAX
// postcondition: sumEvens returns the sum of
all even,
// positive
integers less than and equal to num
3. Write a recursive function named balance that returns the final balance of a savings account with monthly compounded interest, where the initial deposit (deposit), the annual interest rate (rate), and the number of months (months) are passed as parameters.
double balance(int deposit, double
rate, int months)
{
// precondition: deposit and months are positive,
even integers
// rate
is a double where 0 < rate < 100
// postcondition: balance returns a double that
is represents
// the deposit plus monthly
compounded interest at an interest
// rate of
rate after months months