第5講 配列の学習

第4話 2次元配列の和・積
Form1例
q
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, a(9, 9) As Integer, w As String

     '2次元データの発生
     For i = 0 To 9
       For j = 0 To 9
         a(i, j) = 10 * i + j
       Next
     Next
     For i = 0 To 9
       For j = 0 To 9
         b(i, j) = Int(100 * Rnd())
       Next
     Next

     '2次元データの表示
     w = ""
     For i = 0 To 9
       For j = 0 To 9
         If a(i, j) < 10 Then
           w = w + "0" + a(i, j).ToString() + " "
         Else
           w = w + a(i, j).ToString() + " "
         End If
       Next
       w = w & vbNewLine
     Next
     Label3.Text = w
     w = ""
     For i = 0 To 9
       For j = 0 To 9
         If b(i, j) < 10 Then
           w = w + "0" + b(i, j).ToString() + " "
         Else
           w = w + b(i, j).ToString() + " "
         End If
       Next
       w = w & vbNewLine
     Next
     Label4.Text = w

     '和の表示
     w = ""
     For i = 0 To 9
       For j = 0 To 9
         If (a(i, j) + b(i, j)) < 10 Then
            w = w + "00" + (a(i, j) + b(i, j)).ToString() + " "
         Else
            If (a(i, j) + b(i, j)) < 100 Then
               w = w + "0" + (a(i, j) + b(i, j)).ToString() + " "
            Else
               w = w + (a(i, j) + b(i, j)).ToString() + " "
            End If
         End If
       Next
       w = w & vbNewLine
     Next
     Label7.Text = w

     '積の表示
     w = ""
     For i = 0 To 9
       For j = 0 To 9
         If (a(i, j) * b(i, j)) < 10 Then
           w = w + "000" + (a(i, j) * b(i, j)).ToString() + " "
         Else
           If (a(i, j) * b(i, j)) < 100 Then
             w = w + "00" + (a(i, j) * b(i, j)).ToString() + " "
           Else
             If (a(i, j) * b(i, j)) < 1000 Then
               w = w + "0" + (a(i, j) * b(i, j)).ToString() + " "
             Else
               w = w + (a(i, j) * b(i, j)).ToString() + " "
             End If
          End If
        End If
      Next
      w = w & vbNewLine
    Next
    Label8.Text = w


   End Sub
End Class

やはり、
         If (a(i, j) * b(i, j)) < 10 Then
           w = w + "000" + (a(i, j) * b(i, j)).ToString() + " "
         Else
           If (a(i, j) * b(i, j)) < 100 Then
             w = w + "00" + (a(i, j) * b(i, j)).ToString() + " "
           Else
             If (a(i, j) * b(i, j)) < 1000 Then
               w = w + "0" + (a(i, j) * b(i, j)).ToString() + " "
             Else
               w = w + (a(i, j) * b(i, j)).ToString() + " "
             End If
          End If
        End If       
の部分はわかりにくいですね。
すべて肯定表現にするとどうなりますか。
30行下に模範解答。



























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, a(9, 9) As Integer, w As String

     '2次元データの発生
     For i = 0 To 9
       For j = 0 To 9
         a(i, j) = 10 * i + j
       Next
     Next
     For i = 0 To 9
       For j = 0 To 9
         b(i, j) = Int(100 * Rnd())
       Next
     Next

     '2次元データの表示
     w = ""
     For i = 0 To 9
       For j = 0 To 9
         If a(i, j) < 10 Then
           w = w + "0" + a(i, j).ToString() + " "
         Else
           w = w + a(i, j).ToString() + " "
         End If
       Next
       w = w & vbNewLine
     Next
     Label3.Text = w
     w = ""
     For i = 0 To 9
       For j = 0 To 9
         If b(i, j) < 10 Then
           w = w + "0" + b(i, j).ToString() + " "
         Else
           w = w + b(i, j).ToString() + " "
         End If
       Next
       w = w & vbNewLine
     Next
     Label4.Text = w

     '和の表示
     w = ""
     For i = 0 To 9
       For j = 0 To 9
         
If (a(i, j) + b(i, j)) < 10 Then w = w + "00" + (a(i, j) + b(i, j)).ToString() + " "
         If (a(i, j) + b(i, j)) >= 10 And (a(i, j) + b(i, j)) < 100 Then w = w + "0" + (a(i, j) + b(i, j)).ToString() + " "
         If (a(i, j) + b(i, j)) >= 100 And (a(i, j) + b(i, j)) < 1000 Then w = w + (a(i, j) + b(i, j)).ToString() + " "

       Next
       w = w & vbNewLine
     Next
     Label7.Text = w

     '積の表示
     w = ""
     For i = 0 To 9
       For j = 0 To 9
         
If (a(i, j) * b(i, j)) < 10 Then w = w + "000" + (a(i, j) * b(i, j)).ToString() + " "
         If (a(i, j) * b(i, j)) >= 10 And (a(i, j) * b(i, j)) < 100 Then w = w + "00" + (a(i, j) * b(i, j)).ToString() + " "
         If (a(i, j) * b(i, j)) >= 100 And (a(i, j) * b(i, j)) < 1000 Then w = w + "0" + (a(i, j) * b(i, j)).ToString() + " "
         If (a(i, j) * b(i, j)) >= 1000 And (a(i, j) * b(i, j)) < 10000 Then w = w + (a(i, j) * b(i, j)).ToString() + " "

      Next
      w = w & vbNewLine
    Next
    Label8.Text = w


   End Sub
End Class

課題を出してこの話を閉めます。
第4講で作った成績一覧表の合計値・平均値・最大値・最小値・並び替えを
DataGridView1(*,*)を使わないで実現してみましょう。
もちろん表示させるときにはDataGridView1(*,*)を使うという条件ですよ。
つまり、
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

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

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

     '41行から44行 行見出し
     DataGridView1(0, 41).Value = "合計 "
     DataGridView1(0, 42).Value = "平均 "
     DataGridView1(0, 43).Value = "最高点"
     DataGridView1(0, 44).Value = "最低点"


     '出席番号とランダムデータの発生
     For i = 1 To 40
       DataGridView1(0, i).Value = i
       For j = 1 To 5
         DataGridView1(j, i).Value = Int(101 * Rnd())
       Next
     Next

    
'各生徒の5教科合計・平均・合否・講評の算出と表示
     For i = 1 To 40
       '合計・平均の算出
       w = 0
       For j = 1 To 5
         w = w + DataGridView1(j, i).Value
       Next
       '合計・平均の表示
       DataGridView1(6, i).Value = w
       DataGridView1(7, i).Value = w / 5
       '合否の表示
       If w >= 250 Then
         DataGridView1(10, i).Value = "合格"
       Else
         DataGridView1(10, i).Value = "不合格"
       End If
       '講評の表示
       If w >= 350 Then
         DataGridView1(11, i).Value = "上位合格です。"
       Else
         If w >= 280 Then
           DataGridView1(11, i).Value = "余裕で合格です。"
         Else
           If w >= 250 Then
             DataGridView1(11, i).Value = "ぎりぎり合格です。"
           Else
             If w >= 230 Then
                DataGridView1(11, i).Value = "合格まで後一歩。"
             Else
                DataGridView1(11, i).Value = "よく勉強して再挑戦!"
             End If
           End If
         End If
       End If
     Next

     '教科合計・平均の算出と表示
     For i = 1 To 5
       '合計・平均の算出
       w = 0
       For j = 1 To 40
         w = w + DataGridView1(i, j).Value
       Next
       '合計・平均の表示
       DataGridView1(i, 41).Value = w
       DataGridView1(i, 42).Value = w / 40
     Next

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

     
Next

     '各教科の最高点と最低点の算出と表示
     For i = 1 To 5
       max = 0
       min = 100
       For j = 1 To 40
         If DataGridView1(i, j).Value > max Then max = DataGridView1(i, j).Value
         If DataGridView1(i, j).Value < min Then min = DataGridView1(i, j).Value
       Next
       DataGridView1(i, 43).Value = max
       DataGridView1(i, 44).Value = min

     
Next

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

         
DataGridView1(j, bn).Value = DataGridView1(j, i).Value
         
DataGridView1(j, i).Value = w1
     
  Next
     Next


   
  '順位付け
     For i = 1 To 40
       DataGridView1(10, i).Value = i
     Next
     For i = 2 To 40
       If DataGridView1(6, i - 1).Value = DataGridView1(6, i).Value Then DataGridView1(10, i).Value = DataGridView1(10, i - 1).Value
     Next

     '出席番号順並び替え
     For i = 40 To 2 Step -1
       max = 0
       For j = 1 To i
         If DataGridView1(0, j).Value > max Then
           max = DataGridView1(0, j).Value
           bn = j
         End If
       Next
       For j = 0 To 12
         w1 = DataGridView1(j, bn).Value
         DataGridView1(j, bn).Value = DataGridView1(j, i).Value
         DataGridView1(j, i).Value = w1
      Next
    Next


   End Sub

End Class

の青色部分のデータ処理部分(合計や順付けなど)を配列で処理し、それをDataGridViewに表示するように変更してください。
用意する配列は、Dim a(39,6) As Integer とし、列ではAからFまで(すなわち出席番号から合計値まで)、行では2行目から41行目まで(つまり出席番号1から40まで)
を配列にコピーして処理して下さい。



第3話へ 第5話へ

006

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

数学研究室に戻る