2011-03-04 49 views
0

如果你可以指出我正確的方向,那會很棒。我曾嘗試研究這個,但發現沒有解決方案。將2d數組列數據添加在一起?

我有一個二維數組持有學生姓名和3級(ENG,數學,科學)

學生陣列(24,3)

現在,我想用這是該數據在陣列內舉行創建平均值。

我需要一個總的班級平均數:數學專欄,英文專欄和科學。我還需要對所有科目的總平均。

到目前爲止,我使用的代碼帶來了一些奇怪的結果。

studentarray(i,1)/count 
studentarray(i,2)/count 

studentarray(i,3)/count 

totalaverage = (studentarray(i,1) + studentarray(i,2) + studentarray(i,3))/count 

前3個平均值只給出了數組中第一個條目的結果。那麼計算將只顯示3列中顯示的數據,例如101010.

任何幫助將非常感謝!

回答

0
Function CalcSubjectAverage(ByVal studentArray(,) As Integer, ByVal subject As Integer) As Single 
    Dim total As Single 
    For i As Integer = 0 To studentArray.Length 
     total += studentArray(i, 0) 
    Next 

    Return total/studentArray.Length 
End Function 

您需要先添加科目了,那麼你就可以計算出平均:)

+0

感謝您的幫助 – 2011-03-05 12:27:19

+0

如果我的回答對您有幫助,請不要忘記將它標記爲已回答,方法是按複選標記按鈕:) – 2011-03-08 08:28:09

0

我希望這一個也幫助:

我已經在代碼中提供了必要的註釋。

代碼: 「這是2D動態數組 」 StudentArray(,)爲「用於存儲平均值 Ave_Values_per_Subject(,)」 Ave_Values_per_Student() '學生成績 的數據的存儲實際上你只能使用第一陣列,而是使代碼更容易理解,_ '我們聲明另一個2個陣列 昏暗StudentArray(,),Ave_Values_per_Subject(,),Ave_Values_per_Student()作爲對象 ' 是這樣的函數 子SolveMyProblem(BYVAL Num_of_Students作爲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