В массиве A(4,4) найти сумму элементов главной диагонали- Basic(Бейсик)

'1] . . .  Pervaya
DIM A(4, 4), sum
RANDOMIZE TIMER: CLS
PRINT "Zadanie 1 "; STRING$(70, CHR$(196))
FOR i2 = 1 TO 4
    FOR i = 1 TO 4
        A(i2, i) = INT(RND * 10) - 5
        IF i = i2 THEN COLOR 10 ELSE COLOR 7
        PRINT A(i2, i); LEFT$(CHR$(13), ABS(i = 4));
NEXT i, i2: COLOR 7
FOR i = 1 TO 4: sum = sum + A(i, i): NEXT
PRINT "Summa elem-ov diagonali:"; sum
'2] . . .  Vtoraya
PRINT "Zadanie 2 "; STRING$(70, CHR$(196))
DIM A2(10)
PRINT "Dano    : ";
FOR i = 1 TO 10
    A2(i) = INT(RND * 10) - 5: PRINT A2(i);
NEXT
FOR i = 1 TO 10
    A2(i) = A2(i) * ((A2(i) < 0) + 1)
NEXT: PRINT
PRINT "Result  : ";
FOR i = 1 TO 10
    PRINT A2(i);
NEXT

Следующий пример

CLS 'ochistka ekrana
n = 4
DIM a(n, n) 'obyavlenie massiva
RANDOMIZE TIMER 'Zapusk generatora sluchainikh chisel
' Zapolnenie massiva sluchainimi tselimi chislami ot -50 do 50
FOR i = 1 TO n
    FOR j = 1 TO n
        a(i, j) = INT(RND * 101) - 50
    NEXT j
NEXT i
'Vivod massiva na ekran
FOR i = 1 TO n
    FOR j = 1 TO n
        PRINT a(i, j);
    NEXT j
    PRINT
NEXT i
s = 0
'pribavlyaem k peremennoi S znachenie kazhdogo elementa glavnoi diagonali
FOR i = 1 TO n
    s = s + a(i, i)
NEXT i
PRINT "Summa elementov glavnoy diagonali="; s 'vivod znacheniya summi na ekran
END

Leave a Comment