輸出4×4數組的對角線之和 vb中如何實現數組對角線求和問題?
vb中如何實現數組對角線求和問題?一個n階方陣,用n×n(n行n列)二維數組存儲:主對角線上元素的下標,滿足:行號=列號,次對角線上元素的下標,滿足:行號和列號=n 1VB代碼實現:(以n=5為例,數
vb中如何實現數組對角線求和問題?
一個n階方陣,用n×n(n行n列)二維數組存儲:主對角線上元素的下標,滿足:行號=列號,次對角線上元素的下標,滿足:行號和列號=n 1VB代碼實現:(以n=5為例,數組為D,已分配)dim n as integerdim S1 as integer“主對角線上的元素之和dim S2 as integer”次對角線上的元素之和dim I as integerdim J as integer n=5s1=0s2=0,對于I=1到5,對于J=1到n,如果I=J,則S1=S1 D(I,J)如果I J=n1,則S2=S2 D(I,J)下一個jnext iPrint“主對角線元素之和=“s1print”子對角線元素之和=”S2
c語言一維數組對角線求和?
#include
#define n3
int Main()
{
int i,J,sum=0
int a[n][n]//假設數組為3*3;
printf(”please input:n“)
for(i=0I
~]#include<stdio。H>
main()
{
int a[3][3],sum=0,I,J/*以3*3的數組為例。我們也可以是另一個printf(“please input number:number:the input number:the input number:the input number:the input number:the input number:the input number:the input number:the input number:the input number:the input number:the please input number:the number of[i=0I< 3I
{
]{
!]{[for(J=0j(J=0j<)]3j)
{
!{[對于(J=0j[J(J=0j(J=0j< 3j)J)
!]{
][printf([I[I][I][I]/*
![sum=sum=a[I[I[I][2-2-I][2-I]///*右上角到右下角到左下角到左下角到右下角的底部底部最后一個[“sum”=%Dn,sum)
求二維數組對角線方向數組元素如何求和?
請參閱以下程序:
int a[5][5]//5*5數組
int i,x,Y//x,Y是兩個對角元素的和
…
x=0
for(i=0I
{
x=a[i][i
}]Y=0
for(i=0I
{
Y=a[4-i][i
}