vb中求一个矩阵所有元素的和,代码该怎么写?

vb中求一个矩阵所有元素的和,代码该怎么写?,第1张

矩阵在程序中用二维数组来实橘雀现,如下所表示圆答早的是一个3行4列的矩阵

d(1,1)

d(1,2)

d(1,3)

d(1,4)

d(2,1)

d(2,2)

d(2,3)

d(2,4)

d(3,1)

d(3,2)

d(3,3)

d(3,4)

通常在程序举纯中对二维数组的 *** 作,采用双重循环来实现,例如求和:

dim

i

as

integer

dim

j

as

integer

dim

S

as

single

S

=

0

for

i=1

to

3

'外循环针对行

for

j=1

to

4

'内循环针对列

S

=

S

+

d(i,j)

next

j

next

i

Print

S

Public Sub add(a() As Integer, b() As Integer, c() As Integer, n As Integer)

For i = 1 To n

For j = 1 To n

c(i, j) = a(i, j) + b(i, j)

Next j

Next i

End SubPrivate Sub Command1_Click()

Dim n As Integer

Dim i As Integer, j As Integer

n = InputBox("输入方阵的行数", "输入框")

ReDim a(1 To n, 1 To n)

ReDim b(1 To n, 1 To n)

ReDim c(1 To n, 1 To n)

Call readm(a, "输入矩阵a")

Call readm(a, "输入矩阵b")

Print "矩阵a"

Call out(a)

Print

Print "矩阵b"羡野岩

Call out(b)

Print

Call add(a(), b(), c(), n)

Print "矩阵c"

Call out(c)

End Sub

Public Sub add(a() As Integer, b() As Integer, c() As Integer, n As Integer)

For i = 1 To n

For j = 1 To n

c(i, j) = a(i, j) + b(i, j)

Next j

Next i

End SubPrivate Sub Command1_Click()

Dim n As Integer

Dim i As Integer, j As Integer

n = InputBox("输入方阵的行数", "兄御输入框")

ReDim a(1 To n, 1 To n)

ReDim b(1 To n, 1 To n)

ReDim c(1 To n, 1 To n)

Call readm(a, "输脊岩入矩阵a")

Call readm(a, "输入矩阵b")

Print "矩阵a"

Call out(a)

Print

Print "矩阵b"

Call out(b)

Print

Call add(a(), b(), c(), n)

Print "矩阵c"

Call out(c)

End Sub


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/8269092.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-14
下一篇 2023-04-14

发表评论

登录后才能评论

评论列表(0条)

保存