Help me with a simple C++ program. calculating powers..

RSI

Diamond Member
May 22, 2000
7,281
1
0
Hi, just as I thought I had a grasp on for/while loops, my C++ teacher thoroughly confused me.
We can only use #include <iostream.h>, no stdio.h or anything like that. So sticking to cout instead of printf. But anyway, what the program needs to do is very basic. I thought I got it, but now I'm confused. We need to use some loops (for loops I guess). It just confuses me... blech. I should have paid more attention.

Anyway the output should look like this, according to the teacher :

What number do you want the powers of? 3
how many powers of 3 do you want? 4

The first 4 powers of 3 are:
3^0 = 1
3^1 = 3
3^2 = 9
3^3 = 27

that's really simple, but coding it I don't know :(

Thanks in advance for any enlightenment..

-RSI

 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
maybe you should post how you think you should do it, and where you're getting confused, then we can help you from there :)
 

DaiShan

Diamond Member
Jul 5, 2001
9,617
1
0
that is read, gopunk doesn't really want to type it all out (lazy bum!) jk yah give us where you are having problems, then it is less like plagiarism (sp) ;)
 

RSI

Diamond Member
May 22, 2000
7,281
1
0
I had a bunch of code written out at school, and i'm at home now :\

I forget the syntax for the loops :eek:

i know that C++ doesn't have built in powers. And obviously, number * number = number², but how do I tell it .. argh. Will I need .. two loops.. ? I think i need at least that. One of them has to be for the number and one for the powers, right? I don't know about the syntax for the whole loop at the moment (I can look it up or you can help out), but for the powers it has to be power=power+1 or something, right? I don't know, I just don't understand the loops well enough, or how to implement them in this program... Even though it's disgustingly simple :(

-RSI
 

RSI

Diamond Member
May 22, 2000
7,281
1
0


<< that is read, gopunk doesn't really want to type it all out (lazy bum!) jk yah give us where you are having problems, then it is less like plagiarism (sp) ;) >>

I'm not interested in plagiarising anything. You could just give me an example of a for loop that would help me, or some such drivel. :p

-RSI
 

yoda291

Diamond Member
Aug 11, 2001
5,079
0
0
I'm not sure I get what the problem is. you get the input base and number of powers. Then use the cycling value in the for loop to cout the result.

Edit:
completely out of a textbook

for (int i = 0; i< 20; i++)
{
do something 20 times
}
 

RSI

Diamond Member
May 22, 2000
7,281
1
0


<< I'm not sure I get what the problem is. you get the input base and number of powers. Then use the cycling value in the for loop to cout the result. >>

The problem is I'm a sleep-deprived moron with a headache and I don't get it. ;)

-RSI
 

DaiShan

Diamond Member
Jul 5, 2001
9,617
1
0
lemme break out the c++ book (it has been a while and I am forgetting syntax!) also you may want to post in the software and programming forums, they usually have a lot of programming junkies.
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
I forget the syntax for the loops :eek:

for loops:

for (i = 0; i < power; i++){
//junk
}

i know that C++ doesn't have built in powers. And obviously, number * number = number², but how do I tell it .. argh. Will I need .. two loops.. ? I think i need at least that. One of them has to be for the number and one for the powers, right?

no, i think you only need one loop, since the number is constant. if you look at the for loop i wrote above, that should do the trick. you basically want to think about what junk will get you the right answers. don't forget to figure out a way to get the 0th power to equal 1.

since i'm nice.... scroll down for spoiler....
























you basically want to set the answer to zero, then for every iteration of the loop, you multiply the current answer by the number. make sure it doesn't run through the loop when the power is zero, but only if it's 1 or greater.
 

RSI

Diamond Member
May 22, 2000
7,281
1
0
The number isn't constant. The user gets to choose the number as well as the power...

Thanks for the example, I'll see if I can do something with it.

-RSI
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0


<< The number isn't constant. The user gets to choose the number as well as the power... >>



no, i meant the number is constant for the loop, in that if you say 3^4, the number will always be 3 for that loop.
 

RSI

Diamond Member
May 22, 2000
7,281
1
0


<<

<< The number isn't constant. The user gets to choose the number as well as the power... >>



no, i meant the number is constant for the loop, in that if you say 3^4, the number will always be 3 for that loop.
>>

So don't you need two loops? Like, what if the user inputs 35 and wants 6 powers? Or the number 4 and 3 powers? Etc.. is that all handled within one loop? :confused:

-RSI
 

gopunk

Lifer
Jul 7, 2001
29,239
2
0
So don't you need two loops? Like, what if the user inputs 35 and wants 6 powers? Or the number 4 and 3 powers? Etc.. is that all handled within one loop? :confused:

i don't think you would need 2.

for the example you gave:

set number=35, power=6, answer=1.

set i=0
for power 1, answer = answer multiplied by number, increase i by one.
for power 2, answer = answer multipied by number, increase i by one...etc

set the loop so that i has to be less than power.
 

yoda291

Diamond Member
Aug 11, 2001
5,079
0
0
just cuz I feel bad for taking up anand's bandwidth, and since it's prolly due tomorrow and I can relate to bein a tired numbnut who can't think straight...

//declare your integer type variables
int base,num_of_powers, spotholder;

//use cin to get the user's preference for base and the number of powers.

cin >> base;
cin >> num_of_powers;

//then make a for loop as follows.

for (int i =0; i<num_of_powers;i++)
{
spotholder = base^i;
cout << spotholder << "\n";
}
return 0;




.....That should be about 90% of your code. Syntax and style forgiven.
 

Bloodybrain

Member
Oct 11, 1999
139
0
0
int main(){int a,b,i=0;int r=1;cin>>a;cin>>b;cout<<a<<"^"<<i<<"="<<r<<endl;for(i++;i<=b;i++){r=r*a;cout<<a<<"^"<<i<<"="<<r<<endl;}return 0;}
 

RSI

Diamond Member
May 22, 2000
7,281
1
0
Thanks for all the help. My messengers, music, and nagging parents are distracting me too much to be able to think about this, though. I have an 8:30 C++ class tomorrow morning, so I'll get that cleared up then hopefully. There's a test on thursday, so I should be ok by then. Thanks again

-RSI
 

yoda291

Diamond Member
Aug 11, 2001
5,079
0
0


<< int main(){int a,b,i=0;int r=1;cin>>a;cin>>b;cout<<a<<"^"<<i<<"="<<r<<endl;for(i++;i<=b;i++){r=r*a;cout<<a<<"^"<<i<<"="<<r<<endl;}return 0;} >>



I don't know you Bloodybrain, nor do I have anything against you personally, but just as forewarning. If I am ever in a situation where I have to sift through your code and/or debug it...I will promptly have you fired or drawn and quartered or something bad. :)
 

Gooberlx2

Lifer
May 4, 2001
15,381
6
91
Man.....I wish my program assignments were like this. My last assignment was writing a trac intepreter and tomorrow our next assignment gets handed out where we have to write our own compiler. Good stuff to know, but when the prof. does a sh!tty job of explaining it it causes many headaches and LATEnights.
 

jpsj82

Senior member
Oct 30, 2000
958
0
0


<<

<< int main(){int a,b,i=0;int r=1;cin>>a;cin>>b;cout<<a<<"^"<<i<<"="<<r<<endl;for(i++;i<=b;i++){r=r*a;cout<<a<<"^"<<i<<"="<<r<<endl;}return 0;} >>



I don't know you Bloodybrain, nor do I have anything against you personally, but just as forewarning. If I am ever in a situation where I have to sift through your code and/or debug it...I will promptly have you fired or drawn and quartered or something bad. :)
>>

well said.
 

notfred

Lifer
Feb 12, 2001
38,241
4
0
#include <math.h>

//base and exponent are doubles.

result = pow(base,exponent);


edit: I should read better.

int base = (user input);
int exponent = (user input);
double answer = 1;
for(int i=0;i<exponent;i++){
answer *= base;
}

cout << answer;