I need to create an integer^Power program for example. User gives 2 numbers (first is int, 2nd is power) (3,4) = 3*3*3*3 or 3^4. I think I need to use a for or while loop, but I don't know which? Can someone give me the calculation for the for loop?
#include<iostream>
usingnamespace std;
void main()
{
int x, y, total = 1;
cout<< "This program calculates an exponet for example 2^2 is 4. Please enter the base: ";
cin >> x;
cout << "Please enter the power to raise the base too: ";
cin >> y;
for(int i = 0; i < y; i++)
total = total * x;
cout << "The reslult of " << x << "^" << y << "=" << total << "." << endl;
system("pause");
}
Not necessarily. Some learn by going in depth and making sure they understand every line of code. It would be dumb to turn in homework that you don't understand and then fail the test.
Like me, I am confused about the int i. Is that the loop counter? I see that it increments by 1 each time, so it's i=i+i.
1 2
for(int i = 0; i < y; i++)
total = total * x
I'm not sure what the computer/compiler is doing here. The loop creates a new variable (i) then it declares it to equal to 0. Then if exponent is less than 0 it increments the exponent itself until it equals 0? Then total is doing 1*integer? I don't get that, because wouldnt that be the same number due to multiplication logic? The program works however.
Ok. in the for loop the int i = 0 part is in fact a counter. then the middle part "i < y" is to be read: as long as i is less than y execute the code. after it executes i++ is the same as i = i +1; then it reavaluates the i < y
Here is a step through example of the code if the user entered 2^5
so in this case we will assume that the user entered a 2 and it was stored in the variable x
and the user also entered a 5 for the y value in the above code
and in the above code we set int total = 1;
ok when it hits the for loop the following happens
int i gets created and set to 0
then i < y gets evaluated : in this case 0 is less than 5
the code total = total * x gets executed: in this case total = 1 * 2 so now total= 2
now i++ gets executed and i now equals 1
the loop starts over. so now it checks i < y again: 1 is less than 5
so total = total * x gets executed again: in this case total = 2 * 2 so now total = 4
then i gets incremented and it checks i < y which it is: 2 is less than 5
so now total = 4 * 2 so now total = 8
i is incremented i < y is checked : 3 is less than 5
total = 8 * 2 so now total = 16
i is incremented and i < y checked : 4 is less than 5
total = 16 * 2 so now total = 32
i is incremented and i < y checked: 5 is NOT less than 5 so the loop stops and total remains 32
Some learn by going in depth and making sure they understand every line of code.
That's fine, but you still need practice making the code yourself. In fact, homework like is supposed to do this. The lectures are where you should be analyzing the code.
It would be dumb to turn in homework that you don't understand and then fail the test.
Too bad people DO do that, and they manage to pass the test anyway.