VB程式設計,求M行N列的矩陣四周元素之和,元素值可以隨機產生,M N從鍵盤輸入

時間 2021-07-01 02:02:51

1樓:裘誠

這個是那一章的全部答案

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的矩陣,編寫程式,找出其中最大的那個元素所在的行和列,並輸出其值及行號和列號。

**private sub command1_click()

dim a(1 to 10, 1 to 10) as integer

dim m%, n%, x%

for i = 1 to 10

for j = 1 to 10

a(i, j) = int(rnd * 101)

print tab((j - 1) * 5 + 2); a(i, j);

next j

next i

print

x = a(1, 1)

for i = 1 to 10

for j = 1 to 10

if a(i, j) > x then m = i: n = j: x = a(i, j)

next j

next i

print "最大數為:" & x & ",它為第 " & m & "行," & "第" & n & "列的元素。"

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("請輸入第" & i & "個數")

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 "90-100分有:" & x(9) + x(10) & "人"

for i = 8 to 6 step -1

picture1.print i * 10 & "分—"; i * 10 + 9 & "分有" & x(i) & "人"

next i

for i = 1 to 5

z = z + x(i)

next i

picture1.print "60分以下的有:" & z & "人"

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("請輸入第" & i + 1 & "個數")

print a(i);

s = s + a(i) * w(i)

next i

print

y = s mod 11

y = y + 1

m = choose(y, "1", "0", "x", "9", "8", "7", "6", "5", "4", "3", "2")

print "第十八位是:" & m

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

執行介面

11.某校召開運動會有10人蔘加男子100米短跑決賽,運動員號碼和成績如表5-3所示,試編制程式,按成績由高到低排序。

**執行介面

2樓:匿名使用者

'第一題:

private sub command1_click()

dim m%, n%, i%, j%, temp&, s&

m = val(inputbox("請輸入矩陣行數m=", , "3"))

n = val(inputbox("請輸入矩陣列數n=", , "4"))

s=0for i = 1 to m

for j = 1 to n

temp = int(rnd() * 10)

print temp,

if i = 1 or j = 1 or i = m or j = n then s = s + temp

next

print

next

print "四周元素之和是:" & s

end sub

'第二題

private sub command2_click()

dim m%, n%, i%, a%()

redim a(10)

print "原陣列為:"

for i = 1 to ubound(a)

a(i) = i: print a(i),

next

print

m = val(inputbox("請輸入插入的位置m=", , "3"))

n = val(inputbox("請輸入要插入的數n=", , "9999"))

redim preserve a(ubound(a) + 1)

for i = ubound(a) to m step -1

a(i) = a(i - 1)

next

a(m) = n

print "插入後陣列為:"

for i = 1 to ubound(a)

print a(i),

next

print

end sub

行矩陣的逆矩陣怎麼求,n行1列矩陣怎麼求逆矩陣

雨說情感 1 伴隨矩陣法 如果矩陣a可逆,則 的餘因子矩陣的轉置矩陣。a 0,a 為該矩陣對應的行列式的值 a的伴隨矩陣為 其中aij 1 i jmij稱為aij的代數餘子式。2 初等行變換法 在行階梯矩陣的基礎上,即非零行的第一個非零單元為1,且這些非零單元所在的列其它元素都是0。綜上,行最簡型矩...

vb程式設計如何實現資料中指定行的資料相加

dim j as string dim p as string dim h as long adodc1.recordsource select from tb where 主鍵 text1.text adodc1.refresh j adodc1.recordset.fields 數字 adodc...

求vb的求數平方的程式設計,求一個vb的求一個數平方的程式設計

樓主您好!什麼也不用畫,直接複製貼上 簡單就一句 msgbox 這個數的平方為 val inputbox 請輸入要求平方的數 提示視窗 2 64 vbokcancel vbdefaultbutton1,提示視窗 多寫點就這樣 dim number1 as double dim number2 as ...