Книга программиста/Обработка матриц VB
(перенаправлено с «Обработка матриц VB»)
Все программы, код которых выложен здесь, являются работоспособными. На момент написания программ использовалась среда SharpDevelop 5.1.
Простые задачи
правитьМаксимальные элементы столбцов матрицы
правитьModule Program
Sub Main()
Const N As Integer = 3
Const M As Integer = 3
Dim a(N - 1, M - 1) As Integer
Dim max(M - 1) As Integer
For i As Integer = 0 To N - 1
For j As Integer = 0 To M - 1
a(i, j) = Rnd()*100
Next
Next
Console.WriteLine("Матрица:")
For i As Integer = 0 To N - 1
For j As Integer = 0 To M - 1
Console.Write(String.Format("{0, 6}", a(i, j)))
Next
Console.WriteLine()
Next
For j As Integer = 0 To M - 1
max(j) = integer.MinValue
For i As Integer = 0 To N - 1
If (a(i, j) > max(j)) Then
max(j) = a(i, j)
End If
Next
Next
Console.WriteLine("Максимумы:")
For j As Integer = 0 To M - 1
Console.Write(String.Format("{0, 3}", max(j)))
Next
Console.ReadKey()
End Sub
End Module
Наибольший по модулю элемент матрицы
правитьModule Program
Sub Print(ByRef a(,) As Integer)
Console.WriteLine("Матрица:")
For i As Integer = 0 To a.GetLength(0) - 1
For j As Integer = 0 To a.GetLength(1) - 1
Console.Write(String.Format("{0, 6}", a(i, j)))
Next
Console.WriteLine()
Next
End Sub
Sub Main()
Const N As Integer = 3
Const M As Integer = 3
Dim a(N - 1, M - 1) As Integer
For i As Integer = 0 To N - 1
For j As Integer = 0 To M - 1
a(i, j) = -10 + Convert.ToInt32(Rnd()*20)
Next
Next
Print(a)
Dim max As Integer = 0
Dim maxI As Integer = 0
Dim maxJ As Integer = 0
For i As Integer = 0 To N - 1
For j As Integer = 0 To M - 1
If (Math.Abs(a(i, j)) > max) Then
max = Math.Abs(a(i, j))
maxI = i
maxJ = j
End If
Next
Next
Console.WriteLine(String.Format("Наибольший по модулю элемент матрицы с индексами [{0}, {1}] равен {2}.", maxI, maxJ, max))
Console.ReadKey()
End Sub
End Module
Числа, кратные 2
правитьModule Program
Sub Print(ByRef a(,) As Integer)
Console.WriteLine("Матрица:")
For i As Integer = 0 To a.GetLength(0) - 1
For j As Integer = 0 To a.GetLength(1) - 1
Console.Write(String.Format("{0, 6}", a(i, j)))
Next
Console.WriteLine()
Next
End Sub
Sub Main()
Const N As Integer = 3
Const M As Integer = 3
Dim a(N - 1, M - 1) As Integer
For i As Integer = 0 To N - 1
For j As Integer = 0 To M - 1
a(i, j) = Convert.ToInt32(Rnd()*100)
Next
Next
Print(a)
Dim c As Integer = 0
For i As Integer = 0 To N - 1
For j As Integer = 0 To M - 1
If ((Math.Abs(a(i, j)) >= 10) And (Math.Abs(a(i, j)) < 100) And ((s \ 2 + s Mod 2) Mod 2 = 0)) Then
c += 1
End If
Next
Next
Console.WriteLine(String.Format("Количество двузначных чисел с четной суммой цифр равно {0}.", c))
Console.ReadKey()
End Sub
End Module
Средняя сложность
правитьОтрицательные элементы под диагональю
правитьModule Program
Sub Print(ByRef a(,) As Integer)
Console.WriteLine("Матрица:")
For i As Integer = 0 To a.GetLength(0) - 1
For j As Integer = 0 To a.GetLength(1) - 1
Console.Write(String.Format("{0, 6}", a(i, j)))
Next
Console.WriteLine()
Next
End Sub
Sub Main()
Const N As Integer = 3
Const M As Integer = 3
Dim a(N - 1, M - 1) As Integer
For i As Integer = 0 To N - 1
For j As Integer = 0 To M - 1
a(i, j) = -10 + Convert.ToInt32(Rnd()*20)
Next
Next
Print(a)
Dim c As Integer = 0
For i As Integer = 0 To N - 1
For j As Integer = 0 To M - 1
If ((i > j) And (a(i, j) < 0)) Then
c += 1
End If
Next
Next
Console.WriteLine(String.Format("Количество отрицательных элементов под главной диагональю матрицы равно {0}.", C))
Console.ReadKey()
End Sub
End Module
Сложные задачи
правитьЗаполнение матрицы по правилу
правитьПравило заполнения:
1 | 2 | 3 | 4 | 5 |
2 | 1 | 2 | 3 | 4 |
3 | 2 | 1 | 2 | 3 |
4 | 3 | 2 | 1 | 2 |
5 | 4 | 3 | 2 | 1 |
Module Program
Sub Print(ByRef a(,) As Integer)
Console.WriteLine("Матрица:")
For i As Integer = 0 To a.GetLength(0) - 1
For j As Integer = 0 To a.GetLength(1) - 1
Console.Write(String.Format("{0, 6}", a(i, j)))
Next
Console.WriteLine()
Next
End Sub
Sub Main()
Const N As Integer = 9
Dim a(N - 1, N - 1) As Integer
For d As Integer = 0 To N - 1
For j As Integer = d To N - 1
a(d, j) = j - d + 1
Next
For i2 As Integer = d + 1 To N - 1
a(i2, d) = i2 - d + 1
Next
Next
Print(a)
Console.ReadKey()
End Sub
End Module