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 <iostream.h>
#include <stdlib.h>
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;
}
 
Share this Question
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?
 
P: 29

yes...2^3 would be 2*2*2 is that what youre asking?
  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)!
 
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
  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.
 
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
  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)?
  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?
 
P: 29

y.....so i would put y=x*e??
  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?
  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)?
 
P: 29

for (int i=1; i<=e; i++)
{
y
}
  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.
 
P: 29

so am i putting an equation in my for loop? or doea the equation go outside the loop?
  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?
 
P: 29

c increments it and d is what is actually going to be reapeted but im not sure what b does
  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?
  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?
 
P: 29

whatever the exponent is is how many times you multiply the base
  Expert Mod 2.5K+
P: 4,677

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

No, what does e stand for in your program?
  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?
 
P: 29

yes, telling the computer to multiply the bases until its equal to the exponent
  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?
  Expert Mod 2.5K+
P: 4,677

the answer?
Exactly!
Now, what is something in your program that we haven't used?
  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'.
  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?
  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?
 
P: 29

no, im sorry but im having the hardest time applying this
  Expert Mod 2.5K+
P: 4,677

right
x=e
y=y*x?
You got it.
  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
 
P: 29

really?? i ran it and it gave a crazy number and e41
  Expert Mod 2.5K+
P: 4,677

really?? i ran it and it gave a crazy number and e41
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
  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.
 
P: 29

i did 3^2 and it gave the the answer 4
  Expert Mod 2.5K+
P: 4,677

Can you repost the code? I did have to make a few modifications to get yours running with my compiler...
 
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";
  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....
 
P: 29

first part says the part to keep looping, and the second part tells it how to loop it
 
P: 29

makes x and e equal,
multiplies it all
    Question stats  viewed: 5104
 replies: 58
 date asked: Nov 9 '06
