' Histogram — build frequency histogram array from data
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i AS LONG
    LOCAL j AS LONG
    LOCAL roll AS LONG
    LOCAL bar AS STRING
    LOCAL h(1 TO 26) AS LONG
    LOCAL results(1 TO 1000) AS LONG
    LOCAL scores(1 TO 100) AS LONG
    LOCAL norm(1 TO 10) AS DOUBLE
    LOCAL text AS STRING
    LOCAL ch AS STRING
    LOCAL acode AS LONG
    LOCAL total AS LONG
    LOCAL maxCount AS LONG

    PRINT "Dice roll histogram (1000 rolls)"
    FOR i = 1 TO 6
        h(i) = 0
    NEXT i
    FOR i = 1 TO 1000
        roll = (i * 7) MOD 6 + 1
        results(i) = roll
    NEXT i
    FOR i = 1 TO 1000
        roll = results(i)
        IF roll >= 1 AND roll <= 6 THEN h(roll) = h(roll) + 1
    NEXT i
    maxCount = 0
    FOR i = 1 TO 6
        IF h(i) > maxCount THEN maxCount = h(i)
    NEXT i
    FOR i = 1 TO 6
        bar = ""
        FOR j = 1 TO h(i)
            bar = bar + "#"
        NEXT j
        PRINT STR$(i) + ": " + bar + " (" + STR$(h(i)) + ")"
    NEXT i

    PRINT ""
    PRINT "Score distribution (100 students)"
    FOR i = 1 TO 10
        h(i) = 0
    NEXT i
    FOR i = 1 TO 100
        roll = ((i * 13) MOD 51) + 50
        scores(i) = roll
    NEXT i
    FOR i = 1 TO 100
        roll = scores(i)
        IF roll >= 95 THEN h(10) = h(10) + 1
        IF roll >= 85 AND roll < 95 THEN h(9) = h(9) + 1
        IF roll >= 75 AND roll < 85 THEN h(8) = h(8) + 1
        IF roll >= 65 AND roll < 75 THEN h(7) = h(7) + 1
        IF roll >= 55 AND roll < 65 THEN h(6) = h(6) + 1
        IF roll < 55 THEN h(1) = h(1) + 1
    NEXT i
    PRINT "  <55 : " + STR$(h(1))
    PRINT " 55-64: " + STR$(h(6))
    PRINT " 65-74: " + STR$(h(7))
    PRINT " 75-84: " + STR$(h(8))
    PRINT " 85-94: " + STR$(h(9))
    PRINT " 95-100: " + STR$(h(10))

    PRINT ""
    PRINT "Letter frequency"
    FOR i = 1 TO 26
        h(i) = 0
    NEXT i
    text = "The quick brown fox jumps over the lazy dog."
    FOR i = 1 TO LEN(text)
        ch = MID$(text, i, 1)
        acode = ASC(ch)
        IF acode >= 65 AND acode <= 90 THEN
            h(acode - 64) = h(acode - 64) + 1
        END IF
        IF acode >= 97 AND acode <= 122 THEN
            h(acode - 96) = h(acode - 96) + 1
        END IF
    NEXT i
    FOR i = 1 TO 26
        IF h(i) > 0 THEN PRINT CHR$(i + 64) + ": " + STR$(h(i))
    NEXT i

    PRINT ""
    PRINT "Histogram analysis"
    h(1) = 3: h(2) = 7: h(3) = 12: h(4) = 8: h(5) = 5
    h(6) = 9: h(7) = 14: h(8) = 6: h(9) = 4: h(10) = 2
    total = 0
    FOR i = 1 TO 10
        total = total + h(i)
    NEXT i
    FOR i = 1 TO 10
        IF total > 0 THEN norm(i) = h(i) / total ELSE norm(i) = 0.0
    NEXT i
    maxCount = 1
    FOR i = 2 TO 10
        IF h(i) > h(maxCount) THEN maxCount = i
    NEXT i
    PRINT "Mode: bin " + STR$(maxCount)
    PRINT "Total: " + STR$(total)
    FOR i = 1 TO 10
        PRINT STR$(i) + ": " + STR$(h(i)) + " (" + STR$(norm(i) * 100) + "%)"
    NEXT i

    PRINT "Done."
    FUNCTION = 0
END FUNCTION
