八年級(jí)信息技術(shù)教案第六章 各就各位---數(shù)組
如:dim s(20),x(20,30)
……
√redim s(50)
×redim x(30)
只改變同名數(shù)組的大小,但不能改變維數(shù);
(2).釋放數(shù)組語(yǔ)句
erase <數(shù)組名>
釋放數(shù)組所占據(jù)的空間;
例1:釋放和重定義數(shù)組:
n=10
dim a(n)
for i=1 to n
a(i)=i
print a(i);
next i
erase a
redim a(16)
for i=1 to 16
a(i)=i
print a(i);
next i
end
說(shuō)明:
.如果不使用erase語(yǔ)句釋放數(shù)組,不能用dim語(yǔ)句對(duì)同一個(gè)數(shù)組兩次定義;
.用redim可改變數(shù)組的大小,而不必先用erase語(yǔ)句釋放數(shù)組;
.無(wú)論用dim或redim定義一個(gè)與原來(lái)數(shù)組同名的數(shù)組,可以改變數(shù)組中各維的上下界,而不能改變數(shù)組的維數(shù)。
(3).數(shù)組上、下界函數(shù)
lbound(<數(shù)組名>,<維數(shù)>)——下界函數(shù)
ubound(<數(shù)組名>,<維數(shù)>)——上界函數(shù)
例:
input n,m
dim a(n to m)
for i=lbound(a,1) to ubound(a,1)
a(i)=i
print a(i);
next i
dim b(10,15)
for i=1 to ubound(b,1)
for j=1 to ubound(b,2)
b(i,j)=10*i+j
print b(i,j);
next j
next i
end
三、數(shù)組的應(yīng)用
第二節(jié) 一字排開-----一維數(shù)組
1、一維數(shù)組
只有一個(gè)下標(biāo)的數(shù)組。
(1).數(shù)據(jù)統(tǒng)計(jì)
例: 輸入n個(gè)學(xué)生的成績(jī),求平均成績(jī)。
option base 1
input "number of students is:";n
dim s(n)
for i=1 to n
input s(i)
sum=sum+s(i)
next i
aver=sum/n
print "aver=";aver
end
例: 輸入n個(gè)學(xué)生的學(xué)號(hào)和成績(jī),要求輸出平均成績(jī)和高于平均分的學(xué)生學(xué)號(hào)及成績(jī)。
option base 1
input "number of students is:";n
dim num(n),score(n)
for i=1 to n
input num(i),score(n)
sum=sum+score(i)
next i
aver=sum/n
print "平均分是:";aver
print "高于平均分的學(xué)生有:"
print "學(xué)號(hào)","成績(jī)"
for i=1 to n
if score(i)>aver then print num(i),score(i)
next i
end
(2)、數(shù)據(jù)排序
例:從鍵盤輸入10個(gè)數(shù)。要求按由小到大的順序?qū)⑺鼈兇蛴〕鰜?lái);
• 比較交換法
a1、a2、a3、a4、a5、a6、a7、a8、a9、a10
第一次:a1與a2、a3、a4、a5、a6、a7、a8、a9、a10比較
第二次:a2與a3、a4、a5、a6、a7、a8、a9、a10比較
第三次:a3與a4、a5、a6、a7、a8、a9、a10比較
第四次:a4與a5、a6、a7、a8、a9、a10比較
第五次:a5與a6、a7、a8、a9、a10比較
第六次:a6與a7、a8、a9、a10比較
第七次:a7與a8、a9、a10比較
第八次:a8與a9、a10比較
第九次:a9與a10比較
option base 1
dim a(10)
for i=1 to 10
input a(i)
next i
for i=1 to 9
for j=i+1 to 10
if a(i)>a(j) then swap a(i),a(j)
next j
next i
for i=1 to 10
print a(i);
next i
end
6. 選擇法
option base 1
dim a(10)
for i=1 to 10
input a(i)
next i
for i=1 to 9
k=i
for j=i+1 to 10
if a(k)>a(j) then k=j
next j
if k<>i then swap a(k),a(i)
next i
for i=1 to 10
print a(i);
next i
end (3).數(shù)據(jù)查找(檢索) 例: 設(shè)有n個(gè)學(xué)生,每個(gè)學(xué)生的數(shù)據(jù)包括:學(xué)號(hào)、姓名、性別、年齡、平均分等;要求輸入一個(gè)學(xué)號(hào),程序輸出該學(xué)生的所有數(shù)據(jù)。
• 順序查找法
假設(shè):num(i)為學(xué)生學(xué)號(hào),nam$(i)為姓名,num為查找對(duì)象。 sub search