454,605 Members | 1,391 Online
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

# Making an exponent project in C++

 P: 29 im stuck on what to put in my for loop please help me ive tried everything it feels like //Computer Science //Period 7 //Bonus #include #include int main() { float x; float y; int e; cout << "What is the base?" << "\n"; cin >> x; cout << "What is the exponent?" << "\n"; cin >> e; if (e==o) {cout << "Answer is: 1" << "\n";} else { for (int i=1; i<=e; i++) { cout << "Answer is:" << y << "\n"; } } system("PAUSE"); return 0; } Nov 9 '06 #1
58 Replies

 Expert Mod 2.5K+ P: 4,677 im stuck on what to put in my for loop please help me ive tried everything it feels like Well, can you tell me in a sentence or two, what a power is, mathematically? Nov 9 '06 #2

 P: 29 yes...2^3 would be 2*2*2 is that what youre asking? Nov 9 '06 #3

 Expert Mod 2.5K+ P: 4,677 yes...2^3 would be 2*2*2 is that what youre asking? And you just answered your own post. Good job (I knew you could do it)! Nov 9 '06 #4

 P: 29 i know what it means im just not sure how to code that. i cant make the loop multiply the base by itself Nov 9 '06 #5

 Expert Mod 2.5K+ P: 4,677 i know what it means im just not sure how to code that. i cant make the loop multiply the base by itself Read over your initialization in the 'for' loop, and think about that. Nov 9 '06 #6

 P: 29 i just learned how to use for loops like 2 weeks ago and havent done a program like this before so im not understanding what your telling me Nov 9 '06 #7

 Expert Mod 2.5K+ P: 4,677 How many times do you want to multiply the base by itself (in either terms, your program or the structure of an exponent)? Nov 9 '06 #8

 P: 29 whatever e is Nov 9 '06 #9

 Expert Mod 2.5K+ P: 4,677 Bingo! You are seriously right there! Ok, final click - what are you going to save the answer in? Nov 9 '06 #10

 P: 29 y.....so i would put y=x*e?? Nov 9 '06 #11

 Expert Mod 2.5K+ P: 4,677 Ok, you're right when you say 'y', but think of this in a for loop. How many times does your 'for' loop execute? Nov 9 '06 #12

 P: 29 i honestly have no idea Nov 9 '06 #13

 Expert Mod 2.5K+ P: 4,677 Ok, that's ok. You said you jsut learned for loops, right? What is the structure of a for loop? for (a ; b; c) { d } What goes in each part (a, b, c and d)? Nov 9 '06 #14

 P: 29 for (int i=1; i<=e; i++) { y } Nov 9 '06 #15

 Expert Mod 2.5K+ P: 4,677 Sorry, my fault. What goes in a for loop logically? I'll start you out; A) the initialization of the temporary counter variable that will be manipulated each time the loop is executed. Nov 9 '06 #16

 P: 29 so am i putting an equation in my for loop? or doea the equation go outside the loop? Nov 9 '06 #17

 Expert Mod 2.5K+ P: 4,677 so am i putting an equation in my for loop? or doea the equation go outside the loop? You are going to put an equation inside your for loop. Do you know what functions the parts b, c, and d perform? Nov 9 '06 #18

 P: 29 c increments it and d is what is actually going to be reapeted but im not sure what b does Nov 9 '06 #19

 Expert Mod 2.5K+ P: 4,677 c increments it and d is what is actually going to be reapeted but im not sure what b does Good. One last question before we hit what b does. How many times will your loop execute? Nov 9 '06 #20

 P: 29 whatever e is Nov 9 '06 #21

 Expert Mod 2.5K+ P: 4,677 whatever e is Right. And you just answered what b is. So your loop will start out at i, increment by 1 each time, however many times e is (because of the conditional <= which means 'less than or equal to'). Now, can you tell me one more time how many times you multiply a base by itself? Nov 9 '06 #22

 P: 29 whatever the exponent is is how many times you multiply the base Nov 9 '06 #23

 Expert Mod 2.5K+ P: 4,677 whatever the exponent is is how many times you multiply the base And what is e? Nov 9 '06 #24

 P: 29 umm i????? Nov 9 '06 #25

 Expert Mod 2.5K+ P: 4,677 No, what does e stand for in your program? Nov 9 '06 #26

 P: 29 exponent entered by user Nov 9 '06 #27

 Expert Mod 2.5K+ P: 4,677 So you have a loop for (i = 1; i <= e; i++) that is going to count up to however many times until it is equal to the exponent. And you have your base, which is what you want to multiply by. Are you missing anything else? Nov 9 '06 #28

 P: 29 yes, telling the computer to multiply the bases until its equal to the exponent Nov 9 '06 #29

 Expert Mod 2.5K+ P: 4,677 yes, telling the computer to multiply the bases until its equal to the exponent Ha, sorry - my fault again - bad question. For the general problem of a base and exponent, what is still missing? Nov 9 '06 #30

 P: 29 the answer? Nov 9 '06 #31

 Expert Mod 2.5K+ P: 4,677 the answer? Exactly! Now, what is something in your program that we haven't used? Nov 9 '06 #32

 P: 29 the base, x Nov 9 '06 #33

 Expert Mod 2.5K+ P: 4,677 the base, x But we did - we are going to use the base, as many times as the exponent for the answer. There's something else in your code that we haven't talked about, but you're REALLY close with 'x'. Nov 9 '06 #34

 P: 29 the answer, y Nov 9 '06 #35

 Expert Mod 2.5K+ P: 4,677 the answer, y Right. So we are going to take the base, as many times as we have an exponent, multiply it by the answer. Basic math, right? Nov 9 '06 #36

 Expert Mod 2.5K+ P: 4,677 Sorry, hand slipped, posted before I wanted to. With computer algorithms, the syntax is just a little different for as many times as the exponent, we are going to multiply the answer times the base Now can you see the for loop, and what is supposed to be inside it? Nov 9 '06 #37

 P: 29 right x=e y=y*x? Nov 9 '06 #38

 P: 29 no, im sorry but im having the hardest time applying this Nov 9 '06 #39

 Expert Mod 2.5K+ P: 4,677 right x=e y=y*x? You got it. Nov 9 '06 #40

 Expert Mod 2.5K+ P: 4,677 But you just answered it. And came up with it all yourself. Is this better? for as many times as the exponent, multiply the answer times the base Nov 9 '06 #41

 P: 29 really?? i ran it and it gave a crazy number and e-41 Nov 9 '06 #42

 Expert Mod 2.5K+ P: 4,677 really?? i ran it and it gave a crazy number and e-41 Yeah, i tried to run it just now and my compiler is yelling about using c++ and .h in the includes. Hang on a sec Nov 9 '06 #43

 Expert Mod 2.5K+ P: 4,677 Yeah, i tried to run it just now and my compiler is yelling about using c++ and .h in the includes. Hang on a sec It's the initializations. In math, anything multiplied by anything is 0, so you just have to assign x and y the values of 1.0, and e 1, or else none of the math will work. Nov 9 '06 #44

 P: 29 i did 3^2 and it gave the the answer 4 Nov 9 '06 #45

 Expert Mod 2.5K+ P: 4,677 Can you re-post the code? I did have to make a few modifications to get yours running with my compiler... Nov 9 '06 #46

 P: 29 int main() { float x=1.0; float y=1.0; int e=1; cout << "What is the base?" << "\n"; cin >> x; cout << "What is the exponent?" << "\n"; cin >> e; for (int i=1; i<=e; i++) { x=e; y=y*x; } cout << "Answer is:" << y << "\n"; Nov 9 '06 #47

 Expert Mod 2.5K+ P: 4,677 Once again, my bad. But an integral part of programming (debugging!). Can you tell me what is happening inside your for loop? Not the "this will increment..." but just give a short explanation of both lines: this line modifies.... Nov 9 '06 #48

 P: 29 first part says the part to keep looping, and the second part tells it how to loop it Nov 9 '06 #49

 P: 29 makes x and e equal, multiplies it all Nov 9 '06 #50

58 Replies