### user created power function

This is my assignment, please look at my program and give me any tips? (I'm not quite sure I did it right).

Write a prototype and function for calculating powers of numbers.
This version should have an integer return type and take two integer inputs, for the base and the exponent. It should do the following:
• handle negative bases
• return an error on negative exponents (we’re not doing those yet)
• handle zero exponents
Call your function from a nested loop in main(), which calls the function using every combination of negative, zero and positive numbers for both base and exponent. This will test your function without the user having to input all the data.
Any code which calls the library pow() function will lose half of the assignment points.

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374`` ``````#include using namespace std; int power (int, int); int main() { int size = 10; int test[size]; int base; for (base = -2; base < 4; base += 2) { for (int expon = -2; expon < 4; expon += 2) { cout << base<< " " << expon << " " << power(base,expon) << endl; } } cout << endl; return 0; } int power (int num, int raisedTo) { int product; if (raisedTo < 0) { cout << "Please enter a non negative exponent. " << endl; } else if (raisedTo == 0) { product = 1; return product; } while (num > 0) { for (num = num; num <= raisedTo; num++) { product = num * num; return product; } } while (num <= -1) { if(raisedTo % 2 == 0) { for (num = num; num <= raisedTo; num++) { product = num * num; return product; } } else for (num = num; num <= raisedTo; num++) { product = num * num * -1; return product; } } } ``````

Last edited on
Topic archived. No new replies allowed.