Programming Homework

Emporius

Space Core
Joined
Aug 20, 2007
Messages
3,553
Reaction score
10
Well I guess this thread goes here.

I am working on a C++ programming problem for school, and I've managed to muck it all up. The question says:

"Example 4. Write a program that finds x^y for a given base x and exponent y. (Don?t use pow function to solve this examle)."


I need to use repetition structures, and in my frustration I've managed to butcher the code.

]#include <iostream>
#include <iomanip>

using namespace std;

int main () {


int x;
int y;
int pro;


cout<<"Please enter an integer value"<<endl;
cin>>x;

cout<<"Now enter a value for the exponent"<<endl;
cin>>y;

pro=0;
x=y;


while (x<=pro)
{
pro=pro*x;
x=x*x;}

cout<<""<<endl;

cout<<"The sum of all of the numbers between 1 and is equal to:"<<endl;

cout<<pro<<endl;

cout<<""<<endl;

return 0;
}


If anyone could lend a hand it would be greatly appreciated.


PS: This is the area of main confusion:

pro=0;
x=y;


while (x<=pro)
{
pro=pro*x;
x=x*x;}



edit: Ignore the bottom, it's just the remains of an old question
 
What is pro?

It should be a for loop
for(int k=0; k<y; k++)
{x*=x}

cout>>x
 
What is pro?

It should be a for loop
for(int k=0; k<y; k++)
{x*=x}

cout>>x

I agree it should be a for loop, and you're closer. That code won't work though. That computes x^(2^y). It's a step in the right direction though. I'm glad you didn't just give him the answer. That'd be cheating.

To the OP: Think a little more on what 'pro' should be initialized to, and how you want your loop to act (a set number of times? a variable number of times? until a condition is fulfilled?).
 
Code:
#include <iostream.h>
#include <iomanip.h>

using namespace std;

int main () {
    int x,y,count,pro=1;

    cout<<"Please enter an integer value"<<endl;
    cin>>x;
    cout<<"Now enter a value for the exponent"<<endl;
    cin>>y;

    count=0;
    while (count++<y) {
        pro=pro*x;
        }

    cout<<"Result:- "<<pro<<"\n";
    }

Done and done. :)

It should be a for loop
:rolleyes: Any loop will work in such a simple case.
 
Code:
#include <iostream.h>
#include <iomanip.h>

using namespace std;

int main () {
    int x,y,count,pro=1;

    cout<<"Please enter an integer value"<<endl;
    cin>>x;
    cout<<"Now enter a value for the exponent"<<endl;
    cin>>y;

    count=0;
    while (count<y) {
        pro=pro*x;
        }

    cout<<"Result:- "<<pro<<"\n";
    }

Done and done. :)


:rolleyes: Any loop will work in such a simple case.

:rolleyes: Any loop, you say? Not quite any loop. An infinite loop like yours doesn't do it.

edit: You fixed it, but a for loop is still better in this case. You can make any loop structure work in any case, but the for-loop is generally preferred when incrementing a counter for a number of cycles known at the beginning of the loop.

Anyone want to get it running in logarithmic time?
 
Fixed, heh. :p

how dare you mock me
Emporius said:
while (x<=pro)
{
pro=pro*x;
x=x*x;}
Firstly, don't use the base x in the condition. It's better not to change the value of the input.

Next, the condition in the while loop is wierd. Use a counter to keep track of how many times pro has been multiplied by x, and then compare that counter to exponent y. (Remember that pro has to be initialised to 1 to get the correct result)

I think the rest is fine.
 
Back
Top