hello

M

MC felon

sorry for that!
anyway..
how do i make a function that takes 2 numbers as arguments (1 as the
number and the 2nd as
the exponent) and raises that number to the power (stored as exponent)
i worked this out.. though i could not get the logical statement inside
the for-loop right.
it's squaring itself for every one incrementation!
here's the listing:

double long pwr( double long num,int exp)
{
int y = 0;
for( y = 1;y <= exp; y++)
{
num = num*num;
}
return num;
}
 
M

Mensanator

MC said:
sorry for that!
anyway..
how do i make a function that takes 2 numbers as arguments (1 as the
number and the 2nd as
the exponent) and raises that number to the power (stored as exponent)
i worked this out.. though i could not get the logical statement inside
the for-loop right.
it's squaring itself for every one incrementation!
here's the listing:

double long pwr( double long num,int exp)
{
int y = 0;

double long num temp = num; // this is num^1
for( y = 1;y <= exp; y++)
{

temp = temp*num; // exponent +1 each iteration

return temp;
 
K

Kai-Uwe Bux

MC said:
sorry for that!
anyway..
how do i make a function that takes 2 numbers as arguments (1 as the
number and the 2nd as
the exponent) and raises that number to the power (stored as exponent)
i worked this out.. though i could not get the logical statement inside
the for-loop right.
it's squaring itself for every one incrementation!
here's the listing:

double long pwr( double long num,int exp)
{
int y = 0;
for( y = 1;y <= exp; y++)
{
num = num*num;
}
return num;
}

This will square the number exp times. You need to introduce a new variable
to keep track of the current power:

double long pwr( double long num,int exp)
{
double result = 1.0;
int y = 0;
for( y = 1;y <= exp; y++)
{
result = result*num;
}
return result;
}

Now, there are slick ways of computing powers using squaring. But the above
might get you started.

A few words on style:

(a) You can declare a running variable within the for loop.

(b) You will want to make it a habit to count from 0 to n-1 instead of from
1 to n. (This is more in line with the indexingconventions used for arrays
and random access sequences like std::deque and std::vector. Keeping this
counting convention consistently keeps you from getting dizzy.)

(c) You also want to make it a habit to use the conceptually least demanding
operation, i.e., use ++i instead of i++. The main rational behind this
suggestion is that postfix operators can be more involved for some iterator
types. Again, making it a habit to use prefix whenever the result is
ignored makes life easier in the long run.

Thus, the function really wants to be written like this:

double long pwr ( double long num, int exp ) {
double result = 1.0;
for ( int i = 0; i < exp; ++i ) {
result *= num;
}
return result;
}


Best

Kai-Uwe Bux
 
M

MC felon

Kai-Uwe Bux said:
This will square the number exp times. You need to introduce a new variable
to keep track of the current power:

double long pwr( double long num,int exp)
{
double result = 1.0;
int y = 0;
for( y = 1;y <= exp; y++)
{
result = result*num;
}
return result;
}

Now, there are slick ways of computing powers using squaring. But the above
might get you started.

A few words on style:

(a) You can declare a running variable within the for loop.

(b) You will want to make it a habit to count from 0 to n-1 instead of from
1 to n. (This is more in line with the indexingconventions used for arrays
and random access sequences like std::deque and std::vector. Keeping this
counting convention consistently keeps you from getting dizzy.)

(c) You also want to make it a habit to use the conceptually least demanding
operation, i.e., use ++i instead of i++. The main rational behind this
suggestion is that postfix operators can be more involved for some iterator
types. Again, making it a habit to use prefix whenever the result is
ignored makes life easier in the long run.

Thus, the function really wants to be written like this:

double long pwr ( double long num, int exp ) {
double result = 1.0;
for ( int i = 0; i < exp; ++i ) {
result *= num;
}
return result;
}


Best

Kai-Uwe Bux

THANX a TON!
 
S

Stuart Golodetz

MC felon said:
sorry for that!
anyway..
how do i make a function that takes 2 numbers as arguments (1 as the
number and the 2nd as
the exponent) and raises that number to the power (stored as exponent)
i worked this out.. though i could not get the logical statement inside
the for-loop right.
it's squaring itself for every one incrementation!
here's the listing:

double long pwr( double long num,int exp)
{
int y = 0;
for( y = 1;y <= exp; y++)
{
num = num*num;
}
return num;
}

long double pwr(long double num, int exp)
{
// Invariant to maintain: accum = num^i

// Establish the invariant.
long double accum = 1.0;
int i = 0;

for(; i<exp; ++i)
{
// Maintain the invariant.
accum *= num; // accum = old(accum) * num, where old(accum) =
num^(i-1)
}

// Postcondition: accum = num^exp (since i = exp at the end)

return accum;
}

Writing it more normally:

long double pwr(long double num, int exp)
{
long double accum = 1.0;

for(int i=0; i<exp; ++i)
{
accum *= num;
}

return accum;
}

We'd prefer to compute it a little more efficiently, though, so let's try
something a bit more cunning:

long double pwr(long double num, int exp)
{
// Invariant to maintain: x * y^i = num^exp

// Establish the invariant.
long double x = 1.0, y = num;
int i = exp;

while(i > 0)
{
// Maintain the invariant.
if(i % 2 == 0)
{
// x * (y^2)^(i/2) = x * y^i
y *= y;
i /= 2;
}
else
{
// (x*y) * y^(i-1) = x * y^i
x *= y;
--i;
}
}

// Postcondition: x = num^exp (since i = 0 at the end)

return x;
}

Example - Computing 3^7

x y i

1 3 7 1 * 3^7
3 3 6 = 3 * 3^6
3 9 3 = 3 * 9^3
27 9 2 = 27 * 9^2
27 81 1 = 27 * 81^1
2187 81 0 = 2187 * 81^0

Hope this helps a bit,
Stu
 
J

Jim Langston

MC felon said:
sorry for that!
anyway..
how do i make a function that takes 2 numbers as arguments (1 as the
number and the 2nd as
the exponent) and raises that number to the power (stored as exponent)
i worked this out.. though i could not get the logical statement inside
the for-loop right.
it's squaring itself for every one incrementation!
here's the listing:

double long pwr( double long num,int exp)
{
int y = 0;
for( y = 1;y <= exp; y++)
{
num = num*num;
}
return num;
}

If this isn't homework, you could just include <cmath> and call the pow
function.
 
H

Howard

MC felon said:
Didn't work!

Are we supposed to guess what the problem was? Personally, I can see at
least one problem, but you really need to tell us what's wrong, not make us
try to guess.

-Howard
 
H

Howard

Mensanator said:
double long num temp = num; // this is num^1

I assume you meant:

double long temp = num;
temp = temp*num; // exponent +1 each iteration


return temp;

Did you try this code? Suppose num is 5, and exp is 1. The loop will
execute once, returning 5*5, or 25. That's one loop too many.

And if exp is 0, the answer should always be 1 (not num).

(By the way, is "double long" legal? I've always seen it written "long
double". Perhaps it doesn't matter to the compiler, but phrasing it "double
long" makes me think it's some kind of special long integer, not a long
double.)

-Howard
 
M

Mensanator

Howard said:
I assume you meant:

double long temp = num;


Did you try this code?
No.

Suppose num is 5, and exp is 1. The loop will
execute once, returning 5*5, or 25. That's one loop too many.

Yeah, I realized that when the OP said it didn't work.
But how hard is it to figure that out? You can only lead
a horse to knowledge, you can't make him think. There's really
no point in solving a particular problem if you don't learn
from it.
And if exp is 0, the answer should always be 1 (not num).

Good point. That's another lesson to be learned. ALWAYS
check the boundary conditions.
(By the way, is "double long" legal?

No clue. I just quoted what he had in his source. If it
wasn't legal, he wouldn't have put it in his source, right? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top