Help needed sorting a list

brainache

Member
Joined
Mar 18, 2008
Messages
5
Programming Experience
Beginner
Hi folks
I've got this little bit of code going on...

VB.NET:
Sub Main()
        Dim litre As Double = 1, gallon As Double = 0, litre2 As Double = 0, gallon2 As Double = 0
        Dim halfGallons As Integer = 0, prevHalfGallons As Integer = 0

        Console.WriteLine("Conversion chart for Litres-Gallons")
        Console.WriteLine("Litres Gallons")

        While litre < 11
            gallon = litre * 0.22
            halfGallons = CInt((gallon * 2))
            If halfGallons > prevHalfGallons Then
                prevHalfGallons = halfGallons
                gallon2 = halfGallons / 2
                litre2 = gallon2 / 0.22
                Console.WriteLine("{0:F2} {1:F2}", litre2, gallon2)
            End If
            Console.WriteLine("{0:F3} {1:F3}", litre, gallon)
            litre += 1
        End While
        Console.ReadLine()
... which pretty much does what i want it to do, but i can't get it to sort so that the litre are in numerical order.
Is there anyome out there that is kind enough to help?
Thanks in advance
 
i dont understand how this is NOT in numerical order.. the litre variable is in a loop and it rises by 1 each time!
 
Yeah, that's what has stumped me.
The whole idea is that it runs through the litres (1 to 10), but also shows the conversion for each half gallon. When i run it, the half gallon conversions (i.e 2.27 litre = 0.500 gallons) appear between the 1.000 litre and 2.000 litres.
I'm stumped.
 
the problem is when you output the half Gallons, the value of the liter2 can be greater than liter

One fix:

VB.NET:
 Sub Main()
        Dim litre As Double = 1, gallon As Double = 0, litre2 As Double = 0, gallon2 As Double = 0
        Dim halfGallons As Integer = 0, prevHalfGallons As Integer = 0
        Dim container As New SortedDictionary(Of Double, Double)

        Console.WriteLine("Conversion chart for Litres-Gallons")
        Console.WriteLine("Litres Gallons")

        While litre < 11
            gallon = litre * 0.22
            halfGallons = CInt((gallon * 2))
            If halfGallons > prevHalfGallons Then
                prevHalfGallons = halfGallons
                gallon2 = halfGallons / 2
                litre2 = gallon2 / 0.22
                container.Add(litre2, gallon2)
            End If
            container.Add(litre, gallon)
            litre += 1
        End While
               For Each LiterGallonPair As KeyValuePair(Of Double, Double) In container
            Console.WriteLine("{0:F3} {1:F3}", LiterGallonPair.Key, LiterGallonPair.Value)
        Next
        Console.ReadLine()
    End Sub
 
Here is a much simpler logic:

VB.NET:
    Dim litres As Double = 0, gals As Double = 0

    While litres < 11
      litres += 1

      Dim galsAsLitres As Double = gals / 0.22


      If galsAsLitres < litres Then
        Console.WriteLine("{0:F3} {1:F3}", galsAsLitres, gals)
        gals += 0.5
      End If

      Console.WriteLine("{0:F3} {1:F3}", litres, litres * 0.22)
    End While

What we do is convert the gallons to litres on every pass of the loop, and if the gallons (as litres) are less than the current litres, we print them, then add half a gallon.

In this way, you can think of it as a race between glalons and litres.. litre plods along slow and steady, gallon bounds ahead and then stops for a while. At any one moment, we print the smallest value we find.. i.e. we always print litres, and we only print gallons when it is lagging behind litres. If we ensure that we print a gallon when it is lagging, then we are always going to print the smallest entry, because as soon as we print gallon, we put it back in the lead

;)
1.000 0.220
2.000 0.440
2.273 0.500
3.000 0.660
4.000 0.880
4.545 1.000
5.000 1.100
6.000 1.320
6.818 1.500
7.000 1.540
8.000 1.760
9.000 1.980
9.091 2.000
10.000 2.200
 
Back
Top