Task

Write a function/procedure/subroutine that is called with an integer value and returns the middle three digits of the integer if possible or a clear indication of an error if this is not possible.

Note: The order of the middle digits should be preserved.

Your function should be tested with the following values; the first line should return valid answers, those of the second line should return clear indications of an error:

123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345
1, 2, -1, -10, 2002, -2002, 0

Show your output on this page.




INCLUDE "H6:REALMATH.ACT"
INCLUDE "D2:PRINTF.ACT" ;from the Action! Tool Kit
 
PROC MiddleThreeDigits(REAL POINTER n CHAR ARRAY res)
  REAL r
  CHAR ARRAY s(15)
  BYTE i
 
  RealAbs(n,r)
  StrR(r,s)
  i=s(0)
  IF i<3 OR (i&1)=0 THEN
    res(0)=0
  ELSE
    i==RSH 1
    SCopyS(res,s,i,i+2)
  FI
RETURN
 
PROC Test(CHAR ARRAY s)
  REAL n
  CHAR ARRAY res(4)
 
  ValR(s,n)
  MiddleThreeDigits(n,res)
  IF res(0) THEN
    PrintF("%9S -> %S%E",s,res)
  ELSE
    PrintF("%9S -> error!%E",s)
  FI
RETURN
 
PROC Main()
  Put(125) PutE() ;clear the screen
 
  Test("123")     Test("12345")
  Test("1234567") Test("987654321")
  Test("10001")   Test("-10001")
  Test("-123")    Test("-100")
  Test("100")     Test("-12345")
  Test("1")       Test("2")
  Test("-1")      Test("-10")
  Test("2002")    Test("-2002")
  Test("0")
RETURN