Надеюсь, этот тоже поможет:
Я предоставил необходимые комментарии в коде.
Код:
'это 2D динамические массивы
StudentArray (,) для хранения данных студенческих оценок
'Ave_Values_per_Subject (,) для хранения средних значений
'Ave_Values_per_Student ()
'на самом деле вы можете использовать только первый массив, но чтобы сделать коды более понятными, _
мы объявляем еще 2 массива
Dim StudentArray (,), Ave_Values_per_Subject (,), Ave_Values_per_Student () как объект
это функция
Sub SolveMyProblem (ByVal Num_of_Students As UInteger, ByVal Num_of_Subjects As UInteger)
' get the number of students and resize the array
ReDim StudentArray(Num_of_Students - 1, Num_of_Subjects - 1), Ave_Values_per_Subject(0, Num_of_Subjects - 1), _
Ave_Values_per_Student(Num_of_Students - 1)
' you can imagine this as having a table with Num_of_Students as the number of rows and Num_of_Subjects as the _
' number of columns
' StudentArray(0,0) gives the value of student #1 at subject #1 (say english) _
' StudentArray(0,1) gives the value of student #1 at subject #2 (say math) _
' StudentArray(1,3) gives the value of student #2 at subject #3 (say science) and so on
' example: we have 4 students with english, math, and science as subjects
' thus Num_of_Students = 4 and Num_of_Subjects = 3 giving us StudentArray(3,2) and Ave_Values_per_Subject(0,2)
' Suppose the grades of student #1 for english, math, and science are 70, 80, and 90 respectively; _
' student #2 = {75, 80, 90}; student #3 = {75, 85, 95}; and student #4 = {60, 100, 85}
' Suppose index 0 of StudentArray (StudentArray(0,0)) represents english subject; _
' StudentArray (0,1) = math; and StudentArray (0,2) = science
'' to calculate for the average of students for EACH subject and to store it in a separate array:
For subjectCount = 0 To UBound(StudentArray, 2)
Dim SumPerSubject As Single
For studentCount = 0 To UBound(StudentArray, 1)
SumPerSubject += StudentArray(studentCount, subjectCount)
Next
' average of students per subject:
Ave_Values_per_Subject(0, subjectCount) = SumPerSubject / (UBound(StudentArray, 1) + 1)
' the average of students per subject is determined and stored in the above array
' this means that the average of values for english is stored in Ave_Values_per_Subject(0,0) _
' Ave_Values_per_Subject(0,1) for math; and Ave_Values_per_Subject(0,2) for science
Next
'' to calculate for the average of EACH student on all subjects:
For studentCount = 0 To UBound(StudentArray, 1)
Dim SumPerStudent As Single
For subjectCount = 0 To UBound(StudentArray, 2)
SumPerStudent += StudentArray(studentCount, subjectCount)
Next
' ave values of each student on all subjects:
Ave_Values_per_Student(studentCount) = SumPerStudent / 3
Next
End Sub