Create a VBA code (in R1C1 format) for a sub-function that sort the values in co
ID: 3814784 • Letter: C
Question
Create a VBA code (in R1C1 format) for a sub-function that sort the values in column 4 (see the picture below) in order of increasing value. While sorting make sure you also sort/change the entire values of the respective row. Explain your code by commenting. For this problem use For Next Loop, Arrays and If Statements.
The final result of the sorting should be expressed as the picture below:
eee aaa ddd 345 dd 432 ! ssss bbbbb| 123 | eee | gggg | 234 fff d- s s- e-f 5 E a s- e-f 4 DO 5234 4D04323 34-2 e-d bg 3ced bg ed bg 2B-2345 | A 1 2 3 456 78Explanation / Answer
Imports System
Namespace Sample
Public Class Sort
Public Shared Sub Main(ByVal args() As String)
sortFunction()
End Sub
Friend Shared Sub sortFunction()
Dim col As Integer = 4 //To Assign the column number
//To Assign the Table row and column size: The following call to the 'RectangularArrays' helper class reproduces the rectangular array initialization
Dim arr()() As Integer = RectangularArrays.ReturnRectangularIntegerArray(5, 5)
Console.WriteLine("Enter elements")
For i As Integer = 0 To arr.Length - 1
For j As Integer = 0 To arr.Length - 1
Dim s As New Scanner(System.in)
arr(i)(j) = s.Next()
Next j
Next i
For i As Integer = 0 To arr.Length - 1
For j As Integer = i+1 To arr.Length - 1
If arr(i)(col-1) > arr(j)(col-1) Then
Dim temp As Integer
For j2 As Integer = 0 To arr.Length - 1
temp = arr(j)(j2)
arr(j)(j2)=arr(i)(j2)
arr(i)(j2) = temp
Next j2
End If
Next j
Next i
For i As Integer = 0 To arr.Length - 1
For j As Integer = 0 To arr.Length - 1
Console.Write(arr(i)(j) & " ")
Next j
Console.WriteLine()
Next i
End Sub
End Class
End Namespace
Helper Class:
Friend Module RectangularArrays
Friend Function ReturnRectangularIntegerArray(ByVal size1 As Integer, ByVal size2 As Integer) As Integer()()
Dim newArray As Integer()() = New Integer(size1 - 1)() {}
For array1 As Integer = 0 To size1 - 1
newArray(array1) = New Integer(size2 - 1) {}
Next array1
Return newArray
End Function
End Module