设有一个5*5的方阵,其中元素是由计算机随机生成的小于100的整数。求出:(1)主对角线上的元素之和;(2)方阵中最大的元素。编写代码:首先在表单的load事件代码中声明数组:public a(5,5)方阵的生成由表单的active事件代码完成:for i=1 to 25 yes=1 do while yes=1 x=int(rand()*100) yes=0 for j=1 to i-1 if x=val(a(j)) yes=1 exit endif endfor enddo a(i)=str(x,3) endfor thisform.list1.numberofelements=5 thisform.text1.value="" thisform.text2.value=""在表单的unload事件代码中释放全局变量数组a(): release a计算功能由“计算”按钮command1的click事件代码完成:s=0for i=1 to 5 s=s + val(a(i,i))endforthisform.text1.value=smax=0for i=1 to 5 for j= 1 to 5 if max< val(a(i,j)) max=val(a(i,j)) p=i q=j endif endfor endfor thisform.text2.value="A("+str(p,1)+","+str(q,1)+")="+str(max,3) “重置”按钮command2的click事件代码:thisform.activate

评论