469,646 Members | 1,193 Online

# Help with exponents loop, not allowed to use pow() library

so i'm trying to set up this exponents loop, keep in mind this is my first year in computer science so my knowledge of script is somewhat minimal. basically this assignment (or at least part of it) tells us to have the user enter 2 numbers, (left and right) and to output the correct answer using a loop to calculate. my idea was to use to write a script so the number the user inputs for the right side will determine the number of times the loop runs. this is what i have so far but it's not working. can anyone help please?
Expand|Select|Wrap|Line Numbers
1. #include <iostream>
2.
3. using namespace std;
4.
5. int main()
6. {
7.     int left, right;
8.
9.     cout << "Enter left and right side: ";
10.     cin >> left >> right;
11.
12.
13.
14.     for (right;;)
15.
16.     left *= right;
17.
18.
19.
20.
21.
22.
23.     system("pause");
24.     return 0;
25. }
Oct 21 '07 #1
12 12534
sicarie
4,677 Expert Mod 4TB

What is the logic you are trying to implement? How would you accomplish this task if you were given a piece of paper and the numbers? Each of those steps is your algorithm, which you need to figure out before you start coding. I would recommend reading in the number and the exponent, and then using the exponent as the test limit in your for loop.
Oct 21 '07 #2
i'm not sure if i understand you completely, as i've been only doing this for a few months, but i'll try express myself the best i can.

basically what i'm trying to do is have the user enter in two numbers, (left and right) and then calculating it like this:
left ^ right
but i can't use pow()
i'm not too sure what you meant by "reading in" though
i'm still at a loss for this assignment really, no idea what to do
do you think im on the right track?

#include <iostream>

using namespace std;

int main()
{

cout << "Enter left and right side: ";
cin >> left >> right;

do
{
left *= right;
} while (count = right);

system("pause");
return 0;
}
Oct 21 '07 #3
sicarie
4,677 Expert Mod 4TB
Okay. Close your compiler/IDE. Leave it closed.

Take a pen (or pencil) and a sheet of paper. Now, without using code, write down the steps that anyone who did not know how to calculate an exponent would be able to pick up, read, and follow. For example, you would start with the basics:

get base
get exponent

from there, where would you go? What do you do to calculate a power? What steps do you take (when you're not allowed to use a calculator)? You need to figure this out, and look at how the real-world behaves before you can conceptualize it enough to abstract it to a computer.
Oct 21 '07 #4
oler1s
671 Expert 512MB
Have you thought about writing down on paper the logic? Look at what you are saying right now. Left is multiplied by right. Actually, you know what, learn to use reasonable mathematical terms. Call left "base" and "right" power instead.

Your loop involves multiplying base and power. Here's an example for you 3^2. What's that? 3 * 3. 2^4 = 2*2*2*2. You see the base and power directly multiplying. Try writing your logic down in plain english. Once you think you have it right, show your logic, in plain english to us.
Oct 21 '07 #5
so if this was done on a paper, i would input the numbers

x ^ y

let's say x=5, and y=6

so 5^6

5*5*5*5*5*5=15625

very basic, i do completely understand that concept (as though it may not seem that way)

so what i'm trying to do with my code, in plain english:

1. ask user to input a number for the base, and a number for the exponent
2. calculate that by using a loop (and with no use of pow() )
Oct 21 '07 #6
sicarie
4,677 Expert Mod 4TB
so what i'm trying to do with my code, in plain english:
...
2. calculate that by using a loop with no use of pow()
....
Not so much in plain English because I don't (for this thread) know how to calculate the power. How do you do that? I'll give you a hint, you do the same thing a certain number of times. What do you do, and how many times do you do it?

FYI - You are still concentrating on the code instead of the issue - you're still talking about the pow() function. You're not supposed to use it, so forget about it. Concentrate on the numbers, and what they are doing. Look for patterns.
Oct 21 '07 #7
Not so much in plain English because I don't (for this thread) know how to calculate the power. How do you do that? I'll give you a hint, you do the same thing a certain number of times. What do you do, and how many times do you do it?
i suppose you would take the base value, multiply that by the base value, and then multiply the same number of times there is an exponent

so for 5^6

(5*5)

and multiply that by 5, 5 more times over (since the first time counts as once)
Oct 21 '07 #8
sicarie
4,677 Expert Mod 4TB
for 5^6

(5*5)

and multiply that by 5, 5 more times over
That looks very close to a control structure used in programming, doesn't it? ;) From there it's just the technical details on where the base and exponent go for the checks each time.
Oct 21 '07 #9
That looks very close to a control structure used in programming, doesn't it? ;) From there it's just the technical details on where the base and exponent go for the checks each time.
yes, it certainly does. the only problem is that i'm not too sure how to repeat a loop for a set number of times. i'll try figure it out by going through my textbook though.
Oct 21 '07 #10
so i got this code, and it works like a charm!

int result = 1;

for (; exp--; )
result *= base;
Oct 21 '07 #11
JosAH
11,448 Expert 8TB
so i got this code, and it works like a charm!

int result = 1;

for (; exp--; )
result *= base;
Well done; if you want funny loops like the one above you can also do:

Expand|Select|Wrap|Line Numbers
1. int result= 1;
2. for (;exp--; result*= base);
3.
or:

Expand|Select|Wrap|Line Numbers
1. int result= 1;
2. while(exp--)
3.    result*= base;
4.
They all do the same thing.

kind regards,

Jos
Oct 21 '07 #12
sicarie
4,677 Expert Mod 4TB
so i got this code, and it works like a charm!

int result = 1;

for (; exp--; )
result *= base;
I was thinking more along the lines of:

Expand|Select|Wrap|Line Numbers
1.
2. // this is pseudocode, BTW, this won't compile
3. declare base, exponent, result
4.
5. for i = 0; i < exponent; i++
6.     result *= base
7.
Of course, that would involve figuring out how to initialize result, and how that effects the loop, but you get the idea. Or you can do like Jos said and use the more odd-looking loops...
Oct 22 '07 #13