We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,688 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Help with Exponentiation

I want to write this function integerPower(base, exponent) that return the value of base^exponent For instance integerPower(3, 4) = 3*3*3*3

The function has two parameters my question is how can I use the for loop to control the calculation? It's not that hard to use the for loop when the function has one parameter, but with two parameters I little bit confused by that.

3
Contributors
3
Replies
15 Hours
Discussion Span
1 Year Ago
Last Updated
4
Views
Question
Answered
arold10
Light Poster
31 posts since Nov 2010
Reputation Points: 24
Solved Threads: 0
Skill Endorsements: 0

I want to write this function integerPower(base, exponent) that return the value of base^exponent For instance integerPower(3, 4) = 3*3*3*3

The function has two parameters my question is how can I use the for loop to control the calculation? It's not that hard to use the for loop when the function has one parameter, but with two parameters I little bit confused by that.

I suggest that you approach this as a multi-step process.

1) Write an "empty" function definition that shows the type the function returns and the types of each of the parameters. This might be a good time to think about what values of exponent you are going to accept. Only positive integers? All integers? Or all reals?
2) Then add any local variables that you know will need to write the function.
3) Then add the for statement you will need to calculate the integerPower(). For statements are used to define loops that execute 0 or more times and are natural if you are going to count the number of loops. (Yes, they do not have to just be used for that but that is where they are a natural fit.) Which of your parameters represents the count of the number of times through the loop? (If you were implementing your example with paper and pencil, how would you do it?)
4) What are you going to return from the function?
5) What other local variables do you need to implement the "stuff" you did in steps 3 and 4?

If you do steps 1) and 2) and make an attempt at step 3), we (including me) can help you by giving comments or corrections on your attempt. But this will show you are making an effort. Most forums avoid giving out fully coded examples so that the person asking is actually going to learn something.

pheininger
Light Poster
29 posts since Jul 2010
Reputation Points: 15
Solved Threads: 5
Skill Endorsements: 0

I don't seem to understand the difficulty. Can you do the task without using a function? If so, why would a function cause a problem?

WaltP
Posting Sage w/ dash of thyme
Team Colleague
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37

Thanks guys, I solved the problem.

I am writing a function with floor just like this, y = floor(x + .5); I did it in dev c++ everything seemed to work fine. But, when I did in CodeBlock, it states that floor must be declared.

I am rounding x and assigning it to y.

Sorry, I realized what was wrong. I forgot to include the #include <math.h> header.

arold10
Light Poster
31 posts since Nov 2010
Reputation Points: 24
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by WaltP and pheininger

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0746 seconds using 2.66MB