Untitled
unknown
plain_text
2 years ago
2.4 kB
12
Indexable
Private Function GetMatrixInput(rows As Integer, columns As Integer) As Double(,)
Dim matrix(rows - 1, columns - 1) As Double
For i As Integer = 0 To rows - 1
For j As Integer = 0 To columns - 1
Dim textBoxName As String = "arr" & i & j
Dim textBox As TextBox = Me.Controls(textBoxName)
Dim value As Double
If Double.TryParse(textBox.Text, value) Then
matrix(i, j) = value
Else
MessageBox.Show("Invalid input in arr" & i & j & ". Please enter valid integers.")
Return Nothing ' or handle error as needed
End If
Next
Next
Return matrix
End Function
Private Sub BtnCalculate_Click(sender As Object, e As EventArgs) Handles BtnCalculate.Click
' Assuming the matrix is a square matrix (n = rows = columns)
Dim n As Integer = 3 ' Example: For a 3x3 matrix
Dim matrix(n - 1, n - 1) As Double
matrix = GetMatrixInput(n, n)
If matrix IsNot Nothing Then
' Calculate determinant
Dim determinant As Double = Det(matrix)
' Display the result in a designated textbox
result.Text = determinant.ToString()
Else
' Handle the case where there was an error in input
result.Text = "Error in input. Please check values."
End If
End Sub
Function Minor(a As Double(,), x As Integer, y As Integer) As Double(,)
Dim length = a.GetLength(0) - 1
Dim result(length - 1, length - 1) As Double
For i = 1 To length
For j = 1 To length
If i < x AndAlso j < y Then
result(i - 1, j - 1) = a(i - 1, j - 1)
ElseIf i >= x AndAlso j < y Then
result(i - 1, j - 1) = a(i, j - 1)
ElseIf i < x AndAlso j >= y Then
result(i - 1, j - 1) = a(i - 1, j)
Else
result(i - 1, j - 1) = a(i, j)
End If
Next
Next
Return result
End Function
Function Det(a As Double(,)) As Double
If a.GetLength(0) = 1 Then
Return a(0, 0)
Else
Dim sign = 1
Dim sum = 0.0
For i = 1 To a.GetLength(0)
sum += sign * a(0, i - 1) * Det(Minor(a, 0, i))
sign *= -1
Next
Return sum
End If
End FunctionEditor is loading...
Leave a Comment