Vb编程:输入5*5矩阵,求出每列元素之和,并把最大值的那一列上的个元素和第一列上个元素进行对调。救急!
展开全部
Private Sub Command1_Click()
Dim a(5, 5) As Integer
Dim b(5) As Integer
Dim i As Integer, j As Integer
Dim tMax As Long, tt As Long
Dim maxColNum As Long
tMax = 0
tt = 0
maxColNum = 0
For i = 0 To 4
For j = 0 To 4
a(j, i) = Val(InputBox("输入矩阵数据:第" & i + 1 & "列,第" & j + 1 & "行:", ""))
tt = tt + a(j, i)
If tt > tMax Then
tMax = tt
maxColNum = i
End If
Next j
Next i
'对调
If maxColNum > 0 Then
For i = 0 To 4
b(i) = a(i, 0)
a(i, 0) = a(i, maxColNum)
a(i, maxColNum) = b(i)
Next
End If
'输出
For i = 0 To 4
For j = 0 To 4
Me.Print a(i, j) & ",";
Next j
Me.Print
Next i
End Sub
Dim a(5, 5) As Integer
Dim b(5) As Integer
Dim i As Integer, j As Integer
Dim tMax As Long, tt As Long
Dim maxColNum As Long
tMax = 0
tt = 0
maxColNum = 0
For i = 0 To 4
For j = 0 To 4
a(j, i) = Val(InputBox("输入矩阵数据:第" & i + 1 & "列,第" & j + 1 & "行:", ""))
tt = tt + a(j, i)
If tt > tMax Then
tMax = tt
maxColNum = i
End If
Next j
Next i
'对调
If maxColNum > 0 Then
For i = 0 To 4
b(i) = a(i, 0)
a(i, 0) = a(i, maxColNum)
a(i, maxColNum) = b(i)
Next
End If
'输出
For i = 0 To 4
For j = 0 To 4
Me.Print a(i, j) & ",";
Next j
Me.Print
Next i
End Sub
展开全部
做了一个类 Matrix.cls
Option Explicit
Dim mMatrix(0 To 4, 0 To 4) As Double
Dim mSum(0 To 4) As Double
Public Sub InitMatrix()
Dim i As Integer
Dim j As Integer
For i = 0 To 4
For j = 0 To 4
mMatrix(i, j) = i + j
Next j
Next i
End Sub
Public Sub GetMSum()
Dim i As Integer
Dim j As Integer
For i = 0 To 4
mSum(i) = 0
For j = 0 To 4
mSum(i) = mSum(i) + mMatrix(j, i)
Next j
Next i
End Sub
Public Sub ChgMatrix()
'get max column
Dim i As Integer
Dim iMaxIdx As Integer
Dim dMax As Double
dMax = mSum(0)
For i = 0 To 4
If mSum(i) > dMax Then
iMaxIdx = i
dMax = mSum(i)
End If
Next i
Debug.Print "max = " & iMaxIdx
'exchange the column with first column
Dim j As Integer
Dim dTemp As Double
For i = 0 To 4
dTemp = mMatrix(i, iMaxIdx)
mMatrix(i, iMaxIdx) = mMatrix(i, 0)
mMatrix(i, 0) = dTemp
Next i
End Sub
Public Sub PrintMatrix()
Dim i As Integer
Dim j As Integer
Dim sLine As String
Debug.Print "print matrix====== "
For i = 0 To 4
sLine = ""
For j = 0 To 4
sLine = sLine & mMatrix(i, j) & " "
Next j
Debug.Print sLine
Next i
End Sub
Public Sub PrintSum()
Dim i As Integer
Dim j As Integer
Dim sLine As String
Debug.Print "print Sum====== "
sLine = ""
For i = 0 To 4
sLine = sLine & mSum(i) & " "
Next i
Debug.Print sLine
End Sub
调用该类
Private Sub Command1_Click()
Dim mMatrix As New Matrix
mMatrix.InitMatrix
mMatrix.PrintMatrix
mMatrix.GetMSum
mMatrix.PrintSum
mMatrix.ChgMatrix
mMatrix.PrintMatrix
End Sub
结果输出
print matrix======
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
print Sum======
10 15 20 25 30
max = 4
print matrix======
4 1 2 3 0
5 2 3 4 1
6 3 4 5 2
7 4 5 6 3
8 5 6 7 4
Option Explicit
Dim mMatrix(0 To 4, 0 To 4) As Double
Dim mSum(0 To 4) As Double
Public Sub InitMatrix()
Dim i As Integer
Dim j As Integer
For i = 0 To 4
For j = 0 To 4
mMatrix(i, j) = i + j
Next j
Next i
End Sub
Public Sub GetMSum()
Dim i As Integer
Dim j As Integer
For i = 0 To 4
mSum(i) = 0
For j = 0 To 4
mSum(i) = mSum(i) + mMatrix(j, i)
Next j
Next i
End Sub
Public Sub ChgMatrix()
'get max column
Dim i As Integer
Dim iMaxIdx As Integer
Dim dMax As Double
dMax = mSum(0)
For i = 0 To 4
If mSum(i) > dMax Then
iMaxIdx = i
dMax = mSum(i)
End If
Next i
Debug.Print "max = " & iMaxIdx
'exchange the column with first column
Dim j As Integer
Dim dTemp As Double
For i = 0 To 4
dTemp = mMatrix(i, iMaxIdx)
mMatrix(i, iMaxIdx) = mMatrix(i, 0)
mMatrix(i, 0) = dTemp
Next i
End Sub
Public Sub PrintMatrix()
Dim i As Integer
Dim j As Integer
Dim sLine As String
Debug.Print "print matrix====== "
For i = 0 To 4
sLine = ""
For j = 0 To 4
sLine = sLine & mMatrix(i, j) & " "
Next j
Debug.Print sLine
Next i
End Sub
Public Sub PrintSum()
Dim i As Integer
Dim j As Integer
Dim sLine As String
Debug.Print "print Sum====== "
sLine = ""
For i = 0 To 4
sLine = sLine & mSum(i) & " "
Next i
Debug.Print sLine
End Sub
调用该类
Private Sub Command1_Click()
Dim mMatrix As New Matrix
mMatrix.InitMatrix
mMatrix.PrintMatrix
mMatrix.GetMSum
mMatrix.PrintSum
mMatrix.ChgMatrix
mMatrix.PrintMatrix
End Sub
结果输出
print matrix======
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
print Sum======
10 15 20 25 30
max = 4
print matrix======
4 1 2 3 0
5 2 3 4 1
6 3 4 5 2
7 4 5 6 3
8 5 6 7 4
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询