0

I am currently trying to write a batch file for a project that will mimic the structure of the following pseudocode:

void h ( int n ) 
     if ( n ≥ 4 ) 
          h ( n / 2 ) 
     end if 
     print n 
end h

And generate the following output:

2
4
8
16

When the function is given 16 as input.

This is the closest I've come so far:

@ECHO off
SET /A number=%1 
IF %number% GEQ 4 (
    CALL recursive-36 number/2
REM The following line returns 4, 8, 16.
REM ECHO %number%
)
REM The following line returns 2, 2, 2, 2.
ECHO %number%

No matter where I put the ECHO %number% statement, however, I can't get 2, 4, 8, 16 as output. Why is that? Is this a pass-by-reference thing? Or is there something else I'm missing?

3
  • You write IF %number% GEQ 4 ( which means if number is greater or equal to 4. That is why it never does a 2. Isn't it enough to change that to GEQ 2?
    – LPChip
    Commented Jun 20, 2022 at 11:03
  • That's a good point, and I've wondered about that myself. But does that mean there error's actually in the pseudocode as well? (So it would seem.) Commented Jun 21, 2022 at 18:43
  • I would say so, yes. Although on the other hand, 4 divided by 2 would become 2. So on second thought somehting else seems to go wrong.
    – LPChip
    Commented Jun 21, 2022 at 19:32

2 Answers 2

1

This recursive algorithm in batch can be written like this:

@echo off
setlocal EnableDelayedExpansion
set /A number=16
echo %number%
call :recursive
goto :eof

:recursive
if %number% gtr 2 (
  set /A number/=2
  echo %number%
  call :recursive
)
exit /b

References :

4
  • 1
    Alternate solution, make the batchfile call itself with 2 parameters, the init value and the max, and it parses the current value to the init parameter until init is larger than max.
    – LPChip
    Commented Jun 20, 2022 at 11:00
  • recurseive is a label and not a function. All variables are available and usable.
    – harrymc
    Commented Jun 20, 2022 at 15:08
  • Thanks for your help. Commented Jun 21, 2022 at 18:45
  • If my answer was helpful, please consider marking it as accepted (click the ✔ sign).
    – harrymc
    Commented Jun 21, 2022 at 19:17
0
@echo off 

if not "%~1" == "" (
     setlocal enabledelayedexpansion
     set "_n=0")else exit /b 1

%:^0     
if %~1 geq 4 (
     set /a "_n=%~1/2"
     echo;!_n! & call %:^0 !_n!
    )else endlocal && exit /b 0

  • Results/Outputs:

enter image description here


  • For Reverse Outputs/Results:
@echo off 

if "%~1"=="" (exit /b 1)else set "_n=0"
setlocal enabledelayedexpansion

%:^0     
if %~1 geq 4 (
     set /a "_n=%~1/2"
     call set "_out=!_n!,!_out!"
     if !_n! neq %~1 call %:^0 !_n!
     exit /b)else for %%i in (!_out!)do echo;%%i

endlocal & exit /b 0
  • Outputs/Results:

enter image description here _ _

1. You can treat your bat as a function, since it already starts receiving an argument.

2. If no arguments are passed, it aborts/exits, if any are passed, it loops with if + call it handles the division and prints the result...

3. For a better understanding of how to handle/operate one with a label/function in bat, see this answer:

2
  • 1
    Thanks for your help. Commented Jun 21, 2022 at 18:45
  • if my answer was helpful, please feel free to make use of it d;)
    – Io-oI
    Commented Jun 22, 2022 at 13:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .