一维数组举例:
1.王小二是个卖大饼的,他一刀可以把大饼切2块,两刀可以切出4块,三刀最多可以切7块,四刀可以切11块,问10刀最多可以切多少块?
private sub form_click()
dim x(10) as integer
x(0) =1
for i = 1 to 10
x(i) =i + x(i - 1)
print x(i),if i mod 5 = 0 then print
next i
end sub
2.输入一串字符,统计各字母出现的次数
private sub form_click()
dim i%, m%, s$, c$
dim a(25) as integer
s = inputbox("请输入字符串")
for i = 1 to len(s)
c = mid(s, i, 1)
m = asc(ucase(c)) 65
a(m) =a(m) +1
next i
for i = 0 to 25
print a(i)
next i
end sub
3.随机产生10个小于100的自然数,找出其最大值、最小值和平均值。
private sub form_click()
dim i%, max%, min%, s!
dim a%(1 to 10等价于dim a (1 to 10) as integer
randomize
for i = 1 to 10
a(i) =int(rnd * 99) +1
print a(i)
next i
max = a(1): min = a(1)
for i = 1 to 10
if a(i) >max then max = a(i)
if a(i) s = s + a(i) next i print "最大值是"; max print "最小值是"; min print "平均值是"; s / 10 end sub 4.产生10个小于20的不等随机整数,利用冒泡排序法,将数据按从大到小的顺序排列。 private sub form_click() dim i%, j% dim a(1 to 10) as integer print "排序前10个数的次序:" for i = 1 to 10 dox = int(rnd * 20) yes = 0 for j = 1 to i - 1 if x = a(j) then yes = 1: exit for next j loop while yes = 1 a(i) =x print a(i); next i for i = 1 to 9 for j = 1 to 10 - i if a(j)
t = a(j): a(j) =a(j + 1): a(j + 1) =t end if next j next i printprint "排序后10个数的次序:" for i = 1 to 10 print a(i); next i end sub 5.产生10个小于20的不等随机整数利用选择排序法,将数据按从大到小的顺序排列。 private sub form_click() dim i%, j% dim a(1 to 10) as integer print "排序前10个数的次序:" for i = 1 to 10 dox = int(rnd * 20) yes = 0 for j = 1 to i - 1 if x = a(j) then yes = 1: exit for next j loop while yes = 1 a(i) =x print a(i); next i for i = 1 to 9 for j = i + 1 to 10 if a(i)
t = a(i): a(i) =a(j): a(j) =t end if next j next i printprint "排序后10个数的次序:" for i = 1 to 10 print a(i); next i end sub 例1 编写程序,利用二维数组在窗体上输出如图5x5矩阵,该矩阵两条对角线上的元素为1,其余元素为0 要求 数组的下标下界从1开始 option base 1 private sub form click dim x 5,5 as integer for i 1 to 5 for j 1 to 5 x... 1 斐波那契数列 如果设f n 为该数列的第n项 n n 那么这句话可以写成如下形式 f 1 1,f 2 1,f n f n 1 f n 2 n 3 也就是说,从第三个数开始,每一个数是它前面两个数的和。要求 使用数组,输出斐波那契数列的前20项。include void main int i 0 ... 班级学号姓名 本次题目一共5题,必做4个,选做1个。选做题可能有点难度,请尝试着做一下,能做出来说明目前你已经很强了!一 目的要求。1 掌握一维数组的基本概念,定义和初始化方法 2 掌握一维数组的基本操作,如输入 输出 引用数组元素 3 掌握与数组有关的基本算法,如寻找最大 最小值 排序等。二 实验...二维数组例题答案
c语言例题4 数组 答案
上机实验六一维数组 答案