' Student records — parallel arrays for grades, credits, GPA calculation
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL j  AS LONG
    LOCAL numStudents  AS LONG
    LOCAL studentId(10) AS LONG
    LOCAL studentName(10) AS STRING
    LOCAL studentYear(10) AS LONG
    LOCAL studentNumCourses(10) AS LONG
    LOCAL studentGrades(200) AS LONG
    LOCAL studentCredits(200) AS LONG
    LOCAL gpa AS DOUBLE
    LOCAL coursePoints AS DOUBLE
    LOCAL totalCredits AS LONG
    LOCAL gradePoints AS LONG
    LOCAL gradeVal AS LONG
    LOCAL maxGpaIdx  AS LONG
    LOCAL minGpaIdx  AS LONG
    LOCAL maxGpa  AS DOUBLE
    LOCAL minGpa  AS DOUBLE
    LOCAL thisGpa  AS DOUBLE
    LOCAL letterGrade AS STRING
    LOCAL s AS STRING
    LOCAL idx  AS DOUBLE
    LOCAL gpaSum  AS DOUBLE
    LOCAL gpaCount AS LONG

    PRINT "=== Student Records & GPA Calculation ==="
    PRINT

    numStudents = 5
    studentId(0) = 1001
    studentName(0) = "Alice Wonderland     "
    studentYear(0) = 3
    studentNumCourses(0) = 5
    studentGrades(0) = 90
    studentGrades(1) = 85
    studentGrades(2) = 92
    studentGrades(3) = 78
    studentGrades(4) = 95
    studentCredits(0) = 3
    studentCredits(1) = 4
    studentCredits(2) = 3
    studentCredits(3) = 3
    studentCredits(4) = 4

    studentId(1) = 1002
    studentName(1) = "Bob Builder          "
    studentYear(1) = 2
    studentNumCourses(1) = 4
    studentGrades(5) = 72
    studentGrades(6) = 68
    studentGrades(7) = 85
    studentGrades(8) = 70
    studentCredits(5) = 3
    studentCredits(6) = 4
    studentCredits(7) = 3
    studentCredits(8) = 3

    studentId(2) = 1003
    studentName(2) = "Carol Mathematics    "
    studentYear(2) = 4
    studentNumCourses(2) = 5
    studentGrades(9) = 98
    studentGrades(10) = 94
    studentGrades(11) = 96
    studentGrades(12) = 92
    studentGrades(13) = 100
    studentCredits(9) = 3
    studentCredits(10) = 4
    studentCredits(11) = 3
    studentCredits(12) = 4
    studentCredits(13) = 3

    studentId(3) = 1004
    studentName(3) = "David Engineer       "
    studentYear(3) = 1
    studentNumCourses(3) = 4
    studentGrades(14) = 55
    studentGrades(15) = 62
    studentGrades(16) = 71
    studentGrades(17) = 58
    studentCredits(14) = 4
    studentCredits(15) = 3
    studentCredits(16) = 3
    studentCredits(17) = 4

    studentId(4) = 1005
    studentName(4) = "Eva History          "
    studentYear(4) = 3
    studentNumCourses(4) = 5
    studentGrades(18) = 88
    studentGrades(19) = 90
    studentGrades(20) = 82
    studentGrades(21) = 86
    studentGrades(22) = 91
    studentCredits(18) = 3
    studentCredits(19) = 3
    studentCredits(20) = 3
    studentCredits(21) = 4
    studentCredits(22) = 3

    PRINT "--- Student GPAs ---"
    maxGpa = -1.0
    minGpa = 5.0

    FOR i = 0 TO numStudents - 1
        totalCredits = 0
        coursePoints = 0.0
        idx = i * 10
        s = TRIM$(studentName(i))

        FOR j = 0 TO studentNumCourses(i) - 1
            gradeVal = studentGrades(idx + j)
            IF gradeVal >= 90 THEN
                gradePoints = 4
            ELSE
                IF gradeVal >= 80 THEN
                    gradePoints = 3
                ELSE
                    IF gradeVal >= 70 THEN
                        gradePoints = 2
                    ELSE
                        IF gradeVal >= 60 THEN
                            gradePoints = 1
                        ELSE
                            gradePoints = 0
                        END IF
                    END IF
                END IF
            END IF
            coursePoints = coursePoints + gradePoints * studentCredits(idx + j)
            totalCredits = totalCredits + studentCredits(idx + j)
        NEXT j

        IF totalCredits > 0 THEN
            thisGpa = coursePoints / totalCredits
        ELSE
            thisGpa = 0.0
        END IF
        gpa = thisGpa

        IF gpa >= 3.5 THEN
            letterGrade = "A"
        ELSE
            IF gpa >= 2.5 THEN
                letterGrade = "B"
            ELSE
                IF gpa >= 1.5 THEN
                    letterGrade = "C"
                ELSE
                    IF gpa >= 0.5 THEN
                        letterGrade = "D"
                    ELSE
                        letterGrade = "F"
                    END IF
                END IF
            END IF
        END IF

        PRINT "  " & s & " (ID" & STR$(studentId(i)) & ") - GPA: " & FORMAT$(gpa) & " (" & letterGrade & ")"

        IF gpa > maxGpa THEN
            maxGpa = gpa
            maxGpaIdx = i
        END IF
        IF gpa < minGpa THEN
            minGpa = gpa
            minGpaIdx = i
        END IF
    NEXT i

    PRINT
    s = TRIM$(studentName(maxGpaIdx))
    PRINT "Highest GPA: " & s & " (" & FORMAT$(maxGpa) & ")"
    s = TRIM$(studentName(minGpaIdx))
    PRINT "Lowest GPA: " & s & " (" & FORMAT$(minGpa) & ")"

    PRINT
    PRINT "--- Grade Distribution ---"
    FOR i = 0 TO numStudents - 1
        s = TRIM$(studentName(i))
        PRINT "  " & s & ":"
        idx = i * 10
        FOR j = 0 TO studentNumCourses(i) - 1
            gradeVal = studentGrades(idx + j)
            IF gradeVal >= 90 THEN letterGrade = "A"
            IF gradeVal >= 80 AND gradeVal < 90 THEN letterGrade = "B"
            IF gradeVal >= 70 AND gradeVal < 80 THEN letterGrade = "C"
            IF gradeVal >= 60 AND gradeVal < 70 THEN letterGrade = "D"
            IF gradeVal < 60 THEN letterGrade = "F"
            PRINT "    Course" & STR$(j) & ": " & STR$(gradeVal) & " (" & letterGrade & ", " & STR$(studentCredits(idx + j)) & " cr)"
        NEXT j
    NEXT i

    PRINT
    PRINT "Student records complete."

    FUNCTION = 0
END FUNCTION
