西北农林科技大学Vb第五章课后题

更新时间:2023-10-28 15:05:01 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

1.设有如下两组数据: (1)1,3,5,2,4,18,50,25

(2)5,27,30,35,60,41,87,33

编写一个程序,把上面两组数据分别读入两个数组中,然后把两个数组中对应下标的元素相加,即1+5,3+27,……,25+33,并把相应的结果放入第三个数组中,最后输出第三个数组的值。 Private Sub Command1_Click() Dim i Dim x() Dim y()

Dim z(0 To 7)

x = Array(1, 3, 5, 2, 4, 18, 50, 25) y = Array(5, 27, 30, 35, 60, 41, 87, 33) For i = 0 To 7 z(i) = x(i) + y(i) Print z(i); Next i End Sub

2. 有一个n*m的矩阵,编写程序,找出其中最大的那个元素所在的行和列,并输出其值及行号和列号。 Option Base 1

Private Sub Command1_Click() Dim a%(), b%()

m = InputBox(\请输入行数m\n = InputBox(\请输入列数n\ReDim a%(m, n) For i = 1 To m For j = 1 To n

a(i, j) = Int(Rnd * 100) Next j Next i

For i = 1 To m For j = 1 To n

Print Tab(6 * (k Mod n) + 3); a(i, j); k = k + 1 Next j Next i Print

ReDim b%(m, n) x = 1: y = 1 For i = 1 To m For j = 1 To n

If b(x, y) < a(i, j) Then

x = i: y = j

b(x, y) = a(i, j) End If Next j Next i

Print \最大的数是\它在第\列,第\行\End Sub

3.某数组中有10个元素,元素的值由键盘输入,要求将前5个元素与后5个元素对换。即第1个元素与第10个元素互换,第2个元素与第9个元素互换……。请输出数组中原来各元素的值和对换后各元素的值。 Option Base 1

Private Sub Command1_Click() Dim a%(1 To 10) Dim i%

For i = 1 To 10

a(i) = InputBox(\请输入第\个数\Print a(i); Next i Print

For i = 1 To 5 t = a(i)

a(i) = a(10 - i + 1) a(10 - i + 1) = t Print a(i); Next i

For i = 1 To 5 Print a(i + 5); Next i End Sub

4.输入若干个学生的一门课的成绩,统计各分数段的人数。按小于60分、60~69分、70~79分、80~89分、90~100分各为一个分数段。 Private Sub Command1_Click() Dim a$(), k%, y

Dim x(0 To 10) As Integer a = Split(Text1.Text, \For Each y In a k = y \\ 10

x(k) = x(k) + 1 Next y

Picture1.Print \统计结果如下\

Picture1.Print \分有:\人\

For i = 8 To 6 Step -1

Picture1.Print i * 10 & \分—\分有\人\Next i

For i = 1 To 5 z = z + x(i) Next i

Picture1.Print \分以下的有:\人\End Sub

5.我国身份证号码的第18位是由前17位通过公式计算出来的,请编程: 要求从键盘上输入其一身份证号码的前17位,求出第18位 Private Sub Command1_Click() Dim W(), A(0 To 16), s%, y% For i = 0 To 16

W = Array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2) A(i) = InputBox(\请输入第\个数\Print A(i);

s = s + A(i) * W(i) Next i Print

y = s Mod 11 y = y + 1

m = Choose(y, \Print \第十八位是:\End Sub

6.有一个数列,前两项为1,从第三项开始,每一项都是前现两项之和,输出该数列前30项。 Option Base 1

Private Sub Command1_Click() Dim a!(1 To 30) a(1) = 1: a(2) = 2 Print a(1); a(2) For i = 3 To 30

a(i) = a(i - 1) + a(i - 2) Print a(i); Next i End Sub

7.随机产生15个不重复的A~Z之间的大写字母,存放在字符数组中。 Private Sub Command1_Click() Dim a(1 To 15) For i = 1 To 15

a(i) = Int(Rnd * 26) + 65 Next i

For i = 1 To 14 For j = i + 1 To 15

If a(j) = a(i) Then a(j) = Int(Rnd * 26) + 65 Next j

Print Chr(a(i)); Next i End Sub

8.将从键盘上输入的一个数插入到数组指定位置。 Private Sub Command1_Click() Dim a(), l%, u% Dim x%, k%

a = Array(1, 2, 3, 4, 5, 6, 7, 9, 10) l = LBound(a) u = UBound(a) For i = l To u Print a(i); Next i Print

k = InputBox(\请输入要插入的位置\x = InputBox(\请输入要插入的数\ReDim Preserve a(l To u + 1) For i = u To k Step -1 k = i

a(i + 1) = a(i) Next i a(k) = x

For i = l To u + 1 Print a(i); Next i End Sub

9.在数组中查找从键盘上输入的一个数,如果找不到输出相应信息,如果找到,则删除该元素。

Private Sub Command1_Click()

Dim a(), p As Boolean, x%, l%, u%, i%, k% a = Array(1, 5, 8, 9, 4, 6, 3, 7) l = LBound(a): u = UBound(a) For i = l To u Print a(i); Next i Print

x = Val(InputBox(\请输入要删除的数\For i = l To u If x = a(i) Then p = True: k = i Exit For End If Next i

If p = True Then For i = k + 1 To u a(i - 1) = a(i) Next i u = u - 1

ReDim Preserve a(l To u) For i = l To u Print a(i); Next i Else

MsgBox \未找到!\End If End Sub

10.求一个M行N列的矩阵四周元素之和,元素值可以随机产生,M、N从键盘输入。

Private Sub Command1_Click()

Dim a%(), m%, n%, i%, j%, b%, c%, d%, e%, f%

m = InputBox(\请输入该矩阵的行数\n = InputBox(\请输入该矩阵的列数\ReDim a%(1 To m, 1 To n) For i = 1 To m For j = 1 To n

a(i, j) = Int(Rnd * 11)

Print Tab((j - 1) * 6 + 3); a(i, j); Next j Next i

For j = 1 To n b = b + a(1, j) e = e + a(m, j) Next j

For i = 2 To m - 1 c = c + a(i, 1) d = d + a(i, n) Next i

f = b + c + d + e Print Print f End Sub

本文来源:https://www.bwwdw.com/article/rre2.html

Top