exponents?

Pages: 12
I'm making a program so that the user enters the base and then an exponent and then it displays the result. My code so far is

1
2
3
4
5
6
7
float Base;
float Exponent;
std::cout<<"Enter The Base: ";
std::cin>>Base;
std::cout<<"Enter The Exponent: ";
std::cin>>Exponent;
float Result (

but then I got stuck because I was not sure how I could make it calculate the result. Sorry if that was not clear
make a for loop that loops Exponent times and on every loop multiply Result by Base. Result should be set to 1, at the beginning.
your wording confused me can you show me what you mean?
float Result = 1
loop from 0 to Exponent{
multiply Result by Base
}
sorry I'm not very good at loops so could someone show me what that would look like?
I started it but then i got confused this is what I had
1
2
float Result (1);
for (Result;???;???)

but then I didn't know how to go any further
I'm not going to show you the code but I'll give you a more verbose version of what he meant.
Set result to 1. Start with a for loop, counter at 0. As long as the counter is less than (NOT <=) the exponent, multiply result by base and set result to the new value.
result *= base;
If you can't figure it out from there you need to give this a good read:
http://www.cplusplus.com/doc/tutorial/control/
I understood all that except for the part when you said counter at 0. what did u mean by that

edit: Could someone just give me the code so that I could know how to do that in the future. It would be more beneficial to see how it's done than randomly guessing with the code

edit2: Ive messed with it more but can someone tell me why this doesn't work?

 
for (Result;0<Exponent;Result*=Base);}
Last edited on
It's a for loop. Did you read the thing I directed you to? That's an extraordinary weird for loop.
And no, nobody's just *giving* you the code, that's not how it works. You get help, not the answer. Otherwise how would you learn? You'd just come back with a related question later.
actually yea I read it 3 times. Secondly I couldn't make heads or tails with what everyones been telling me so ive been randomly fucking with the code and I know it was strange but it actually compiled so I thought I might have been onto something. Thirdly I would actually understand how to approach something like this next time if I saw some code related to this
OK then, do you know how a for loop works? I'll walk it through with you:
1
2
3
4
for (initialization ; condition ; update)
{
    looped code
}

For loops are traditionally used to run something for a determinate number of executions. Therefore, the loop's syntax is designed to accommodate a counter.
The initialization is ran only at the first execution of the for loop, before any looped code is ran. It is usually used to set the counter for the loop to a desired starting value - in many cases, 0 or 1.
The condition is the factor that determines if the loop will keep running. It is checked at the beginning of each iteration of the loop, including the first (immediately after the initialization, I believe). If the condition is ever false, the loop will stop before executing its code. The condition is usually bound to the value of the counter in some way.
The update is used to modify the counter after each execution of the loop. After the code in the loop is executed, the update's code is then performed. This particular statement is usually what makes the counter's value change over executions of the loop, eventually causing the loop's condition to become false. In the vast majority of cases, at least to my experience, the update code consists of incrementing the counter by 1.
Therefore, your counter would be some arbitrary new variable, set at 0 in the initialization. As long as the counter is LESS THAN the exponent value inputted by the user (the condition, catching on?), you will, in the code of the loop, multiply the result by the base. (Outside the loop, you'd set the result to 1.) Finally, you would increase your counter variable by 1. You will execute this loop "exponent" times (as many times as the value of the exponent).
See?
(By the way, if this charade is tiring you, I'm NEVER EVER going to post you an immediate answer. I'll tutorial you this far, maybe even farther if you are civil and clear, but never ever will I give you the answer. That's just how I roll. http://xkcd.com/524/)
Last edited on
firstly I already read that firedraco
secondly tummychow I already knew 90% of that stuff but for some reason I got so confused by what you guys were saying I forgot to put it in the correct syntax. But the bottem did help me so thanks u i will edit this post when i finish testing some things
this gives me the correct results but I wanna make sure this was the way I was supposed to do it
1
2
for (float Counter=0;Counter<Exponent;Counter++){
	Result=Result*Base;}
Is that the correct way?
Thanks for all the help even if it wasn't exactly clear
You could condense the second line to
result *= base;
Also, you'd be better off making the counter int. Floats could have some funny floating point arithmetic issues and are not as reliable.
However, that code is completely correct. It should work fine.
ok thanks, I thought the only difference between int and float was that float could accept decimals. Thanks for all the help

edit: now that I think about it why did I make counter a float lol it would always be a whole # so why lol
Last edited on
By the way, float is generally not accurate enough for floating point arithmetic either. (Damn floats aren't good for anything.) They just don't have enough significant digits.
so what should I use besides float when I need something to hold a value with a decimal. Int can't do it can it?
In general, double is best. Long double is generally not advisable because it's just... a weird type. I recall reading something about it but I can't remember. In addition, some OSs don't have any difference between long double and double. Run this code to check:
1
2
3
4
5
6
7
8
#include <iostream>
int main ()
{
    std::cout << "The size of double is: " << sizeof (double) << endl;
    std::cout << "The size of long double is: " << sizeof (long double) << endl;

    return 0;
} 
that won't compile because you forgot to add the std:: to endl XD and the fact that it instantly closes isn't good either you should probably of added std::cin.ignore(); to that lol
edit: and the output on both says 8
Last edited on
Depends on the OS and the compiler. (I did miss the endl. I usually program with a using directive. Don't know why I forgot it. Also, in VC++, if you execute code in the IDE, it won't autoclose. don't know why that is either, but that's why I left out a get().) In your case, double would work best in pretty much all cases. On some cases, long double is ten bytes.
Pages: 12