Rationalise (Canonise) the Fractions before Sorting
Before fractions are sorted, they are rationalised;
i.e., they are put in a form where their denominators become
the same. This same denominator is the LCM of the
denominators of all the separate fractions.
After this is done, the new numerators can easily be sorted
in a preferred order.
Steps for Sorting of Fractions VB.Net code
The following steps will guide us in writing our code.
Let's illustrate with 5/9,
3/7, 1/2
Step 1:
Find the LCM of the denominators.
⇒ LCM of 9, 7 & 2 = 126
Step 2:
In a turn by turn fashion, divide the found LCM from Step 1
by each denominator, multiplying the quotient by the corresponding numerator.
⇒
((5 x 14), (3 x 18), (1 x 63))/126
= (70, 54, 63)/126
Step 3:
Go ahead and sort the numerators in our order of choice.
⇒ In ascending order:
54/126, 63/126,
70/126
=
3/7, 1/2,
5/9
Create a new class file; Project, Add Class.
Call it SortFraction.vb.
Optionally, create a new module file; Project, Add Module.
Call it SortFractionModule.vb.
Type out the adjoining Visual Basic (VB.Net) codes for sorting fractions in ascending and descending orders.
Note: You can instead comment out the VB.Net code in
the main module from the previous lesson or simply continue from where it stopped.
So!
Feel free to try out your own set of numerators and denominators for fractional sorting.
VB.Net code for SortFraction Class File
Public Class SortFraction
Inherits AddFraction
Dim final_numerators() As Integer
Dim final_denominators() As Integer
Dim copy_numerators() As Integer
Dim send_back As New Dictionary(Of String, Integer())
Private Sub takeOff()
ReDim final_numerators(numerators.Count - 1)
ReDim final_denominators(denominators.Count - 1)
ReDim copy_numerators(numerators.Count - 1)
canonizeFraction()
For index = 0 To new_numerators.Count - 1
copy_numerators(index) = new_numerators(index)
Next
End Sub
Private Sub Land()
Dim position As Integer
For index = 0 To copy_numerators.Count - 1
position = Array.IndexOf(new_numerators, copy_numerators(index))
final_numerators(index) = numerators(position)
final_denominators(index) = denominators(position)
Next index
send_back.Add("numerators", final_numerators)
send_back.Add("denominators", final_denominators)
End Sub
Public Function sortAscending() As Dictionary(Of String, Integer())
takeOff()
Array.Sort(copy_numerators)
Land()
Return send_back
End Function
Public Function sortDescending() As Dictionary(Of String, Integer())
takeOff()
Array.Sort(copy_numerators)
Array.Reverse(copy_numerators)
Land()
Return send_back
End Function
End Class
Main Module
Module Algebra_SortFraction
Sub Main()
Dim numerators = {9, 3, 5, 7}
Dim denominators = {2, 4, 12, 18}
Dim fractions As New Dictionary(Of String, Integer())
fractions.Add("numerators", numerators)
fractions.Add("denominators", denominators)
Console.WriteLine(" Sorting in ascending order the fractions:" & vbCrLf)
For Each numerator In fractions.Item("numerators")
Console.Write(String.Format("{0,13}", numerator))
Next
Console.Write(Environment.NewLine & String.Format("{0,12}", " "))
For wasted = 0 To numerators.Count - 2
Console.Write(String.Format("{0}", "- , "))
Next
Console.WriteLine(String.Format("{0,1}", "-"))
For Each denominator In fractions.Item("denominators")
Console.Write(String.Format("{0,13}", denominator))
Next
Console.WriteLine("")
Dim sort_fract As New SortFraction
sort_fract._init_(fractions)
fractions = sort_fract.sortAscending()
Console.WriteLine(Environment.NewLine)
For Each numerator In fractions.Item("numerators")
Console.Write(String.Format("{0,13}", numerator))
Next
Console.Write(Environment.NewLine & String.Format("{0,12}", "Answer = "))
For wasted = 0 To numerators.Count - 2
Console.Write(String.Format("{0}", "- , "))
Next
Console.WriteLine(String.Format("{0,1}", "-"))
For Each denominator In fractions.Item("denominators")
Console.Write(String.Format("{0,13}", denominator))
Next
End Sub
End Module