第5講 配列の学習

第8話 3次元配列の利用による年間成績一覧表の作成

解答例
Public Class Form1

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

     '変数の宣言
     Dim i As Integer, j As Integer, w As Integer

     '187行追加
     For i = 1 To 187
       DataGridView1.Rows.Add()
     Next

     For i = 0 To 3
       '見出しを入れる
       If i = 0 Then DataGridView1(0, 47 * i).Value = "1学期"
       If i = 1 Then DataGridView1(0, 47 * i).Value = "2学期"
       If i = 2 Then DataGridView1(0, 47 * i).Value = "3学期"
       If i = 3 Then DataGridView1(0, 47 * i).Value = "1年間"

       '列見出しを入れる。
       DataGridView1(0, 1 + 47 * i).Value = "番号 "
       DataGridView1(1, 1 + 47 * i).Value = "国語 "
       DataGridView1(2, 1 + 47 * i).Value = "社会 "
       DataGridView1(3, 1 + 47 * i).Value = "数学 "
       DataGridView1(4, 1 + 47 * i).Value = "理科 "
       DataGridView1(5, 1 + 47 * i).Value = "英語 "
       DataGridView1(6, 1 + 47 * i).Value = "合計 "
       DataGridView1(7, 1 + 47 * i).Value = "平均 "
       DataGridView1(8, 1 + 47 * i).Value = "最高点"
       DataGridView1(9, 1 + 47 * i).Value = "最低点"
       DataGridView1(10, 1 + 47 * i).Value = "順位 "
       DataGridView1(11, 1 + 47 * i).Value = "合否 "
       DataGridView1(12, 1 + 47 * i).Value = "講評 "

       '行見出し
       DataGridView1(0, 42 + 47 * i).Value = "合計 "
       DataGridView1(0, 43 + 47 * i).Value = "平均 "
       DataGridView1(0, 44 + 47 * i).Value = "最高点"
       DataGridView1(0, 45 + 47 * i).Value = "最低点"
     Next

     '出席番号とランダムデータの発生
     Dim a(3, 39, 6) As Integer, k As Integer
     For i = 0 To 3
       For j = 1 To 40
         a(i, j - 1, 0) = j
       Next
     Next
     For i = 0 To 2
       For j = 1 To 40
         For k = 1 To 5
           a(i, j - 1, k) = Int(101 * Rnd())
         Next
       Next
     Next
 
     '出席番号とランダムデータをDataGridViewに表示させる
     For i = 0 To 3
       For j = 1 To 40
         DataGridView1(0, i * 47 + j + 1).Value = a(i, j - 1, 0)
       Next
     Next
     For i = 0 To 2
       For j = 1 To 40
         For k = 1 To 5
           DataGridView1(k, i * 47 + j + 1).Value = a(i, j - 1, k)
         Next
       Next
    Next

    '各学期合計値・平均値の算出・表示
    For i = 0 To 2
      For j = 1 To 40
        a(i, j - 1, 6) = 0
        For k = 1 To 5
          a(i, j - 1, 6) = a(i, j - 1, 6) + a(i, j - 1, k)
        Next
        DataGridView1(6, i * 47 + j + 1).Value = a(i, j - 1, 6)
        DataGridView1(7, i * 47 + j + 1).Value = a(i, j - 1, 6) / 5
      Next
    Next

    '各学期各生徒の最高点・最低点の算出・表示
    Dim max As Integer, min As Integer
    For i = 0 To 2
      For j = 1 To 40
        max = 0
        min = 100
        For k = 1 To 5
          If a(i, j - 1, k) > max Then max = a(i, j - 1, k)
          If a(i, j - 1, k) < min Then min = a(i, j - 1, k)
        Next
        DataGridView1(8, i * 47 + j + 1).Value = max
        DataGridView1(9, i * 47 + j + 1).Value = min
      Next
    Next

    '各学期合否・講評の算出・表示
    For i = 0 To 2
      For j = 1 To 40
        If a(i, j - 1, 6) >= 250 Then DataGridView1(11, i * 47 + j + 1).Value = "合格"
        If a(i, j - 1, 6) < 250 Then DataGridView1(11, i * 47 + j + 1).Value = "不合格"
      Next
      For j = 1 To 40
        If a(i, j - 1, 6) >= 350 Then DataGridView1(12, i * 47 + j + 1).Value = "上位合格です。"
        If a(i, j - 1, 6) < 350 And a(i, j - 1, 6) >= 280 Then DataGridView1(12, i * 47 + j + 1).Value = "余裕で合格です。"
        If a(i, j - 1, 6) < 280 And a(i, j - 1, 6) >= 250 Then DataGridView1(12, i * 47 + j + 1).Value = "ぎりぎり合格です。"
        If a(i, j - 1, 6) < 250 And a(i, j - 1, 6) >= 230 Then DataGridView1(12, i * 47 + j + 1).Value = "合格まで後一歩です。"
        If a(i, j - 1, 6) < 230 Then DataGridView1(12, i * 47 + j + 1).Value = "よく勉強して再挑戦!"
      Next
    Next

    '各学期各教科の合計・平均の算出・表示
    For i = 0 To 2
      For j = 1 To 6
        w = 0
        For k = 1 To 40
          w = w + a(i, k - 1, j)
        Next
        DataGridView1(j, i * 47 + 42).Value = w
        DataGridView1(j, i * 47 + 43).Value = w / 40
      Next
      DataGridView1(7, i * 47 + 42).Value = DataGridView1(6, i * 47 + 42).Value / 5
      DataGridView1(7, i * 47 + 43).Value = DataGridView1(7, i * 47 + 42).Value / 40
    Next

    '各学期各教科の最高点・算出の算出・表示
     For i = 0 To 2
      For j = 1 To 6
        max = 0
        min = 500
        For k = 1 To 40
          If a(i, k - 1, j) > max Then max = a(i, k - 1, j)
          If a(i, k - 1, j) < min Then min = a(i, k - 1, j)
        Next
        DataGridView1(j, i * 47 + 44).Value = max
        DataGridView1(j, i * 47 + 45).Value = min
      Next
    Next

    '各学期の成績順並び替え
     Dim bn As Integer, w1 As String
     For i = 0 To 2
       For j = 40 To 2 Step -1
         min = 500
         For k = 1 To j
           If a(i, k - 1, 6) < min Then
             min = a(i, k - 1, 6)
             bn = k
           End If
        Next
        For k = 0 To 6
           w = a(i, bn - 1, k)
           a(i, bn - 1, k) = a(i, j - 1, k)
           a(i, j - 1, k) = w
        Next
        For k = 0 To 12
           w1 = DataGridView1(k, i * 47 + bn + 1).Value
           DataGridView1(k, i * 47 + bn + 1).Value = DataGridView1(k, i * 47 + j + 1).Value
           DataGridView1(k, i * 47 + j + 1).Value = w1
        Next
      Next
    Next


    '各学期順位付け
    For i = 0 To 2
      For j = 1 To 40
        DataGridView1(10, i * 47 + j + 1).Value = j
      Next
      For j = 2 To 41
        If a(i, j - 1, 6) = a(i, j - 2, 6) Then DataGridView1(10, i * 47 + j + 1).Value = DataGridView1(10, i * 47 + j).Value
      Next
    Next

    '各学期出席番号順並び替え
    For i = 0 To 2
      For j = 40 To 2 Step -1
        max = 0
        For k = 1 To j
          
If a(i, k - 1, 6) < min Then
            min = a(i, k - 1, 6)

            bn = k
          End If
        Next
        For k = 0 To 6
          w = a(i, bn - 1, k)
          a(i, bn - 1, k) = a(i, j - 1, k)
          a(i, j - 1, k) = w
        Next
        For k = 0 To 12
          w1 = DataGridView1(k, i * 47 + bn + 1).Value
          DataGridView1(k, i * 47 + bn + 1).Value = DataGridView1(k, i * 47 + j + 1).Value
          DataGridView1(k, i * 47 + j + 1).Value = w1
        Next
      Next
    Next


   End Sub

End Class

実行結果例
3次元配列

3次元配列、3次元ループとなって超難解になってしまいました。
難しくなりすぎてごめんなさい。
今回の課題が卒業研究ぐらいでもよかったですね。

解説は次話で行います。
課題を出しておきましょう。
今回の解答例では、3学期まで処理してありますが、
年間処理を行っていません。
各教科のデータは3学期の平均とします。
年間処理を行いましょう。





第7話へ 第9話へ

006

VC++講義第1部へ
vb講義へ
VB講義基礎へ
初心者のための世界で一番わかりやすいVisual C++入門基礎講座

数学研究室に戻る