Velocity Reviews > C++ > Calculating exponents without library functions

# Calculating exponents without library functions

David
Guest
Posts: n/a

 04-11-2004
How can you write a program in C++ by using loop to control the calculation
without the use of library function pwr .For example 3 to the power of 4 (ie
3x3x3x3). Any help will be appreciated

Mike Wahler
Guest
Posts: n/a

 04-12-2004
"David" <(E-Mail Removed)> wrote in message
news:7Ujec.78302\$Pk3.72421@pd7tw1no...
> How can you write a program in C++ by using loop to control the

calculation
> without the use of library function pwr .For example 3 to the power of 4

(ie
> 3x3x3x3). Any help will be appreciated

#include <iostream>

unsigned int pwr(unsigned int base, unsigned int exp)
{
unsigned int result = 1;

while(exp--)
result *= base;

return result;
}

int main()
{
std::cout << pwr(3, 4) << '\n';
return 0;
}

-Mike

David
Guest
Posts: n/a

 04-12-2004
Thanks a lot Mike for the solution

I am trying to do the the calculation without the use of built in pwr
function.I have to use the for or while loop with probably the counter

Thanks once again

David

"Mike Wahler" <(E-Mail Removed)> wrote in message
news:8qlec.5596\$(E-Mail Removed) ink.net...
> "David" <(E-Mail Removed)> wrote in message
> news:7Ujec.78302\$Pk3.72421@pd7tw1no...
> > How can you write a program in C++ by using loop to control the

> calculation
> > without the use of library function pwr .For example 3 to the power of 4

> (ie
> > 3x3x3x3). Any help will be appreciated

>
> #include <iostream>
>
> unsigned int pwr(unsigned int base, unsigned int exp)
> {
> unsigned int result = 1;
>
> while(exp--)
> result *= base;
>
> return result;
> }
>
> int main()
> {
> std::cout << pwr(3, 4) << '\n';
> return 0;
> }
>
>
> -Mike
>
>

Mike Wahler
Guest
Posts: n/a

 04-12-2004
"David" <(E-Mail Removed)> wrote in message
news:X1nec.83152\$Ig.68204@pd7tw2no...
> Thanks a lot Mike for the solution
>
> I am trying to do the the calculation without the use of built in pwr
> function.

There is no built-in function called 'pwr()' (however there
is one called 'pow()' declared by <cmath>

'pwr()' is the name of the function I wrote in my post.

>I have to use the for or while loop with probably the counter

The function I posted uses a while loop with a counter.
(the 'counter' is the exponent parameter).

-Mike

Matt
Guest
Posts: n/a

 04-15-2004
David wrote:
> How can you write a program in C++ by using loop to control the calculation
> without the use of library function pwr .For example 3 to the power of 4 (ie
> 3x3x3x3). Any help will be appreciated

Try posting to comp.lang.c++.homework.

Mabden
Guest
Posts: n/a

 04-15-2004
"Matt" <(E-Mail Removed)> wrote in message
newsspfc.1741\$%(E-Mail Removed)...
> David wrote:
> > How can you write a program in C++ by using loop to control the

calculation
> > without the use of library function pwr .For example 3 to the power

of 4 (ie
> > 3x3x3x3). Any help will be appreciated

>
> Try posting to comp.lang.c++.homework.
>

I would use a loop to control the calculation. For example 3 to the
power of 4 is the same as 3 x 3 x 3 x 3.

Do I get part of the A?

--
Mabden

Bruce Clement
Guest
Posts: n/a

 04-15-2004
Matt wrote:
> David wrote:
>
>> How can you write a program in C++ by using loop to control the
>> calculation
>> without the use of library function pwr .For example 3 to the power of
>> 4 (ie
>> 3x3x3x3). Any help will be appreciated

>
>
> Try posting to comp.lang.c++.homework.
>

Try something like this. It uses a redundant loop and calculates
X to the power of Y in approximately Y! steps.

/************************************************** *************************
main.cpp - description
-------------------
begin : Thu Apr 15 19:35:27 NZDT 2004
copyright : (C) 2004 by Bruce Clement
email : http://www.velocityreviews.com/forums/(E-Mail Removed)

************************************************** *************************/

/************************************************** *************************
*
* This program is free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
* either version 2 of the License, or (at your option) any
* later version.
*
************************************************** ********************/

#include <iostream>

double power( double x, int y )
{
double rslt = 1;
for( int i = y; 0 < i--; )
rslt = x * power( x, y - 1 );
return rslt;
}

int main(int argc, char *argv[])
{
std::cout << "Zap, Pow, Kapow ... 3^4 = " << power( 3.0, 4 ) <<
std::endl;

return 0;
}

Richard Herring
Guest
Posts: n/a

 04-15-2004
In message <c5ldnc\$ctu\$(E-Mail Removed)>, Bruce Clement
<(E-Mail Removed)> writes
>Matt wrote:
>> David wrote:
>>
>>> How can you write a program in C++ by using loop to control the
>>>calculation
>>> without the use of library function pwr .For example 3 to the power
>>>of 4 (ie
>>> 3x3x3x3). Any help will be appreciated

>> Try posting to comp.lang.c++.homework.
>>

>
>Try something like this. It uses a redundant loop and calculates
>X to the power of Y in approximately Y! steps.
>
>
>/************************************************** *************************
> main.cpp - description
> -------------------
> begin : Thu Apr 15 19:35:27 NZDT 2004
> copyright : (C) 2004 by Bruce Clement
> email : (E-Mail Removed)
>************************************************* ***********************
>***/
>
>/************************************************** *************************
> *
> * This program is free software; you can redistribute it
> * and/or modify it under the terms of the GNU General Public
> * either version 2 of the License, or (at your option) any
> * later version.
> *
> ************************************************** ********************/
>
>#include <iostream>
>
>double power( double x, int y )
>{
> double rslt = 1;
> for( int i = y; 0 < i--; )
> rslt = x * power( x, y - 1 );

Couldn't you use a recursive function here instead of operator* ?

> return rslt;
>}
>
>
>int main(int argc, char *argv[])
>{
> std::cout << "Zap, Pow, Kapow ... 3^4 = " << power( 3.0, 4 ) <<
>std::endl;
>
> return 0;
>}
>

--
Richard Herring

Bruce Clement
Guest
Posts: n/a

 04-15-2004
Richard Herring wrote:
> In message <c5ldnc\$ctu\$(E-Mail Removed)>, Bruce Clement
> <(E-Mail Removed)> writes
>
>> Matt wrote:
>>
>>> David wrote:
>>>
>>>> How can you write a program in C++ by using loop to control the
>>>> calculation
>>>> without the use of library function pwr .For example 3 to the power
>>>> of 4 (ie
>>>> 3x3x3x3). Any help will be appreciated
>>>
>>> Try posting to comp.lang.c++.homework.
>>>

>>
>> Try something like this. It uses a redundant loop and calculates
>> X to the power of Y in approximately Y! steps.
>>
>>

[...]
>
> Couldn't you use a recursive function here instead of operator* ?
>

[...]

LOL, You're evil. I like that in a person.

rossum
Guest
Posts: n/a

 04-16-2004
On Fri, 16 Apr 2004 09:34:20 +1200, Bruce Clement
<(E-Mail Removed)> wrote:

>Richard Herring wrote:
>> In message <c5ldnc\$ctu\$(E-Mail Removed)>, Bruce Clement
>> <(E-Mail Removed)> writes
>>
>>> Matt wrote:
>>>
>>>> David wrote:
>>>>
>>>>> How can you write a program in C++ by using loop to control the
>>>>> calculation
>>>>> without the use of library function pwr .For example 3 to the power
>>>>> of 4 (ie
>>>>> 3x3x3x3). Any help will be appreciated
>>>>
>>>> Try posting to comp.lang.c++.homework.
>>>>
>>>
>>> Try something like this. It uses a redundant loop and calculates
>>> X to the power of Y in approximately Y! steps.
>>>
>>>

>[...]
>>
>> Couldn't you use a recursive function here instead of operator* ?
>>

>[...]
>
>LOL, You're evil. I like that in a person.

Real programmers do it with the Peano Axioms

rossum

#include <iostream>
#include <cstdlib>

//-------------------------------------

int successor(int num)
{
int result;
result = ++num;
return result;
} // end successor()

//-------------------------------------

{
int result;
if (b == 0)
result = a;
else
result = successor(add(a, b - 1));
return result;

//-------------------------------------

int multiply(int a, int b)
{
int result;
if (b == 0)
result = 0;
else
result = add(a, multiply(a, b - 1));
return result;
} // end multiply()

//-------------------------------------

int power(int x, int y)
{
int result = 1;
// An interesting variation, thanks Bruce.
for (int i = y; 0 < i--; )
result = multiply(x, power(x, y - 1));
return result;
} // end power()

//-------------------------------------

int main(int argc, char **argv)
{
std::cout << "Three raised to the power four is "
<< power(3, 4)
<< std::endl;
return EXIT_SUCCESS;
} // end main()

--

The Ultimate Truth is that there is no Ultimate Truth