How to resolve the algorithm Apply a callback to an array step by step in the uBasic/4tH programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Apply a callback to an array step by step in the uBasic/4tH programming language

Table of Contents

Problem Statement

Take a combined set of elements and apply a function to each element.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Apply a callback to an array step by step in the uBasic/4tH programming language

Source code in the ubasic/4th programming language

S = 5                                  ' Size of the array

For x = 0 To S - 1                     ' Initialize array
  @(x) = x + 1
Next

Proc _MapArray (_SquareRoot, S)        ' Call mapping procedure

For x = 0 To S - 1                     ' Print results
  Print "SQRT(";x+1;") = ";Using "#.####";@(x)
Next

For x = 0 To S - 1                     ' Reinitialize array
  @(x) = x + 1
Next

Proc _MapArray (_Cosine, S)            ' Call mapping procedure

Print : For x = 0 To S - 1             ' Print results
  Print "COS(";x+1;") = ";Using "#.####";@(x)
Next

End


_MapArray Param(2)                     ' Param(1) = function
  Local (1)                            ' Param(2) = array size

  For c@ = 0 To b@ - 1
    @(c@) = FUNC(a@(@(c@)))
  Next
Return


_SquareRoot Param (1)                  ' This is an integer SQR subroutine
  Local (2)

  b@ = (10^(4*2)) * a@                 ' Output is scaled by 10^4
  a@ = b@

  Do
    c@ = (a@ + (b@ / a@))/2
  Until (Abs(a@ - c@) < 2)
    a@ = c@
  Loop

Return (c@)


_Cosine Param(1)                       ' This is an integer COS subroutine
  Push Abs((a@*10000)%62832)           ' Output is scaled by 10^4
  If Tos()>31416 Then Push 62832-Pop()
  Let a@=Tos()>15708
  If a@ Then Push 31416-Pop()
  Push Tos()
  Push (Pop()*Pop())/10000
  Push 10000+((10000*-(Tos()/56))/10000)
  Push 10000+((Pop()*-(Tos()/30))/10000)
  Push 10000+((Pop()*-(Tos()/12))/10000)
  Push 10000+((Pop()*-(Pop()/2))/10000)
  If a@ Then Push -Pop()               ' Result is directly transferred
Return                                 ' through the stack

  

You may also check:How to resolve the algorithm Inheritance/Single step by step in the Objective-C programming language
You may also check:How to resolve the algorithm Hello world/Newline omission step by step in the Oberon-2 programming language
You may also check:How to resolve the algorithm Partition an integer x into n primes step by step in the J programming language
You may also check:How to resolve the algorithm XML/Input step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm Man or boy test step by step in the Scheme programming language