Results 1 to 25 of 25

Thread: 2D array to 1D + bubble sort

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    24

    2D array to 1D + bubble sort

    hi, i don't know the best way to do this but i have a question here:

    i have a button on my form "display all customers". i now need to transfer all customers from the structured 2D array to the 1D array "customers"
    and then sort this with a bubble sort.

    *****Previous to this i made a 2D array with that has stored seats that are 10 rows and from A to J ( eg. A1 A2.....J2, J3)
    I have then made code that allows you to add a customer name into the array and then display it. also having a button that removes that customer.

    i have uploaded some of my code from the programs. i have left out my functions code as they work like the display seats, add customers and remove customers.

    Interface


    Code 1


    Code 2


    Code 3

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 2D array to 1D + bubble sort

    Are you required to use a bubble sort by some assignment requirement or the like? If not then you should just use the Array.Sort method and then provide a Comparison delegate or IComparer object and let the Array class handle the sorting.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    24

    Re: 2D array to 1D + bubble sort

    yeah i have to use the dam bubble sort cause its required.

    if u have a look at the pictures i have some sort of bubble sort happening.

    i just don't no what the error in code is. and if Im on the right track thats all.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 2D array to 1D + bubble sort

    I don't much want to read code in screen shots. If you want to post code then include it in your post and wrap it in code tags. Other screen shots should just be attached directly to your post.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    24

    Re: 2D array to 1D + bubble sort

    lol its jus easy to jus click on the link aint it ?

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    24

    Re: 2D array to 1D + bubble sort

    Code:
    Public Class Form1
        Dim myArray(99) As seat
        Public Structure seat
            Dim seatNo As String
            Dim custName As String
        End Structure
        Dim seats(9, 9) As seat
    
        Private Sub mnuDisplaySeats_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuDisplaySeats.Click
            displaySeats()
        End Sub
    
        Private Sub mnuAddCust_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuAddCust.Click
            AddCust()
            displaySeats()
        End Sub
    
        Private Sub mnuRemoveCust_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuRemoveCust.Click
            RemoveCust()
            displaySeats()
        End Sub

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            'Load combo
            Dim letterNum As Integer
            Dim myStr As String
    
            For letterNum = Asc("A") To Asc("J")
                For seatNum As Integer = 1 To 10
                    myStr = Chr(letterNum) & seatNum.ToString.Trim
                    Me.cboSeats.Items.Add(myStr)
                Next
            Next
            Me.cboSeats.SelectedIndex = 0
    
            'load the 2d array
            Dim row, col As Integer
            Dim letter As Char
            Dim num As Integer = 65
            Dim str As String
    
            For row = 0 To 9
                letter = Chr(num)
                For col = 0 To 9
                    str = letter & (col + 1)
                    seats(row, col).seatNo = str
                    seats(row, col).custName = "avail"
                Next
                str = ""
                num += 1
            Next
        End Sub


    Here is the procedures to add the customer and so on
    Code:
     Private Sub displaySeats()
    
            Dim row, col As Integer
    
    
            Dim seatStr As String = ""
            Dim custStr As String = ""
    
            Me.lstSeats.Items.Clear()
    
            For row = 0 To 9
    
                For col = 0 To 9
    
                    seatStr = seatStr & seats(row, col).seatNo & Chr(9)
    
                Next
                Me.lstSeats.Items.Add(seatStr)
                seatStr = ""
    
                For col = 0 To 9
                    custStr = custStr & seats(row, col).custName & Chr(9)
                Next
                Me.lstSeats.Items.Add(custStr)
                custStr = ""
            Next
        End Sub
        Private Sub AddCust()
            Dim row, col As Integer
    
            For row = 0 To 9
                For col = 0 To 9
                    If seats(row, col).seatNo = cboSeats.SelectedItem And seats(row, col).custName = "avail" Then
                        seats(row, col).custName = txtCustomer.Text
                        MsgBox("Customer Has Been Added")
    
                        Exit Sub
                    End If
    
    
                Next
            Next
            MsgBox("Customer spot has been taken")
        End Sub
        Private Sub RemoveCust()
            Dim row, col As Integer
    
            For row = 0 To 9
                For col = 0 To 9
                    If seats(row, col).seatNo = cboSeats.SelectedItem And seats(row, col).custName = txtCustomer.Text Then
                        seats(row, col).custName = "avail"
                        MsgBox("Customer Has Been Removed")
                    End If
                Next
            Next
        End Sub

    Here is where i have trouble with my question

    Code:
    Private Sub lstSeats_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstSeats.SelectedIndexChanged
    
        End Sub
    
        Private Sub DisplayAllCustome*******tripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisplayAllCustome*******tripMenuItem.Click
    
            bubbleSortArray(myArray)
    
            lstSeats.Items.Clear()
            For i As Integer = 0 To 99
                lstSeats.Items.Add(myArray(i).custName)
            Next
    
            'Dim temp As String
            'For passNum As Integer = 0 To 99
    
            '    For i As Integer = 0 To 100
    
            '        If seats(i - 1) > seats(0) Then
            '            temp = seats(i - 1)
            '            seats(i - 1) = seats(i)
            '            seats(i) = temp
            '        End If
            '    Next
            'Next
        End Sub
        Public Sub bubbleSortArray(ByVal inArray() As Integer)
    
            Dim temp As Integer
    
    
            Dim numPasses As Integer, numItems As Integer
    
            numPasses = inArray.GetUpperBound(0)
            numItems = inArray.GetUpperBound(0) + 1
    
    
            For passNum As Integer = 1 To numPasses
    
                For i As Integer = 1 To numItems - passNum
    
                    If inArray(i - 1) > inArray(i) Then
                        temp = inArray(i - 1)
                        inArray(i - 1) = inArray(i)
                        inArray(i) = temp
                    End If
    
                Next
            Next
    
        End Sub
    End Class

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 2D array to 1D + bubble sort

    So what exactly is the problem?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    24

    Re: 2D array to 1D + bubble sort

    ok well.
    i no the chunks of code probably mean nothin to u and yeah.
    well my problem is i have a button on my form "display all customers". i now need to transfer all customers from the structured 2D array to the 1D array "customers"
    and then sort this with a bubble sort.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 2D array to 1D + bubble sort

    Yes, you already stated the aim and yes, you have also posted the code you have. Now what are we to do with it? You haven't told us what the actual problem is. Are we to read that code in the hope of something jumping out at us? We don't even know what we're looking for. You're saying "this is what I want to do, here's my code, fix it". That doesn't really cut it. You should be telling us how you're trying to do it and where it falls over.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    24

    Re: 2D array to 1D + bubble sort

    ok well. the error that comes up first is with the

    "lstSeats.Items.Add(myArray(i).custName)" and the error is that not a member of Integer

    and i dont no wat this error means .

    thank you

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 2D array to 1D + bubble sort

    Excellent, although it shouldn't take us 10 posts to get to the error message. If you want us to help with a compilation or run time error then the error message is the first thing you should be posting.

    Here's the signature of your sorting method:
    Code:
    Public Sub bubbleSortArray(ByVal inArray() As Integer)
    Now here's how you're calling it:
    Code:
    bubbleSortArray(myArray)
    Now here's how you are declaring myArray:
    Code:
    Dim myArray(99) As seat
    Do you see a compatibility problem there? You've written a method that will sort an array of Integers. That's nice, but how many Integer arrays have you got that you want sorted? None. You do have an array of Seats though, so wouldn't you need a method that sorts a Seat array?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    24

    Re: 2D array to 1D + bubble sort

    sorry about that.

    so i should take the bubble sort(myarray) out ??

    what im having trouble is with what should i be doing next. i understand what u mean but how to implement it

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 2D array to 1D + bubble sort

    You written a method that accepts an array of Integers and sorts them by comparing each pair of Integers and swapping them if they are in the wrong order. That's no good for sorting Seat objects. You need to write a method that accepts an array of Seats and sorts them by comparing each pair of Seats objects and swapping them if they are in the wrong order.

    Presumably comparing two Seat objects means comparing their SeatNo properties.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    24

    Re: 2D array to 1D + bubble sort

    ok i understand now

    i no your not meant to give me complete slabs of code. but with the code stuff that you posted, what should i change from that. or should i add something completely new and add it to what i have ?


    sorry if i sound like a complete noob. im just strugglin in VB at uni atm

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 2D array to 1D + bubble sort

    You should change this:
    vb.net Code:
    1. Public Sub bubbleSortArray(ByVal inArray() As Integer)
    to this:
    vb.net Code:
    1. Public Sub bubbleSortArray(ByVal inArray() As seat)
    and then inside that method you need to compare seat objects instead of Integer values.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    24

    Re: 2D array to 1D + bubble sort

    ok thanks, when i do this tho more errors occur, they mayb simple fixes but here are the errors

    Under the button -

    Code:
            bubbleSortArray(myArray)   -error
    
            lstSeats.Items.Clear()
            For i As Integer = 0 To 99
                lstSeats.Items.Add(myArray(i).custName)    - error
            Next
    And in the bubble sort -
    Code:
            Dim temp As Integer
    
    
            Dim numPasses As Integer, numItems As Integer
    
            numPasses = inArray.GetUpperBound(0)
            numItems = inArray.GetUpperBound(0) + 1
    
    
            For passNum As Integer = 1 To numPasses
    
                For i As Integer = 1 To numItems - passNum
    
                    If inArray(i - 1) > inArray(i) Then   -error
                        temp = inArray(i - 1)               -error
                        inArray(i - 1) = inArray(i)
                        inArray(i) = temp                     -error 
                    End If

  17. #17
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 2D array to 1D + bubble sort

    You're still comparing the elements of the array inside the bubbleSortArray method as though they were Integers. They are NOT Integers. They are Seat objects. You can't compare two Seat objects like this:
    vb.net Code:
    1. If inArray(i - 1) > inArray(i) Then
    and you have declared the 'temp' variable as type Integer but you cannot assign a Seat object to an Integer variable like this:
    vb.net Code:
    1. temp = inArray(i - 1)
    I will say this one last time: you have to write a method that takes a Seat array and then compares and sorts the seat objects it contains. The array does NOT contain Integers so you cannot compare the elements as Integers. Every element in the array is a Seat object so you have to compare them as Seat objects, NOT Integers.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  18. #18

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    24

    Re: 2D array to 1D + bubble sort

    where can i get an example of a method that takes a Seat array and then compares and sorts the seat objects it contains

    because im having trouble understanding

    do i change these to all seat objects =
    Code:
    Dim temp As Integer
    
    
            Dim numPasses As Integer, numItems As Integer

    Code:
     For passNum As Integer = 1 To numPasses
    
                For i As Integer = 1 To numItems - passNum

  19. #19
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 2D array to 1D + bubble sort

    There is no example because it's your class. How could anyone else know how to sort an array of objects of a type that you created, or that your teacher created?

    Think about this. Let's say that you have a line of people. I tell you to sort them by height using a bubble sort algorithm. What would you do? You'd work your way through the people in the line, comparing their height and swapping them if they were in the wrong order. At the end you'd have a line of people ordered by height. Now what if I said to reorder them by age? What would you do? You'd do the same thing but you'd compare the people's ages instead of their heights. Then what if I told you to to order them alphabetically by name? These are all easy things to do, right? Now, what if you had a line of seats and I told you to order them by seat number? That would be no more difficult would it?

    Well, that's exactly what you do have, just in code instead of physically. That said, I am just assuming that sorting chairs means by seat number. You haven't actually told us.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  20. #20

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    24

    Re: 2D array to 1D + bubble sort

    oh i must sound like a complete tool. anyway

    its sorting by the customer. = custName i think

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    24

    Re: 2D array to 1D + bubble sort

    also where have i gone wrong. can u see anywhere that stands out. and have i transfered it from 2D array to a 1D ??

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    24

    Re: 2D array to 1D + bubble sort

    should i also change this

    Code:
    Public Sub bubbleSortArray(ByVal inArray() As seat)
    to myArray as in under the display cust button it has

    Code:
      bubbleSortArray(myArray)

  23. #23
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 2D array to 1D + bubble sort

    Think it through. The point of the exercise is to sort an array of Seat objects customer name. You have to write a method to do the sorting. If it's going to sort a Seat array then it obviously has to take a seat array as a parameter. Inside that method you have to compare the Seat objects in the array in order to sort them. If they're supposed to be sorted by customer name then obviously you have to compare their custName properties. Finally, if this method takes a Seat array as a parameter then when you call it you obviously have to pass it a Seat array.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  24. #24

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    24

    Re: 2D array to 1D + bubble sort

    Something like this ??

    Code:
    Private Sub mnuDisplaycust_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuDisplaycust.Click
            lstSeats.Items.Clear()
    
            arraytransfer()
            bubblesort()
            For i As Integer = 0 To 99
                If allcustomers(i).custName <> "Avail" Then
    
    
                    lstSeats.Items.Add(allcustomers(i).custName & " " & allcustomers(i).seatNo)
    
                End If
    
            Next
            MsgBox("Scroll down for customers", MsgBoxStyle.Exclamation, "Heads-Up")
    
            'displays all the customers names that are not equal to avail.
    
        End Sub
    Code:
      Private Sub bubblesort()
            Dim temp As seat
            For passNum As Integer = 1 To 100
                For i As Integer = 1 To 100 - passNum
                    If (allcustomers(i - 1).custName > allcustomers(i).custName) Then
                        temp = allcustomers(i - 1)
                        allcustomers(i - 1) = allcustomers(i)
                        allcustomers(i) = temp
    
                    End If
                Next
            Next
            'bubblesort rearranges all the customers in the array alphabetically
        End Sub

  25. #25
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: 2D array to 1D + bubble sort

    You appear to be on the right track.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width