Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Lab_MATLAB_English_Part2.doc
Скачиваний:
2
Добавлен:
19.11.2019
Размер:
421.89 Кб
Скачать

2.3 Tasks for Laboratory Work

Problem 2.1. The array X(n) is given. Calculate the value .

Variant 1

Variant 2

Variant 3

Variant 4

Variant 5

Variant 6

Variant 7

Variant 8

Variant 9

Variant 10

Variant 11

Variant 12

Variant 13

Problem 2.2. Calculate new array Y(n) from array X(n) in accordance with the rule :

Variant 1

Variant 2 Arrange the array elements in inverse order.

Variant 3 Shift cyclically the array elements on one position to the left.

Variant 4

Variant 5 Order the array elements on a decrease.

Variant 6 Swap the maximum and minimum elements.

Variant 7

Variant 8 Transfer all negative array elements to the beginning of array, and all the rest the end of array, leaving initial mutual disposition between them.

Variant 9

Variant 10

Variant 11

Variant 12 Shift cyclically the array elements on two positions to the right.

Variant 13 Order the array elements on a growth.

2.3 Examples of Performance of Laboratory Work

Example 2.1. The value n and array a(n) are given. Calculate the value of maximal element and its number.

The text of the programs for the decision of the problem,

Private Sub Command1_Click()

Dim s As String

If IsNull(Text3.Text) Or Text3.Text = "" Then

MsgBox "Input amount of array elements"

Text3.SetFocus

Exit Sub

End If

n = Text3.Text

ReDim a(n)

For i = 1 To n

a(i) = InputBox("a(" & i & ")=")

s = s & i & vbTab & a(i) & vbCrLf

Next i

Text1.Text = s

End Sub

Private Sub Command2_Click()

Dim amax As Integer, kmax As Integer

amax = a(1): kmax = 1

For i = 2 To n

If a(i) > amax Then amax = a(i): kmax = i

Next i

Text2.Text = amax

Text4.Text = kmax

End Sub

Example 2.2. The value n and array a(n) are given. Calculate the new array y according the rule: transfer all negative elements of array a to the beginning of array y and all the rest elements of array a to the end of array y, leaving initial mutual disposition between elements of array.

Dim x() As Integer, y() As Integer, n As Integer

Private Sub CommandButton1_Click()

Dim i As Integer, s As String

n = TextBox1.Text

ReDim x(n)

s = ""

For i = 1 To n

x(i) = InputBox("x(" & i & ")=")

s = s & x(i) & vbCrLf

Next i

TextBox2.Text = s

End Sub

Private Sub CommandButton2_Click()

Dim i As Integer, k As Integer, s As String

ReDim y(n)

k = 0

s = ""

For i = 1 To n

If x(i) < 0 Then

k = k + 1

y(k) = x(i)

s = s & y(k) & vbCrLf

End If

Next i

For i = 1 To n

If x(i) >= 0 Then

k = k + 1

y(k) = x(i)

s = s & y(k) & vbCrLf

End If

Next i

TextBox3.Text = s

End Sub

2.4 Test Questions

1. What is an array variable? Give an example.

2. What are the advantages of using array variables?

3. Discuss the applications of array variables.

4. Is it true that the array elements be of any type?

5. Is it possible to change the size of array at run time of the program?

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]