' Array Size Macro — demonstrates macro for array element counting
#COMPILE EXE
#DIM ALL

MACRO m_array_size(arr) = (SIZEOF(arr) / SIZEOF(arr(0)))

FUNCTION PBMAIN() AS LONG
    LOCAL smallArr(10) AS LONG
    LOCAL mediumArr(100) AS LONG
    LOCAL largeArr(1000) AS LONG
    LOCAL byteArr(50) AS LONG
    LOCAL bigArr(500) AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL n AS LONG
    LOCAL total AS LONG
    LOCAL arrLen AS LONG
    LOCAL lineStr AS STRING
    LOCAL elemStr AS STRING
    LOCAL testCount AS LONG
    LOCAL passCount AS LONG
    LOCAL failCount AS LONG
    testCount = 0
    passCount = 0
    failCount = 0
    PRINT "============================================"
    PRINT "      ARRAY SIZE MACRO DEMONSTRATION"
    PRINT "============================================"
    PRINT ""
    n = m_array_size(smallArr)
    PRINT "smallArr declared as (10): computed size = " + STR$(n)
    testCount = testCount + 1
    IF n = 11 THEN
        passCount = passCount + 1
        PRINT "  PASS (expected 11)"
    ELSE
        failCount = failCount + 1
        PRINT "  FAIL (expected 11)"
    END IF
    PRINT ""
    n = m_array_size(mediumArr)
    PRINT "mediumArr declared as (100): computed size = " + STR$(n)
    testCount = testCount + 1
    IF n = 101 THEN
        passCount = passCount + 1
        PRINT "  PASS (expected 101)"
    ELSE
        failCount = failCount + 1
        PRINT "  FAIL (expected 101)"
    END IF
    PRINT ""
    n = m_array_size(largeArr)
    PRINT "largeArr declared as (1000): computed size = " + STR$(n)
    IF n = 1001 THEN
        passCount = passCount + 1
        PRINT "  PASS (expected 1001)"
    ELSE
        failCount = failCount + 1
        PRINT "  FAIL (expected 1001)"
    END IF
    PRINT ""
    PRINT "============================================"
    PRINT "     USING ARRAY SIZE FOR LOOP BOUNDS"
    PRINT "============================================"
    PRINT ""
    FOR i = 0 TO m_array_size(smallArr) - 1
        smallArr(i) = i * 10
    NEXT i
    PRINT "Filled smallArr with " + STR$(m_array_size(smallArr)) + " elements"
    lineStr = ""
    FOR i = 0 TO m_array_size(smallArr) - 1
        elemStr = STR$(smallArr(i))
        DO WHILE LEN(elemStr) < 3
            elemStr = " " + elemStr
        LOOP
        lineStr = lineStr + elemStr + " "
    NEXT i
    PRINT lineStr
    PRINT ""
    total = 0
    FOR i = 0 TO m_array_size(mediumArr) - 1
        mediumArr(i) = i * 3 + 1
        total = total + mediumArr(i)
    NEXT i
    PRINT "Sum of mediumArr (" + STR$(m_array_size(mediumArr)) + " elements): " + STR$(total)
    PRINT ""
    PRINT "============================================"
    PRINT "     COMPARING ARRAY SIZES"
    PRINT "============================================"
    PRINT ""
    LOCAL s1 AS LONG
    LOCAL s2 AS LONG
    LOCAL s3 AS LONG
    s1 = m_array_size(smallArr)
    s2 = m_array_size(mediumArr)
    s3 = m_array_size(largeArr)
    PRINT "Array size comparison:"
    PRINT "  smallArr:  " + STR$(s1) + " elements"
    PRINT "  mediumArr: " + STR$(s2) + " elements"
    PRINT "  largeArr:  " + STR$(s3) + " elements"
    PRINT "  byteArr:   " + STR$(m_array_size(byteArr)) + " elements"
    PRINT "  bigArr:    " + STR$(m_array_size(bigArr)) + " elements"
    PRINT ""
    PRINT "============================================"
    PRINT "     FILLING MULTIPLE ARRAYS"
    PRINT "============================================"
    PRINT ""
    FOR i = 0 TO m_array_size(byteArr) - 1
        byteArr(i) = (i * 5) MOD 256
    NEXT i
    FOR i = 0 TO m_array_size(bigArr) - 1
        bigArr(i) = (i * 7) MOD 1000
    NEXT i
    PRINT "byteArr (" + STR$(m_array_size(byteArr)) + " elements) first 10:"
    lineStr = ""
    FOR i = 0 TO 9
        elemStr = STR$(byteArr(i))
        DO WHILE LEN(elemStr) < 3
            elemStr = " " + elemStr
        LOOP
        lineStr = lineStr + elemStr + " "
    NEXT i
    PRINT lineStr
    PRINT ""
    PRINT "bigArr (" + STR$(m_array_size(bigArr)) + " elements) first 10:"
    lineStr = ""
    FOR i = 0 TO 9
        elemStr = STR$(bigArr(i))
        DO WHILE LEN(elemStr) < 4
            elemStr = " " + elemStr
        LOOP
        lineStr = lineStr + elemStr + " "
    NEXT i
    PRINT lineStr
    PRINT ""
    PRINT "============================================"
    PRINT "     ARRAY SIZE FOR ITERATION LIMITS"
    PRINT "============================================"
    PRINT ""
    LOCAL sumEven AS LONG
    LOCAL sumOdd AS LONG
    FOR i = 0 TO m_array_size(mediumArr) - 1
        IF mediumArr(i) MOD 2 = 0 THEN
            sumEven = sumEven + mediumArr(i)
        ELSE
            sumOdd = sumOdd + mediumArr(i)
        END IF
    NEXT i
    PRINT "mediumArr sum of evens: " + STR$(sumEven)
    PRINT "mediumArr sum of odds: " + STR$(sumOdd)
    PRINT ""
    LOCAL minVal AS LONG
    LOCAL maxVal AS LONG
    minVal = bigArr(0)
    maxVal = bigArr(0)
    FOR i = 1 TO m_array_size(bigArr) - 1
        IF bigArr(i) < minVal THEN
            minVal = bigArr(i)
        END IF
        IF bigArr(i) > maxVal THEN
            maxVal = bigArr(i)
        END IF
    NEXT i
    PRINT "bigArr min: " + STR$(minVal) + "  max: " + STR$(maxVal)
    PRINT ""
    PRINT "============================================"
    PRINT "     ARRAY SIZE OFF-BY-ONE CHECK"
    PRINT "============================================"
    PRINT ""
    LOCAL testArr(5) AS LONG
    testArr(0) = 10
    testArr(1) = 20
    testArr(2) = 30
    testArr(3) = 40
    testArr(4) = 50
    testArr(5) = 60
    arrLen = m_array_size(testArr)
    PRINT "testArr(5) has " + STR$(arrLen) + " elements (0.." + STR$(arrLen - 1) + ")"
    PRINT ""
    FOR i = 0 TO arrLen - 1
        PRINT " testArr(" + STR$(i) + ") = " + STR$(testArr(i))
    NEXT i
    PRINT ""
    PRINT "============================================"
    PRINT "     ERROR CHECKING"
    PRINT "============================================"
    PRINT ""
    testCount = testCount + 1
    IF m_array_size(smallArr) > 0 THEN
        passCount = passCount + 1
    ELSE
        failCount = failCount + 1
    END IF
    testCount = testCount + 1
    IF m_array_size(largeArr) > m_array_size(smallArr) THEN
        passCount = passCount + 1
    ELSE
        failCount = failCount + 1
    END IF
    PRINT "Results: " + STR$(passCount) + " passed, " + STR$(failCount) + " failed of " + STR$(testCount)
    PRINT ""
    PRINT "Array size macro demonstration complete"
    FUNCTION = 0
END FUNCTION
