Task

Given an array of integers A = (ai), where i = (1,2,3, ..., n). Print the numbers that make up the increasing sequence.
For example: if A = {2,3,4,0, -1,2, -2,0,5}, the result is 2,3,4 and -1,2.
Numbers are entered into a separate text block, I did this part, but I have no idea how to solve the problem directly.

Code:

Dim n, nw, min, i As Integer Dim nws As String Dim Msi() As Integer Private Sub Command1_Click() If Text2.Text <> "" Then n = n + 1 nws = Text2.Text Text2.Text = "" nw = CInt(nws) nws = CStr(nw) If n = 1 Then Text1.Text = nws Else Text1.Text = Text1.Text + ", " + nws ReDim Preserve Msi(n - 1) Msi(n - 1) = nw End If End Sub Private Sub Command2_click() For i = 1 To n If Msi(n) < Msi(n - 1) Then Text3.Text = Text3.Text + ", " + Msi(n) + "," + Msi(n - 1) End If Next i End Sub 
  • one
    And what happened to -1,2,5; -1,0,5; -2,0,5 and others? BASIC know the problem did not understand. - maxleo Nov.

2 answers 2

Visual Basic doesn't know. I'll tell you the algorithm:

 {Индексация массива идет с 0; n - длина массива; } Вывод(A[0]) Для i от 1 до n: Если A[i-1] >= A[i] то Вывод("Новая возрастающая последовательность: " + A[i] + ", "); Иначе Вывод(A[i] + ", "); 

    Implementation on VBA

     Sub Sequences() Dim Arr As Variant Dim Sequence() As Variant Dim CurrentIndex As Integer Arr = Array(2, 3, 4, 0, -1, 2, -2, 0, 5, 3) ReDim Sequence(0) Sequence(0) = Arr(0) CurrentIndex = 0 For I = 1 To UBound(Arr) If Sequence(CurrentIndex) > Arr(I) Then MsgBox (Join(Sequence, ", ")) ReDim Sequence(0) Sequence(0) = Arr(I) CurrentIndex = 0 Else CurrentIndex = CurrentIndex + 1 ReDim Preserve Sequence(CurrentIndex) Sequence(CurrentIndex) = Arr(I) End If Next If UBound(Sequence) = 0 Then MsgBox (Join(Sequence, ", ")) End If End Sub 

    Output: (2, 3, 4) _ (0) _ (-1, 2) _ (-2, 0, 5) _ (3)