早教吧 育儿知识 作业答案 考试题库 百科 知识分享

vb:编制通用sub过程f3,将single类型一维数组反序排列(如实参数组元素依次为1,5,7,调用后为7,5,1PrivateSubf3(a()AsSingle,iAsByte)DimnAsByte,kAsByte,tempAsSingleFori=1Toktemp=a(i)a(i)=a(k+1-i)a(k+

题目详情
vb:编制通用sub过程f3,将single类型一维数组反序排列(如实参数组元素依次为1,5,7,调用后为7,5,1
Private Sub f3(a() As Single,i As Byte)
Dim n As Byte,k As Byte,temp As Single
For i = 1 To k
temp = a(i)
a(i) = a(k + 1 - i)
a(k + 1 - i) = temp
Next i
End Sub
Private Sub Form_Click()
Dim n As Byte,a() As Single,i As Byte
n = InputBox("请输入元素个数")
ReDim a(1 To n)
For i = 1 To n
a(i) = Val(InputBox("a(" & i & ")="))
Next i
Call f3(a,i)
For i = 1 To n
Print a(i);
Next i
End Sub
我写的不对,哪个地方错了啊?
▼优质解答
答案和解析
Private Sub f3(a() As Single, k As Byte) ‘原来定义的i在循环体内做循环变量用了
Dim n As Byte, i As Byte, temp As Single ’原来定义的k没有赋值
For i = 1 To k/2 '只要交换一半,全交换的话,又交换回来了.
temp = a(i)
a(i) = a(k + 1 - i)
a(k + 1 - i) = temp
Next i
End Sub
还有Call f3(a, i)
应改为Call f3(a, n),此时i已经为n+1了.