Option Explicit
==================================================
Sub Array1()
' Работа с едномерен масив
Dim i%, M%
Dim a() As Double
M = InputBox("M=")
ReDim a(1 To M)
For i = 1 To M
a(i) = ActiveSheet.Cells(1, i)
Next i
' a) сума на всички елементи в масива
Dim S As Double
S = 0
For i = 1 To M
S = S + a(i)
Next i
MsgBox ("Сума S=" & S)
' б) произведение на всички ненулеви елементи в масива
Dim P As Double
P = 1
For i = 1 To M
If a(i) <> 0 Then P = P * a(i)
Next i
MsgBox ("Произведение P=" & P)
' в) средно аритметично на всички положителни елементи в масива
Dim Sp As Double, Np As Integer
Sp = 0: Np = 0
For i = 1 To M
If a(i) > 0 Then Sp = Sp + a(i): Np = Np + 1
Next i
If Np > 0 Then
MsgBox ("Средно аритметично: " & Sp / Np)
Else
MsgBox ("Няма положителни елементи")
End If
' г) максимум на елементите в масива
Dim Max As Double, imax As Integer
Max = a(1): imax = 1
For i = 2 To M
If a(i) > Max Then Max = a(i): imax = i
Next i
MsgBox ("Максимум Max = A(" & imax & ")= " & Max)
' д) минимум на елементите в масива
Dim Min As Double, imin As Integer
Min = a(1): imin = 1
For i = 2 To M
If a(i) < Min Then Min = a(i): imin = i
Next i
MsgBox ("Минимум Min = A(" & imin & ")= " & Min)
' е) сортиране на елементите на масива във възходящ ред
Dim T As Double, j As Integer
For i = 1 To M - 1
For j = 1 To M - i
If a(j) > a(j + 1) Then
T = a(j): a(j) = a(j + 1): a(j + 1) = T
End If
Next j
Next i
For i = 1 To M
ActiveSheet.Cells(2, i) = a(i)
Next
End Sub
==================================================
Sub Skalar()
' Скаларно произведение на два вектора
Dim i%, M%
Dim a() As Double, b() As Double, S As Double
M = InputBox("M=")
ReDim a(1 To M), b(1 To M)
For i = 1 To M
a(i) = ActiveSheet.Cells(1, i)
b(i) = ActiveSheet.Cells(2, i)
Next i
S = 0
For i = 1 To M
S = S + a(i) * b(i)
Next i
MsgBox ("Скаларно произведение S=" & S)
End Sub
==================================================
Sub Points()
Dim i%, M%, a#, b#, c#, imax%
Dim X() As Double, Y() As Double, P() As Double, PMax#
M = InputBox("M=")
ReDim X(1 To M), Y(1 To M), P(1 To M)
For i = 1 To M
X(i) = ActiveSheet.Cells(1, i)
Y(i) = ActiveSheet.Cells(2, i)
Next i
For i = 1 To M - 2
a = Sqr((X(i) - X(i + 1)) ^ 2 + (Y(i) - Y(i + 1)) ^ 2)
b = Sqr((X(i + 1) - X(i + 2)) ^ 2 + (Y(i + 1) - Y(i + 2)) ^ 2)
c = Sqr((X(i) - X(i + 2)) ^ 2 + (Y(i) - Y(i + 2)) ^ 2)
P(i) = a + b + c
Next i
PMax = P(1): imax = 1
For i = 2 To M - 2
If PMax < P(i) Then PMax = P(i): imax = i
Next
MsgBox ("Максимален периметър Max = P(" & imax & ")= " & PMax)
End Sub
Сподели с приятели: |