Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Use Dave Anderson's as a basis, but also consider the special cases of where:</p> <pre><code>1) x is 0 2) e is negative. </code></pre> <p>Again, no code so you can try to figure it out yourself :-)</p> <p><strong>Update:</strong> Make sure you create a number of test cases, and you make sure everyone works as you think it should. Once the tests pass, you'll know that your recursive power function is working correctly.</p> <p><strong>Example:</strong> Having had time to figure it out yourself, I though I'd present a solution, with tests:</p> <pre><code>int main(void) { // n = 0 special case test(0, 0, 1); test(4, 0, 1); test(-5, 0, 1); // x = 0 special case test(0, 0, 1); test(0, 2, 0); // normal use test(4, 1, 4); test(4, -1, 0.25); test(-4, 3, -64); test(8, 2, 64); test(2, 3, 8); test(2, -3, 0.125); test(2, -5, 0.03125); // Invalid input tests std::cout &lt;&lt; std::endl &lt;&lt; "Invalid input tests" &lt;&lt; std::endl; test (0, -2, NULL); test(0, -4, NULL); // Negative Tests std::cout &lt;&lt; std::endl &lt;&lt; "Negative tests (expect failure)" &lt;&lt; std::endl; test(4, 0, 4); test(2, 1, 1); test(2, -5, 0.0313); return 0; } double power(int x, int n) { // check for invalid input if (n == 0) { return 1; } if (n &gt; 0) { return x * power(x, n - 1); } else if (n &lt; 0) { return 1 / (x * power(x, -n - 1)); } } bool test(int x, int n, double expected) { if (x == 0 &amp;&amp; n &lt; 0) { std::cout &lt;&lt; "Testing " &lt;&lt; x &lt;&lt; "^" &lt;&lt; n &lt;&lt; ", result = 'Invalid input'." &lt;&lt; std::endl; return false; } double result = power(x, n); std::cout &lt;&lt; "Testing " &lt;&lt; x &lt;&lt; "^" &lt;&lt; n &lt;&lt; ", result = " &lt;&lt; result &lt;&lt; ". Expected " &lt;&lt; expected &lt;&lt; " - test " &lt;&lt; ((result == expected) ? "PASSED" : "FAILED") &lt;&lt; std::endl; return true; } </code></pre> <p><strong>Output:</strong></p> <pre><code>Testing 0^0, result = 1. Expected 1 - test PASSED Testing 4^0, result = 1. Expected 1 - test PASSED Testing -5^0, result = 1. Expected 1 - test PASSED Testing 0^0, result = 1. Expected 1 - test PASSED Testing 0^2, result = 0. Expected 0 - test PASSED Testing 4^1, result = 4. Expected 4 - test PASSED Testing 4^-1, result = 0.25. Expected 0.25 - test PASSED Testing -4^3, result = -64. Expected -64 - test PASSED Testing 8^2, result = 64. Expected 64 - test PASSED Testing 2^3, result = 8. Expected 8 - test PASSED Testing 2^-3, result = 0.125. Expected 0.125 - test PASSED Testing 2^-5, result = 0.03125. Expected 0.03125 - test PASSED Invalid input tests Testing 0^-2, result = 'Invalid input'. Testing 0^-4, result = 'Invalid input'. Negative tests (expect failure) Testing 4^0, result = 1. Expected 4 - test FAILED Testing 2^1, result = 2. Expected 1 - test FAILED Testing 2^-5, result = 0.03125. Expected 0.0313 - test FAILED </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload