项目4数组习题答案

一、单选题

1.在http://m.wendangku.net/doc/ce0d4e3784254b35eefd34ec.html中,一组具有相同名字、不同下标的变量称为____________。

A.数组

B.变量

C.同类数据

D.同类变量

2.一维数组的大小为______________。

A.上界+下界+1

B.上界+下界-1

C.上界-下界+1

D.上界-下界-1

3.数组的存放是按____________存放的。

A.数据大小

B.数据类型

C.列

D.行

4.数组具有相同的_____________。

A.下标

B.类型

C.数值

D.存放地址

5.重定义数组大小的语句是______________。

A.Dim语句

B.ReDim语句

C.Static 语句

D.Public 语句

6.使用Dim A(100) As Integer 语句声明了数组A,其下标的取值范围为____________。

A.0~100

B.1~100

C.0~99

D.1~99

7.若使用Dim A(100) As Integer 语句声明了数组A,下列引用错误的是___________。

A.A(0)=2

B.A(1)= -28

C.A(56)= A(-56)

D.A(100)=A(0)+A(55)

8.如果要对已经声明的数组重新定义大小,并保留原有数据,可使用语句___________。

A.Dim

B.ReDim

C.Dim Preserve

D.ReDim Preserve

9.对于Integer 类型的静态数组,如果没有赋值,则所有元素的值为____________。

A.空

B.0

C.1

D.最大数

10.执行重定义数组大小语句ReDim A(UBound(B))后,A数组的上界为______________。

A.B数组的上界

B.B数组的上界+1

C.B数组的上界- 1

D.不确定

11.下列数组声明语句中正确的是______________。

A.Dim A(9) As Single ={1,2,3,4,5,6,7,8,9,10}

B.Dim A() As Single ={1,2,3,4,5,6,7,8,9,10}

C.Dim A() As Single ={1,”abc”,”ccc”,4,5,6,7,8,9,10}

D.Dim A( , ) As Single ={1,2,3,4,5,6,7,8,9,10}

12. 若有声明Dim a(2 ,3) as Integer,则数组a共有元素个。

A.12 B.30 C.16 D.4

13.下列程序的输出结果是______________。

Dim A() As Integer ={1,2,3,4,5,6,7}

For i=0 to UBound(A)

A(i)= A(i)* A(i)

Next i

MsgBox (A(i))

A.49

B.0

C.不确定

D.程序出错

14. 关于数组的声明,正确的是__________。

A.Dim a(5) As Integer = {1,2,3,4,5,6}

B.Dim a(1,6) As Integer = {1,2,3,4,5,6}

C.Dim a( ) As Integer = {1,2,3,4,5,6}

D.Dim a(1 to 6) As Integer = {1,2,3,4,5,6}

15.有两个数组s1和s2,要将s1的内容“拷贝”一份给s2,使用()。

A.s2=s1

B. s2=s1.Copy

C.s2=s1.Clone

D.s2=s1.All

二、填空题

1.数组元素下标下界为_____0_____ ,不能改变;下标上界只能用常数表达式定义。

2.定义数组大小时,要想不丢失原有的数据,则必须在ReDim 后边加上关键字__ Preserve _。

3.若用Dim A(3,5) As Integer 语句声明数组A ,A数组有_____24___ 个元素。

4.数组的存放是按行存放的,因此要将控制数组第一维的循环变量放在___外_______ 循环中。

5.若有语句A=Array(1,2,3,4,5,6,7,8),则A(5)的值是___6_____。

三、程序填空题

1. 将数组下标为偶数的元素从小到大排序,其他元素不变

Private Sub Button1_Click( ……) Handles Button1.Click

Dim a() As Integer = {90, 49, 23, 45, 11, 45, 67, 23, 68, 56}

Dim i, j, n, temp As Integer

n = UBound(a)

For i = 0 To n

Label1.Text = Label1.Text & a(i) & vbCrLf

Next i

For i = 0 (1) To (n \ 2 - 1) * 2 (2)

For j = i+2 (3) To (n\2) *2 (4) Step 2

If a(i) > a(j) Then

temp = a(i): a(i) = a(j): a(j) = temp

End If

Next j

Next i

For i = 0 To n

Label2.Text = Label2.Text & a(i) & vbCrLf

Next i

End Sub

1.⑴ 0 ⑵ step 2 ⑶ i + 2 ⑷(n \ 2) * 2

2.计算给定的10个数中正数之和和负数之和,最后输出这两个和数的绝对值之商。Private Sub Button1_Click(……) Handles Button1.Click

Dim a() As Integer = {23, -5, 23, -45, 11, 8, -3, 38, -31, 15}

Dim s1, s2, i As Integer, x As Single

s1 = 0: s2 = 0

For i = (1) To (2)

Label1.Text = Label1.Text & a(i) & vbCrLf

If (3) Then

s1 = s1 + a(i)

Else

s2 = (4)

End If

Next i

x = s1 / Math.Abs(s2)

Label2.Text = x

End Sub

(1) 0 ⑵UBound(a) (3)a(i) > 0 ⑷ s2+a(i)

3. 从键盘读取40个数保存到数组a中,将一维数组中各元素的值移到后一个元素中,而最末一个元素的值移到第一个元素中去。按每行4个数的格式输出。

Private Sub Button1_Click(……) Handles Button1.Click

Dim A(10) As Integer,i, b As Integer

For i = 0 To 10

A(i) = Val(InputBox("请输入一个整数"))

Next i

b = A(10)

For i = (1)

A(i + 1) = A(i)

Next i

(2)

For i = 0 To 10

If i (3) 4 = 0 Then Label1.Text = Label1.Text & vbCrLf

Label1.Text = Label1.Text & A(i) & " "

Next i

End Sub

3.⑴9 To 0 Step -1 ⑵A(0) = b ⑶ mod

4.下列程序的功能是对已知数组A,删除数组中指定值的元素。

Private Sub Button1_Click(……) Handles Butt on1.Click

Dim A() As Integer = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

Dim key, i, j, n As Integer

n = UBound(A)

Key = Val(InputBox("输入要删除的值"))

For i = 0 To n

If (1) Then

For j = i + 1 To n

(2)

Next j

ReDim (3)

MsgBox("删除完成")

Exit For

End If

Next i

If i > n Then MsgBox("找不到要删除的元素")

End Sub

(1) A(i) = key (2) A(j-1) = A(j) (3) Preserve A(n - 1)

四.程序调试题

1.下面程序对产生的10个随机数进行排序,程序中有3个错误,请在相应的行上改正。

Private Sub Button1_Click(……) Handles Button1.Click

Dim a(9) As Integer

Dim i, j, imin, t As Integer

Label1.Text = "原始成绩为:" & vbCrLf

For i = 1 To 10 // For i = 0 To 9

a(i) = Int(Rnd() * 101)

Label1.Text &= a(i) & " "

Next

For i = 0 To 8

For j = 9 To i + 1 // For j = 9 To i + 1 step -1

If a(j) < a(j - 1) Then

t = a(i) // t = a(j)

a(j) = a(j - 1)

a(j - 1) = t

End If

Next j

Next i

Label2.Text = "排序后的成绩为:" & vbCrLf

For i = 0 To 9

Label2.Text &= a(i) & " "

Next i

End Sub

2.随机产生20个不重复的A~Z之间的英文字母,按字母顺序输出,如图4- 1所示,程序中有三个错误,请改正。提示:定义一个有20个元素的字符型数组,将产生的字母放入数组中。每次产生一个字母,则与已经产生过的字母比较,看是否相同若相同,则跳过该字母,不放入数组中。

项目4数组习题答案

图4- 1 产生随机字母

Private Sub Button1_Click( ……) Handles Button1.Click

Dim a(19) As Char, i, j As Integer

Label1.Text = ""

Randomize()

For i = 0 To 19

a(i) = Int(Rnd() * 26 + 65) // a(i) = Chr(Int(Rnd() * 26 + 65))

For j = 0 To I //For j = 0 To i - 1

If a(i) = a(j) Then i = i - 1

Next

Next

For i = 0 To 19

Label1.Text &= Chr(a(i)) & " " // Label1.Text &= a(i) & " "

Next

End Sub

3.程序自动产生10个10~99 之间的随机正整数,在lable1显示大的数,lable2显示小的数。找出能被2、3、5整除的数,结果显示在Label3,如图4- 2所示。程序中有3个错误,请调试改正。

项目4数组习题答案

图4- 2 产生随机整数

Private Sub Button1_Click(……) Handles Button1.Click

Dim a%(9), max%, i%, min%

Randomize()

For i = 0 To 9

a(i) = Int(Rnd() * 99 + 10) // Int(Rnd() * 90 + 10)

if i=0 then min=a(i): max=a(i)

If a(i) < min Then

min = a(i)

Else // Else if a(i)> max

max = a(i)

End If

If a(i) Mod (2,3,5) = 0Then //a(i) Mod 2=0 And a(i) Mod 3=0 And a(i) Mod 5=0

Label3.Text &= a(i) & " "

End If

Next i

Label1.Text &= max

Label2.Text &= min

End Sub

相关推荐
相关主题
热门推荐