Лабораторная работа 4. Лабораторная работа 4 Операции с матрицами Задание 1
Скачать 24.96 Kb.
|
Лабораторная работа № 4 Операции с матрицами Задание 1. Вычислить сумму элементов двух матриц А(3,3) и В (3,3). Для ввода, вывода и суммирования матриц использовать отдельные процедуры :
‘При написании процедуры в скобках после имени описываются универсальные переменные, которые используются в данной процедуре Sheet –номер листа, row-номер строки, col-номер столбца, е()- имя вводимой матрицы , n, m –целые числа для работы цикла. Public Sub vvod(Sheet As Integer, row As Integer, col As Integer, e() As Single, n As Integer, m As Integer) Dim i, j As Integer For i = 1 To n For j = 1 To m e(i, j) = Worksheets(Sheet).Cells(row + i - 1, col + j - 1).Value Next j Next i End Sub ‘Запись row + i - 1, col + j – 1 является универсальной, и определяет адрес ячейки для любого случая.
Public Sub summa(S() As Single, n As Integer, m As Integer, P() As Single, L() As Single) Dim i, j As Integer For i = 1 To n For j = 1 To m S(i, j) = P(i, j) + L(i, j) Next j Next i End Sub
‘Переменная nam это текстовый комментарий, который вставляется в соответствующую ячейку Public Sub vivod(Sheet As Integer, row As Integer, col As Integer, n As Integer, m As Integer, z() As Single, nam As String) Dim i, j As Integer Worksheets(Sheet).Cells(row -1, col).Value = nam For i = 1 To n For j = 1 To m Worksheets(Sheet).Cells(row + i - 1, col + j - 1).Value = z(i, j) Next j Next i End Sub
Public Sub matrix() Dim A(3, 3) As Single Dim B(3, 3) As Single Dim C(3, 3) As Single ‘ Обращение к процедуре ввода матриц. Call vvod(1, 2, 1, A(), 3, 3) В скобках цифры и массивы принимают значения соответствующих переменных, объявленных в описании данной процедуры. ‘ Обращение к процедуре ввода матриц. Call vvod(1, 2, 5, B(), 3, 3) ‘ Обращение к процедуре суммирования матриц Call summa(C(), 3, 3, A(), B()) ‘ Обращение к процедуре вывода матрицы. Call vivod(1, 7, 1, 3, 3, C(), "СУММ") End Sub Задание 2. Выполнить транспонирование матрицы A(3,3)
Public Sub vvod(Sheet As Integer, row As Integer, col As Integer, e() As Single, n As Integer, m As Integer) Dim i, j As Integer For i = 1 To n For j = 1 To m e(i, j) = Worksheets(Sheet).Cells(row + i - 1, col + j - 1).Value Next j Next i End Sub
Public Sub transp(M1() As Single, n As Integer, m As Integer, M2() As Single) Dim i, j As Integer For i = 1 To n For j = 1 To m M2(i, j) = M1(j, i) Next j Next i End Sub 3.Процедура для вывода матриц Public Sub vivod(Sheet As Integer, row As Integer, col As Integer, n As Integer, m As Integer, z() As Single, nam As String) Dim i, j As Integer Worksheets(Sheet).Cells(row -1, col).Value = nam For i = 1 To n For j = 1 To m Worksheets(Sheet).Cells(row + i - 1, col + j - 1).Value = z(i, j) Next j Next i End Sub
Public Sub matrix() Dim A(3, 3) As Single Dim B(3, 3) As Single Call vvod(2, 1, 1, A(), 3, 3) Call transp(A(), 3, 3, B()) Call vivod(2, 5, 1, 3, 3, B(), "Трансп") End Sub Задание 3 Выполнить матричное умножение матриц А(4,3) и В(3,2)
Public Sub vvod(Sheet As Integer, row As Integer, col As Integer, e() As Single, n As Integer, m As Integer) Dim i, j As Integer For i = 1 To n For j = 1 To m e(i, j) = Worksheets(Sheet).Cells(row + i - 1, col + j - 1).Value Next j Next i End Sub
Public Sub mumnog(K1() As Single, K2() As Single, K3() As Single , n As Integer, m As Integer, l As Integer) Dim i, j, r As Integer For i = 1 To n For j = 1 To m K3(i, j) = 0 For r = 1 To l K3(i, j) = K3(i, j) + K1(i, r) * K2(r, j) Next r Next j Next iEnd Sub
Public Sub vivod(Sheet As Integer, row As Integer, col As Integer, n As Integer, m As Integer, z() As Single, nam As String) Dim i, j As Integer Worksheets(Sheet).Cells(row + i, col).Value = nam For i = 1 To n For j = 1 To m Worksheets(Sheet).Cells(row + i - 1, col + j - 1).Value = z(i, j) Next j Next i End Sub Основная процедура Public Sub matrix() Dim A(4, 3) As Single Dim B(3, 2) As Single Dim C(4, 2) As Single Call vvod(3, 1, 2, A(), 4, 3) Call vvod(3, 5, 2, B(), 3, 2) Call mumnog(A(), B(), C(), 4, 2, 3) Call vivod(3, 9, 1, 4, 2, C(), "Произв") End Sub Задание 4 Умножить матрицу A(3,3) на число 5
Public Sub vvod(Sheet As Integer, row As Integer, col As Integer, e() As Single, n As Integer, m As Integer) Dim i, j As Integer For i = 1 To n For j = 1 To m e(i, j) = Worksheets(Sheet).Cells(row + i - 1, col + j - 1).Value Next j Next i End Sub
Public Sub umnogch(T1() As Single, n As Integer, m As Integer, ch As Single, T2() As Single) Dim i, j As Integer For i = 1 To n For j = 1 To m T2(i, j) = ch * T1(i, j) Next j Next i End Sub
Public Sub vivod(Sheet As Integer, row As Integer, col As Integer, n As Integer, m As Integer, z() As Single, nam As String) Dim i, j As Integer Worksheets(Sheet).Cells(row + i, col).Value = nam For i = 1 To n For j = 1 To m Worksheets(Sheet).Cells(row + i - 1, col + j - 1).Value = z(i, j) Next j Next i End Sub 5.Основная процедура Public Sub chislo() Dim A(3, 3) As Single Dim B(3, 3) As Single Call vvod(1, 1, 1, A(), 3, 3) Call umnogch(A(), 3, 3, 5, B()) Call vivod(1, 7, 1, 3, 3, B(), "Умнож") End Sub Задание 5 Вычислить матричное выражение.
|