Recursive Power Function

advertisement
Power Function (Exponentiation), Recursively
The goal for this exercise is to practice writing (increasingly complex) recursive
code.
For this exercise, you need to implement the PowR method in the provided
RecursiveMethods class.
This class should be found in the Student_Answers.cs file in a project
named something like 03_PCE_StudentCode. The method should take two
integer parameters named B and E, and will return the value of BE. (for example,
PowR(2,3) = 8, PowR(3,2) = 9, etc) You're not allowed to simply call the built-in
Math.Pow method, and return the value, though – you need to find a way, by
adjusting the values that you pass to the successive recursive calls, to get this to
happen. You only need to make this work for non-negative, whole numbers.
Make sure that your code does the right thing for positive numbers, zero,
and negative numbers for both the base and the exponent. If a particular value
(such as a negative exponent) doesn’t make sense (or is outside the scope of
this exercise), your PowR method should catch that error, and then return 0.
Once you’ve done this, make sure that all the tests in the
NUnit_Tests_Recursive_Power class pass.
What you need to do for this exercise:
1. Implement PowR, as described above
2. Once you’ve completed the above task, you should run the tests in the
NUnit_Tests_Recursive_Power class. They should all pass at this point, and if
not, then you should fix your program so that those tests do pass. You can
run the tests using the NUnit support in the provided starter project. You
should feel free to supplement that code with your own test cases if you wish,
but you are neither required nor expected to.
Download