sorting arrays

Sorting parallel arrays

Page 1 of 1

6 Replies - 3412 Views - Last Post: 31 December 2010 - 10:18 AM Rate Topic: -----

#1 deVirus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 16-December 10

sorting arrays

Posted 30 December 2010 - 09:22 AM

hi guys i have a nother one here, this time i am supposed to enter 10 ice creams flavours and the corresponding 10 sales made after which when the first array is sorted the corresponding value in the second array is also sorted.


here is the code, your help is much appricated.

Public Class Form1

    Dim flavour(10) As String
    Dim numSold(10) As Integer


    Private Sub calx_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calx.Click


        Dim count As Integer = 0
        Dim fla As String
        Dim num As String
        Dim err As Boolean = False


        Do Until count = 10

            fla = InputBox("Enter the name flavour number " & count + 1, "Enter Flavour")
            If fla = "" Then
                MessageBox.Show("enter flavou name", "empty feild")
                err = True
            Else

                If IsNumeric(fla) Then
                    MessageBox.Show("enter a name not a number", "name error")
                    err = True

                Else : err = False


                    num = InputBox("Enter the number of cones sold for flavour " & count + 1)
                    If num = "" Then
                        MessageBox.Show("enter a number into  dis feild", "empty feild")
                        err = True
                    Else
                        If Not IsNumeric(num) Then
                            MessageBox.Show("enter a numeric character into dis feild", "input error")
                            err = True
                        Else
                            flavour(count) = fla
                            numSold(count) = num
                            count += 1


                        End If
                    End If
                End If
            End If


        Loop
        calav.Enabled = True

    End Sub

    Private Sub calav_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calav.Click
        lbox.Items.Clear()
        Dim cost As Decimal = Val(tbox.Text)
        Dim tcones As Integer
        Dim err As Boolean = False


        If (tbox.Text = "") Then
            Beep()
            MessageBox.Show("Pls enter the cost of the icecreame cone", "Empty Codt feild")
            err = True

        ElseIf err = False Then

            If Not IsNumeric(tbox.Text) Then
                Beep()
                MessageBox.Show("Pls enter a numeric cost of a single cone", "Input Error")
                err = True
            ElseIf err = False Then



                Dim c As Integer
                For c = 0 To UBound(flavour) - 1
                    lbox.Items.Add(flavour(c) & vbTab & numSold(c))
                    tcones += numSold(c)
                Next
            End If
        End If

        disp.Text = "The total number of cones sold is " & tcones
        avgcon.Text = "The average number of each cone sold is " & tcones / 10
        amtsold.Text = "The total amount of cones sold is GH¢ " & Format(tcones * cost, "standard")


    End Sub



    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        calav.Enabled = False

    End Sub
End Class

This post has been edited by modi123_1: 30 December 2010 - 09:55 AM
Reason for edit:: please use code tags


Is This A Good Question/Topic? 0
  • +

Replies To: sorting arrays

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: sorting arrays

Posted 30 December 2010 - 11:19 AM

Well, you need to implement a sorting algorithm like bubblesort. The trick is, each time a swap is made in the bubblesort for the first array, make sure you also swap the corresponding values in the second array at the same time. Not hard at all. You can find a VB bubblesort out there for sure, do a quick search and then modify it to swap your second array each time the first one is swapped.

Post your code to us, with the bubblesort, if you continue to have an issue.


;)

This post has been edited by Martyr2: 30 December 2010 - 11:21 AM

Was This Post Helpful? 0
  • +
  • -

#3 dbasnett  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 109
  • View blog
  • Posts: 603
  • Joined: 01-October 08

Re: sorting arrays

Posted 30 December 2010 - 11:56 AM

    Private Sub Button1_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) Handles Button1.Click
        'your arrays with test data
        Dim flavour() As String = New String() {"Vanilla", "Chocolate", "Flav1", "Flav4"}
        Dim numSold() As Integer = New Integer() {1, 2, 3, 4}
        sortArrays(flavour, numSold)
    End Sub
    ''' <summary>
    ''' Sorts two arrays of Equal Length
    ''' </summary>
    ''' <param name="priArray">This array is sorted</param>
    ''' <param name="secArray">Items in this array have a one-to-one correspondence with priArray</param>
    ''' <remarks>Note the arguments are passed ByRef!!!</remarks>
    Private Sub sortArrays(ByRef priArray As String(), ByRef secArray As Integer())
        Array.Sort(priArray, secArray)
    End Sub


Was This Post Helpful? 0
  • +
  • -

#4 deVirus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 16-December 10

Re: sorting arrays

Posted 30 December 2010 - 07:35 PM

what if i just wanted to find the highest and lowest value in the array without sorting it?
Was This Post Helpful? 0
  • +
  • -

#5 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,074
  • Joined: 02-July 08

Re: sorting arrays

Posted 30 December 2010 - 08:20 PM

The maximum value:
Dim ints As Integer() = {2,6,7,1,9}
ints.Max



The minimum value:
Dim ints As Integer() = {2,6,7,1,9}
ints.Min


This post has been edited by hawkvalley1: 30 December 2010 - 08:58 PM

Was This Post Helpful? 0
  • +
  • -

#6 deVirus  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 16-December 10

Re: sorting arrays

Posted 31 December 2010 - 09:40 AM

I am currently using vb.net 2005,
i tried the array.min and array.max,
but it seems not to be working,

if there is any other way i would apprediate it.
Was This Post Helpful? 0
  • +
  • -

#7 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1046
  • View blog
  • Posts: 4,074
  • Joined: 02-July 08

Re: sorting arrays

Posted 31 December 2010 - 10:18 AM

Must be a Framework 3.5+ thing. Consider upgrading if you can, there are many great things in 2008 and even more in 2010.

If you don't want to change the original array make a temp one and sort it then the first element is the Min and the last element is the Max.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1