Jump to content
Sign in to follow this  
Underdogger

Power

Recommended Posts

Underdogger

This is the code which I wrote with For loop to caculate Exponent..

It can be listed pow 1, pow2...,etc

Noraml Forloop ver.

$x = InputBox("Power", "Base:")
$y = InputBox("Power", "Exponent:")
MsgBox(0, "", powFor($x, $y))
Func powFor($x, $y)
    Dim $n, $t = $x, $c = 1, $p = 0
    If $y == 0 Then
        Return 1
    ElseIf $y < 0 Then
        For $i = StringRegExpReplace($y, '-(\d+)', '$1') To 1 Step - 1
            If $c = 1 Then
                $x *= 1
                $c += 1
                $p += 1
                $n &= "p" & $p & " : " & (1 / $x) & @CRLF
            Else
                $x *= $t
                $p += 1
                $n &= "p" & $p & " : " & (1 / $x) & @CRLF
            EndIf
        Next
    Else
        For $i = $y To 1 Step - 1
            If $c == 1 Then
                $x *= 1
                $c += 1
                $p += 1
                $n &= "p" & $p & " : " & $x & @CRLF
            Else
                $x *= $t
                $p += 1
                $n &= "p" & $p & " : " & $x & @CRLF
            EndIf
        Next
    EndIf
    Return $n
EndFunc

And this is the Recursion for caculating Exponent..

But how can I list each pow in Recursion ver. as For loop ver.

Recursion ver.

MsgBox(0, "", powRec($x, $y))
Func powRec($x, $y)
    If $y == 0 Then
        Return 1
    ElseIf $y == 1 Then
        Return $x
    Else
        Return $x * powRec($x, $y - 1)
    EndIf
EndFunc
Edited by Underdogger

Share this post


Link to post
Share on other sites
orbs

the simplest way - although probably will not be acceptable by your professor - is to dedicate a global var to accumulate the steps.

I made the following changes to your code:

declare global var $s

the func returns the computed value, not the string.

the func calculates it into $r before return, because $r is processed before return

if you want the func to return the string, without using any global var, then you will need to pass it as a 3rd parameter and perform some string manipulation.

PS how about some validation routines? your func fails with negative exponent!

Global $s=''
$x = InputBox("Power", "Base:")
$y = InputBox("Power", "Exponent:")
MsgBox(0, 'pow', powRecS($x, $y))
MsgBox(0, '$s', $s)

Func powRecS($x, $y)
Local $r
If $y == 0 Then
$r=1
ElseIf $y == 1 Then
$r=$x
Else
$r=$x * powRecS($x, $y - 1)
EndIf
$s=$s&' p'&$y&' : '&$r&@CR
Return $r
EndFunc

Share this post


Link to post
Share on other sites
Underdogger

the simplest way - although probably will not be acceptable by your professor - is to dedicate a global var to accumulate the steps.

I made the following changes to your code:

declare global var $s

the func returns the computed value, not the string.

the func calculates it into $r before return, because $r is processed before return

if you want the func to return the string, without using any global var, then you will need to pass it as a 3rd parameter and perform some string manipulation.

PS how about some validation routines? your func fails with negative exponent!

Global $s=''
$x = InputBox("Power", "Base:")
$y = InputBox("Power", "Exponent:")
MsgBox(0, 'pow', powRecS($x, $y))
MsgBox(0, '$s', $s)

Func powRecS($x, $y)
Local $r
If $y == 0 Then
$r=1
ElseIf $y == 1 Then
$r=$x
Else
$r=$x * powRecS($x, $y - 1)
EndIf
$s=$s&' p'&$y&' : '&$r&@CR
Return $r
EndFunc

Is it possible to set negative Exponent in Recusion version?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×