vb编程求下列矩阵主对角线上的元素之和.1 2 3 4 5 6 7 8 9
问题描述:
vb编程求下列矩阵主对角线上的元素之和.1 2 3 4 5 6 7 8 9
答
我的理解,你是一个9*9的矩阵,那么:dim s as integerdim i as integerdim j as integerdim d(9,9) as integer'假设二维数组为d,且主对角线元素的值为1-9s=0for i=1 to 9 for j=1 to 9if i=j then s = s...是我的错,应该是3*3的,第一行123 第二行 456 第三行789哦,这个这样:dim s as integerdim i as integerdim j as integerdim d(3,3) as integerdim n as integer'先构成矩阵的二维数组n=0for i=1 to 3for j=1 to 3 n = n + 1 d(i,j) = n next jnext i'输出矩阵For i = 1 To 3For j = 1 To 3Print d(i, j),Next jPrintNext i'计算主对角线和并输出s = 0For i = 1 To 3For j = 1 To 3 If i = j Then s = s + d(i, j)Next jNext iPrint s