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

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

    PRINT "====================="
    PRINT " Histogram Builder"
    PRINT "====================="
    PRINT

    histSize = 6
    FOR i = 1 TO 6
        hist(i) = 0
    NEXT i
    PRINT "=== DICE ROLL HISTOGRAM (1000 rolls) ==="
    FOR i = 1 TO 1000
        roll = (TIMER + i * 7) MOD 6 + 1
        results(i) = roll
    NEXT i
    FOR i = 1 TO 1000
        roll = results(i)
        IF roll >= 1 AND roll <= histSize THEN
            hist(roll) = hist(roll) + 1
        END IF
    NEXT i
    PRINT "Dice face distribution:"
    PRINT "========================================"
    maxCount = 0
    FOR i = 1 TO histSize
        IF hist(i) > maxCount THEN maxCount = hist(i)
    NEXT i
    FOR i = 1 TO histSize
        bar = ""
        FOR j = 1 TO hist(i)
            bar = bar & "#"
        NEXT j
        PRINT FORMAT$(i, "###") & ": " & bar & " (" & STR$(hist(i)) & ")"
    NEXT i
    PRINT
    PRINT

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

    histSize = 26
    FOR i = 1 TO 26
        hist(i) = 0
    NEXT i
    text = "The quick brown fox jumps over the lazy dog. This pangram contains every letter of the English alphabet."
    PRINT "=== LETTER FREQUENCY ==="
    PRINT "Text: " & text
    PRINT
    FOR i = 1 TO LEN(text)
        ch = MID$(text, i, 1)
        acode = ASC(ch)
        IF acode >= 65 AND acode <= 90 THEN
            hist(acode - 64) = hist(acode - 64) + 1
        ELSEIF acode >= 97 AND acode <= 122 THEN
            hist(acode - 96) = hist(acode - 96) + 1
        END IF
    NEXT i
    FOR i = 1 TO 26
        PRINT CHR$(i + 64) & ": " & STR$(hist(i))
    NEXT i
    PRINT
    PRINT

    histSize = 10
    hist(1) = 3
    hist(2) = 7
    hist(3) = 12
    hist(4) = 8
    hist(5) = 5
    hist(6) = 9
    hist(7) = 14
    hist(8) = 6
    hist(9) = 4
    hist(10) = 2
    total = 0
    FOR i = 1 TO 10
        total = total + hist(i)
    NEXT i
    FOR i = 1 TO 10
        IF total > 0 THEN norm(i) = hist(i) / total ELSE norm(i) = 0.0
    NEXT i
    maxCount = 1
    FOR i = 2 TO 10
        IF hist(i) > hist(maxCount) THEN maxCount = i
    NEXT i
    PRINT "=== HISTOGRAM ANALYSIS ==="
    PRINT "Mode (most frequent): bin " & STR$(maxCount)
    PRINT "Total observations: " & STR$(total)
    PRINT "Bin  Value  Normalized"
    PRINT "=============================="
    FOR i = 1 TO 10
        PRINT FORMAT$(i, "###") & "   " & FORMAT$(hist(i), "####") & "     " & FORMAT$(norm(i) * 100, "##.##") & "%"
    NEXT i
    PRINT

    PRINT "Histogram demos complete."
    FUNCTION = 0
END FUNCTION
