求VB高手帮做两道题!
1。在一个长度为10的一维有序整数X中插入一个元素。设数组元素初始值只有9个,插入后数组各元素仍然有序。2。输入一个3行4列的二维数组,分别求出每一行、每一列元素之和。...
1。在一个长度为10的一维有序整数X中插入一个元素。设数组元素初始值只有9个,插入后数组各元素仍然有序。
2。输入一个3行4列的二维数组,分别求出每一行、每一列元素之和。 展开
2。输入一个3行4列的二维数组,分别求出每一行、每一列元素之和。 展开
2个回答
展开全部
1,
Private Sub Command1_Click()
Dim a(1 To 10) As Integer, i%, j%, num%
For i = 1 To 9 '生成9个有序数
a(i) = i * 2
Print CStr(a(i)) + " ";
Next
Print
Randomize
num = Int(Rnd * 18) '生成要插入的数
Print "插入" + CStr(num)
For i = 1 To 10 '此循环依次比较数列中的数和要插入的数
If num < a(i) Then
For j = 9 To i Step -1 '此循环将大于num的数依次后移一位
a(j + 1) = a(j)
Next
a(i) = num
Exit For
End If
Next
For i = 1 To 10
Print CStr(a(i)) + " ";
Next
End Sub
2,
Private Sub Command1_Click()
Dim a(2, 3) As Integer, i%, j%, sOfColumn%, sOfRow%
For i = 0 To 2
For j = 0 To 3
Randomize
a(i, j) = Int(Rnd * 100) '随机生成数组的各元素
Print CStr(a(i, j)) + vbTab;
Next
Print
Next
For i = 0 To 2
For j = 0 To 3
sOfColumn = sOfColumn + a(i, j) '求各行数值和
Next
Print "第" + CStr(i) + "行数值和" + CStr(sOfColumn)
sOfColumn = 0
Next
For j = 0 To 3
For i = 0 To 2
sOfRow = sOfRow + a(i, j) '求各列数值和
Next
Print "第" + CStr(j) + "列数值和" + CStr(sOfRow)
sOfRow = 0
Next
End Sub
Private Sub Command1_Click()
Dim a(1 To 10) As Integer, i%, j%, num%
For i = 1 To 9 '生成9个有序数
a(i) = i * 2
Print CStr(a(i)) + " ";
Next
Randomize
num = Int(Rnd * 18) '生成要插入的数
Print "插入" + CStr(num)
For i = 1 To 10 '此循环依次比较数列中的数和要插入的数
If num < a(i) Then
For j = 9 To i Step -1 '此循环将大于num的数依次后移一位
a(j + 1) = a(j)
Next
a(i) = num
Exit For
End If
Next
For i = 1 To 10
Print CStr(a(i)) + " ";
Next
End Sub
2,
Private Sub Command1_Click()
Dim a(2, 3) As Integer, i%, j%, sOfColumn%, sOfRow%
For i = 0 To 2
For j = 0 To 3
Randomize
a(i, j) = Int(Rnd * 100) '随机生成数组的各元素
Print CStr(a(i, j)) + vbTab;
Next
Next
For i = 0 To 2
For j = 0 To 3
sOfColumn = sOfColumn + a(i, j) '求各行数值和
Next
Print "第" + CStr(i) + "行数值和" + CStr(sOfColumn)
sOfColumn = 0
Next
For j = 0 To 3
For i = 0 To 2
sOfRow = sOfRow + a(i, j) '求各列数值和
Next
Print "第" + CStr(j) + "列数值和" + CStr(sOfRow)
sOfRow = 0
Next
End Sub
展开全部
要考试了,一起多多练习吧。。
第一个题,
Dim a()
Dim i, j As Integer
Dim n, m As Integer
Private Sub Form_Load()
m = 9
End Sub
Private Sub Command1_Click()
Cls
ReDim a(m)
Randomize
For i = 0 To 9
a(i) = Int(Rnd * 900) + 100
Next
Print Join(a);
End Sub
Private Sub Command2_Click()
n = Int(InputBox("插入一个数"))
m = m + 1
ReDim Preserve a(m)
a(m) = n
For i = 0 To m - 1
For j = m To i + 1 Step -1
If a(j) < a(j - 1) Then
t = a(j)
a(j) = a(j - 1)
a(j - 1) = t
End If
Next
Next
Print
Print Join(a);
End Sub
第二个,
Dim a(2, 3) As Integer, sum As Integer
Dim s As String
Private Sub Command1_Click()
Cls
For i = 0 To 2
For j = 0 To 3
Randomize
a(i, j) = Int(Rnd * 100)
Print a(i, j);
Next
Print
Next
End Sub
Private Sub Command2_Click()
For i = 0 To 2
sum = 0
For j = 0 To 3
sum = sum + a(i, j)
Next
Print
Print "第" & i + 1 & "行和" & sum;
Next
End Sub
Private Sub Command3_Click()
For j = 0 To 3
sum = 0
For i = 0 To 2
sum = sum + a(i, j)
Next
Print
Print "第" & j + 1 & "列和" & sum;
Next
End Sub
第一个题,
Dim a()
Dim i, j As Integer
Dim n, m As Integer
Private Sub Form_Load()
m = 9
End Sub
Private Sub Command1_Click()
Cls
ReDim a(m)
Randomize
For i = 0 To 9
a(i) = Int(Rnd * 900) + 100
Next
Print Join(a);
End Sub
Private Sub Command2_Click()
n = Int(InputBox("插入一个数"))
m = m + 1
ReDim Preserve a(m)
a(m) = n
For i = 0 To m - 1
For j = m To i + 1 Step -1
If a(j) < a(j - 1) Then
t = a(j)
a(j) = a(j - 1)
a(j - 1) = t
End If
Next
Next
Print Join(a);
End Sub
第二个,
Dim a(2, 3) As Integer, sum As Integer
Dim s As String
Private Sub Command1_Click()
Cls
For i = 0 To 2
For j = 0 To 3
Randomize
a(i, j) = Int(Rnd * 100)
Print a(i, j);
Next
Next
End Sub
Private Sub Command2_Click()
For i = 0 To 2
sum = 0
For j = 0 To 3
sum = sum + a(i, j)
Next
Print "第" & i + 1 & "行和" & sum;
Next
End Sub
Private Sub Command3_Click()
For j = 0 To 3
sum = 0
For i = 0 To 2
sum = sum + a(i, j)
Next
Print "第" & j + 1 & "列和" & sum;
Next
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询