Jump to content

Sort random numbers...

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Surfensteve

Surfensteve

    Newbie

  • Members
  • Pip
  • 5 posts
I am sorting 20 numbers at this point and cannot get it to sort the numbers but instead produce a value of zero only. The code produces 20 random numbers, but I need it in "Button2" to sort the numbers into descending order (Starting with the largest and going to the smallest), What needs to be done to fix it and to make it produce the numbers descending order in ListBox2?

You will see the see below in big bold letters what code needs to be fixed....

Public Class Form1

    Public SampleArray(20) As Double

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click


        Dim randomNum As New Random()

        Dim i As Integer

        Dim SampleArray(20) As Double

        For i = 0 To 20

            SampleArray(i) = randomNum.Next(1, 100)

            ListBox1.Items.Add(SampleArray(i).ToString)

        Next



    End Sub

Below is the code I need help with.


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click


        Dim randomNum As New Random()

        Dim i As Integer

        Dim SampleArray(20) As Double

        Dim VarTemp, VarNumber1, VarNumber2 As Integer


        'Array.Sort(SampleArray)


        varTemp = varNumber1

        varNumber1 = varNumber2

        varNumber2 = varTemp





        ListBox2.Items.Add(SampleArray(i).ToString)


    End Sub

End Class

Edited by Roger, 07 March 2011 - 05:23 PM.
added code block


#2
wixifo

wixifo

    Newbie

  • Members
  • PipPip
  • 13 posts
I just recreated your program and fixed your bug, I commented the code to help you.


Public Class Form1

    Private SampleArray(20) As Double

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim randomNum As New Random()

        For i = 0 To 20

            SampleArray(i) = randomNum.Next(1, 100)

            ListBox1.Items.Add(SampleArray(i))

        Next

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        ListBox1.Items.Clear() 'Clear the listbox items, you don't need them since your numbers are stored in your array

        Array.Sort(SampleArray) 'Sort the array, pretty straight forward

        For i = 0 To 20

            ListBox1.Items.Add(SampleArray(i)) 'Add the numbers in the listbox again, but this time the array is sorted, so the numbers will get in the listbox in that order

        Next

    End Sub

End Class


Theoretically, you don't even need the array, you could sort directly from the listbox.

Hope that helps.

#3
Surfensteve

Surfensteve

    Newbie

  • Members
  • Pip
  • 5 posts
I tried it but it just prints out 20 0's instead...

STeve

#4
Surfensteve

Surfensteve

    Newbie

  • Members
  • Pip
  • 5 posts
************Actually, I got it to work, Thanks!




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users