谁可以帮我看下这个vb的小程序呀
要求:
(1)将两个矩阵相乘,结果放入矩阵C中(注意矩阵的乘法规则)
(2)统计矩阵C中最大值和下标以及最小值和下标
(3)求矩阵A两条对角线元素的平均值
(4)将矩阵A按列的次序把各元素放入一维数组D中,显示结果。
下面是我自己做的,可是矩阵AB都显示不出来,所以后面也无法检验....
Option Explicit
Private Sub Command1_Click()
Dim a(4, 4), b(4, 4), c(4, 4), d(16) As Integer
Dim i%, j%, s%, max%, min%, ah%, ih%, il%, al%, m%, n%, t%, k%
For i = 1 To 4
For j = 1 To 4
Randomize
s = a(i, j) = Int(Rnd * 9 + 1) '求随机矩阵A,放在text1中
Text1.Text = s
s = b(i, j) = Int(Rnd * 20 + 10) '求随机矩阵B,放在text2中
Text2.Text = s
Next j
Text1.Text = s
Text2.Text = s
Next i
For i = 1 To 4
For j = 1 To 4
m = 0
For n = 1 To 4 '随机矩阵相乘得C,放在text3中
m = m + a(i, n) * b(n, j)
Next n
c(i, j) = m
Next j
Next
Text3.Text = m
ah = 1 '矩阵C的最大值和下标分别放在text4,text5中
al = 1
ih = 1
il = 1
max = c(1, 1)
min = c(1, 1)
For i = 1 To 4
For j = 1 To 4
If c(i, j) > max Then
max = c(i, j)
ah = i
al = j
End If
If c(i, j) < min Then '矩阵C的最小值和下标分别放在text6 , text7中
min = c(i, j)
ih = i
il = j
End If
Next j
Next i
Text4.Text = str(max)
Text5.Text = str(ah) + "," + str(al)
Text6.Text = str(max)
Text7.Text = str(ih) + "," + str(il)
t = 0 '矩阵A对角线元素数值之平均值放在text8中
For i = 1 To 4
For j = 1 To 4
If (i = j) Or (i + j = 4) Then t = t + a(i, j)
Next j
Next
t = Int(t \ 7)
Text8.Text = t
For i = 1 To 4 '将矩阵A的元素按列的次序数排列在text9中
For j = 1 To 4
k = (i - 1) * 4 + j
Text9.Text = d(k)
Next j
Next i
End Sub 展开
'帮你改了,不过,由于习惯问题,连变量名也改了:)
'你自己对比着看,看看你的哪错了吧
'我比较习惯用label来显示不需要改动的量,
'label1,2,3分别显示数组a,b,c三个矩阵。按行顺序显示。
'加“,”号区分各数,方便查看。
'label4,5显示最大,小数:max,min
'label6显示矩阵a的对角平均数
’label7按顺序显示一维数组d()
'切记,s = b(i, j) = Int(Rnd * 20 + 10)这种写法的话,
's赋不了值的,为0。VB里只能是老老实实分开来写成
'b(i,j)=int(rnd*20+10)
's=b(i,j)
Private Sub Command1_Click()
Dim a(4, 4), b(4, 4), c(4, 4), d(16) As Integer
Dim i, j, n, max, min, maxi, maxj, mini, minj As Integer
'i,j,k作为循环变量,max,min,存放最大/小数,maxi,maxj,mini,minj分别
'存放最大/小数的下标
Dim s As Double
's作为临时变量,由于要算平均数,故定义为double,连小数一起要了
Label1.Caption = " "
Label2.Caption = " "
Label3.Caption = " "
'以下初始化矩阵a,b
For i = 1 To 4
For j = 1 To 4
Randomize
a(i, j) = Int(Rnd * 9 + 1) '求随机矩阵A,放在label1中
Label1.Caption = Label1.Caption & Str(a(i, j)) & ","
b(i, j) = Int(Rnd * 20 + 10) '求随机矩阵B,放在label2中
Label2.Caption = Label2.Caption & Str(a(i, j)) & ","
Next j
Next i
'以下做a,b的乘法,得矩阵c
For i = 1 To 4
For j = 1 To 4
s = 0
For n = 1 To 4 '随机矩阵相乘得C,放在label3中
s = s + a(i, n) * b(n, j)
Next n
c(i, j) = s
Label3.Caption = Label3.Caption & Str(c(i, j)) & ","
Next j
Next i
'以下找出矩阵c中最大,小数
max = c(1, 1)
min = c(1, 1)
maxi = 1
maxj = 1
mini 局滚察= 1
minj = 1
For i = 1 桐茄To 4
For j = 1 To 4
If c(i, j) > max Then
max = c(i, j)
maxi = i
maxj = j
End If
If c(i, j) < min 备吵Then '矩阵C的最小值和下标分别放在label6 , label7中
min = c(i, j)
mini = i
minj = j
End If
Next j
Next i
'输出最大,小数
Label4.Caption = "Max:c(" & Str(maxi) & "," & Str(maxj) & ")=" & Str(c(maxi, maxj))
Label5.Caption = "Min:c(" & Str(mini) & "," & Str(minj) & ")=" & Str(c(mini, minj))
'以下计算对角线平均数并输出在label6中
s = 0
For i = 1 To 4
For j = 1 To 4
If (i = j) Or (i + j = 4) Then s = s + a(i, j)
Next j
Next
s = s / 7
Label6.Caption = s
Label7.Caption = ""
For i = 1 To 4 '以下按列序化二维数组为一维数组,输出label7中
For j = 1 To 4
n = (i - 1) * 4 + j
d(n) = a(j, i)
Label7.Caption = Label7.Caption & Str(d(n)) & ","
Next j
Next i
End Sub
'附了一个运行截图,祝好运.有问题,不妨留言到hi里