Go Back  IT Forums > Software > Software Wanted
User Name
Password
Reply
 
Thread Tools Search this Thread Display Modes

hello
  #1
Old 09-21-2006, 06:59 PM
MC Felon
Junior Member


MC Felon is offline
MC Felon's Info
Join Date: Sep 2006
Posts: 3
Default hello

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

Reply With Quote
hello
  #2
Old 09-21-2006, 07:30 PM
Pascal Bourguignon
Junior Member


Pascal Bourguignon is offline
Pascal Bourguignon's Info
Join Date: Jun 2005
Posts: 2
Default hello

"MC Felon" <paec.nwa@gmail.com> writes:
Quote:
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:


Let's add some assertions to understand what it is computing:
Quote:
double long pwr( double long num,int exp) { int y = 0;


/* num = n = n^(2^y) */
Quote:
for( y = 1;y <= exp; y++) {


/* num = n^(2^(y-1)) */
Quote:
num = num*num;


/* num = n^(2^y) */
Quote:
}


/* num = n^(2^exp) */
Quote:
return num; }


So your function pwr(n,e) returns n^(2^e).


Now what you want is n^e.
What does this mean?

n^e = n*n*n*....*n
\_____...._/
e times n

n^e = n*(n*n*....*n
\___...._/
e-1 times n

n^e = n*(n^(e-1))

Another interesting factoid is that n^0 = 1

Therefore we could take as invariant assertion: p = n^i

/* p = n^(i-1) */
p*=n;
/* p = n*n^(i-1) = n^i */

Now, if we add as stop condition i==e, we'd get as post condition:

/* p = n^i & i = e */ ==> /* p = n^e */

which is what we want. So:

while(i!=e){
/* p = n^i) */
i++;
/* p = n^(i-1) */
p*=n;
/* p = n^i) */
}
/* p = n^e */


Remains to add the initial condition: /* i = 0 & p = n^0 */
To ensure that, we need to execute: i=0; p=1;

int pwr(unsigned int n,unsigned int e){
unsigned int i;
unsigned int p;

i=0;p=1;
/* i = 0 & p = n^0 */
while(i!=e){
/* p = n^i) */
i++;
/* p = n^(i-1) */
p*=n;
/* p = n^i) */
}
/* p = n^e */

return p;
}


--
__Pascal Bourguignon__ http://www.informatimago.com/

PLEASE NOTE: Some quantum physics theories suggest that when the
consumer is not directly observing this product, it may cease to
exist or will exist only in a vague and undetermined state.
Reply With Quote
hello
  #3
Old 09-21-2006, 07:43 PM
MC Felon
Junior Member


MC Felon is offline
MC Felon's Info
Join Date: Sep 2006
Posts: 3
Default hello

Pascal Bourguignon wrote:
Quote:
"MC Felon" <paec.nwa@gmail.com> writes:
Quote:
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:
Let's add some assertions to understand what it is computing:
Quote:
double long pwr( double long num,int exp) { int y = 0;
/* num = n = n^(2^y) */
Quote:
for( y = 1;y <= exp; y++) {
/* num = n^(2^(y-1)) */
Quote:
num = num*num;
/* num = n^(2^y) */
Quote:
}
/* num = n^(2^exp) */
Quote:
return num; }
So your function pwr(n,e) returns n^(2^e). Now what you want is n^e. What does this mean? n^e = n*n*n*....*n \_____...._/ e times n n^e = n*(n*n*....*n \___...._/ e-1 times n n^e = n*(n^(e-1)) Another interesting factoid is that n^0 = 1 Therefore we could take as invariant assertion: p = n^i /* p = n^(i-1) */ p*=n; /* p = n*n^(i-1) = n^i */ Now, if we add as stop condition i==e, we'd get as post condition: /* p = n^i & i = e */ ==> /* p = n^e */ which is what we want. So: while(i!=e){ /* p = n^i) */ i++; /* p = n^(i-1) */ p*=n; /* p = n^i) */ } /* p = n^e */ Remains to add the initial condition: /* i = 0 & p = n^0 */ To ensure that, we need to execute: i=0; p=1; int pwr(unsigned int n,unsigned int e){ unsigned int i; unsigned int p; i=0;p=1; /* i = 0 & p = n^0 */ while(i!=e){ /* p = n^i) */ i++; /* p = n^(i-1) */ p*=n; /* p = n^i) */ } /* p = n^e */ return p; } -- __Pascal Bourguignon__ http://www.informatimago.com/ PLEASE NOTE: Some quantum physics theories suggest that when the consumer is not directly observing this product, it may cease to exist or will exist only in a vague and undetermined state.



THANKS A LOT!!!!!
i am not finding words to thank u....
regards!
will ask here if i have doubts!

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump



Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Style Design by vBStyles.com


Top Contact Us - IT Forums - Archive - MyLounge Top
MyLounge.com Site Map
Forum: Cars, Cell Phone, Database, Games, Home Improvement, IT, Music, School, Sports, Web Design, Web Server, Weight Loss

The MyLounge.com forum is intended for informational use only and should not be relied upon and is not a substitute for any advice. The information contained on MyLounge.com are opinions and suggestions of members and is not a representation of the opinions of MyLounge.com. MyLounge.com does not warrant or vouch for the accuracy, completeness or usefulness of any postings or the qualifications of any person responding. Please consult a expert or seek the services of an attorney in your area for more accuracy on your specific situation. Please note that our forums also serve as mirrors to Usenet newsgroups. Many posts you see on our forums are made by newsgroup users who may not be members of MyLounge.com Term of Service