• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

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

RSI

Diamond Member
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

 
maybe you should post how you think you should do it, and where you're getting confused, then we can help you from there 🙂
 
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 had a bunch of code written out at school, and i'm at home now :\

I forget the syntax for the loops 😱

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
 


<< 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. 😛

-RSI
 
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
}
 


<< 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
 
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.
 
I forget the syntax for the loops 😱

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.
 
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
 


<< 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.
 


<<

<< 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? 😕

-RSI
 
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? 😕

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.
 
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.
 
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;}
 
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
 


<< 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. 🙂
 
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.
 


<<

<< 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.
 
#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;
 
Back
Top