# COP 3223H Program 5: Functions – Leap Years

\$30.00

## Problem A: Sum of Leap Years (sumleapfunc.c)

Given a starting year and an ending year, determine the sum of all of the leap years that fall
within those boundaries. This problem was given for homework #3 and students had to write it
without functions.

If you weren’t able to solve it, please consult the solution that is posted. For
this problem, your job is to convert that solution (or your own solution), to one that uses the
following three functions:

// Returns the smallest value x such that x >= start and x is
// a multiple of n.
int getNextMultiple(int start, int n);
// Returns the largest value x such that x <= end and x is // a multiple of n. int getPrevMultiple(int end, int n); // Returns the sum of the arithmetic series that starts at // start, ends at end and has a common difference of skip. // A pre-condition is that end >= start and (end-start) is
// divisible by skip.
int sumArithSeries(int start, int end, int skip);

Most of the credit on this problem will be given to implementing these functions as the
comments indicate how they should be implemented.
After writing and testing these three functions, rewrite your main function to solve the problem
by making the appropriate calls to these functions.

Input Specification
Both the starting year, s, and the ending year, e, will be in between 1600 and 10,000, inclusive.
In addition, s ≤ e. (So, in full, the restrictions are: 1600 ≤ s ≤ e ≤ 10000.)

Output Specification
Output a single line of the format:
The sum of the leap years in between S and E is C.

where S and E are the starting and ending years inputted by the user and C is the result of the
query.
Please see Homework #3 for Sample Program Runs.

## Problem B: Adding to the Math Library (mymathfuncs.py)

Please take the posted program, mymathfuncs.py, and add functions to make the following
calculations:
// Pre-condition: .000000001 < x < 1,000,000,000 // Post-condition: Returns the natural logarithm of x. log(x) // Pre-condition: x > 0
// Post-condition: Returns the square root of x.
sqrt(x)

To solve both problems, please use the binary search technique described below:
Both the log function and square root function are increasing functions, which means that if x <
y, then f(x) < f(y), over the whole domain. For example, if we want to know log(10), and we know that e3 > 10, then we can conclude that log(10) < 3. (Recall, that log of 10 is simply the
value we must raise e to, in order to obtain 10.)

Using this idea, here is the formalized version of the binary search:
1. Come up with initial values, low and high, such that low is below the desired result and high is
above the desired result.
2. Calculate mid = (low+high)/2 and then calculate f(mid).

3. If f(mid) is too big, then we can reset high = mid. If f(mid) is too small, then we can reset low
= mid.
For log, if x == 1, we return 0, if x < 1, we can set x = 1/x and then negate the answer our binary search returns. In the binary search, if x > 1, then we can set low = 0, high = 25, since I am
limiting the input to be in between 10-9
and 109
and e25 > 109
.

(Alternatively, you can take e and
square it multiple times until you get a value bigger than the number you are taking the log of
and account for the appropriate exponent to e.)

For square root, if x < 1, we set low = x and high = 1. If x > 1, we set low = 1 and high = x.
Adjust the main function to allow the user to test either of these functions.

## Problem C: Guessing Game with Functions (guessfunc.c)

In a class example, we showed a program that allows us to run a guessing game, where the
computer generates a secret number from 1-100 and the user makes successive guesses, with the
computer telling the user each time if their guess was too low or too high. Write a new program
that writes a function that implements the guessing game and uses this function.

Here is the
function prototype and description:
// Performs a guessing game where the secret number is in
// between low and high, inclusive, and returns the number of
// turns it took the user to get the correct number.
int guessgame(int low, int high);

Your main program should ask the user how many times they want to play the guessing game,
and what they want the low and high bounds of the guessing game to be.

Then, your program should allow the user to play the guessing game the appropriate number of
times, keeping track of how many guesses the user took each time to get the correct number. At
the end of the last game, your program should print out the average number of guesses the user
took to get the correct number.

No sample run is given so that you have a little leeway in your implementation. You will be
come from whether or not you’ve implemented the function described above to specification.

#### Deliverables

Three source files:
1) sumleapfunc.c for your solution to problem A
2) mymathfuncs.py for your solution to problem B
3) guessfunc.c for your solution to problem C
All files are to be submitted over WebCourses.

Restrictions
Although you may use other compilers and coding environments, your programs must run in
IDLE for the Python programs and Code::Blocks for the C/C++ programs.