CPPForSchool.com: User Defined Function Set 1: Assignment 4
4.
Raising a number to a power p is the same as multiplying n by
itself p times. Write a function called power that takes two arguments, a
double value for n and an int value for p, and return the result as
double value. Use default argument of 2 for p, so that if this argument
is omitted the number will be squared. Write the main function that gets
value from the user to test power function.
This assignment had me quite conflicted. I say this because I got so used to using the math.h library, especially for calculating exponents. Nevertheless, this was quite the learning process. Admittedly, I was able to create the proper loop and algorithm, but my variable declarations were off. Due to the miss-declarations, the resulting product was consistently wrong and way off base (no pun intended). Nevertheless, once I reviewed the sample code, I was able to modify my source and successfully outputted the expected results. Below are screenshots of my source and output along with the sample content for comparison.
This assignment had me quite conflicted. I say this because I got so used to using the math.h library, especially for calculating exponents. Nevertheless, this was quite the learning process. Admittedly, I was able to create the proper loop and algorithm, but my variable declarations were off. Due to the miss-declarations, the resulting product was consistently wrong and way off base (no pun intended). Nevertheless, once I reviewed the sample code, I was able to modify my source and successfully outputted the expected results. Below are screenshots of my source and output along with the sample content for comparison.
Comments
Post a Comment