Results 1 to 9 of 9

Thread: [RESOLVED] 2D Character Array

  1. #1

    Thread Starter
    New Member Jaycoba's Avatar
    Join Date
    Nov 2017
    Location
    In the sands of time.
    Posts
    13

    Resolved [RESOLVED] 2D Character Array

    A little back story... my teacher is the absolute pinnacle of monotony. His voice, his actions, his teaching style. We are expected to understand things from a hand out that is read to us, but not explained thoroughly, that has scattered information written in various, semi-related code structures. When I ask for help, he tells me what to do rather than guiding me which helps me retain about 0.5% of the information. I've asked him to stop telling me what to do to get him to point me in the right direction to no avail. We have class once a week. Between work and other classes, I barely have time to seek him out for help, let alone my multiple attempts of emailing him.

    I cannot for the life of me find the information I need online. The things we do in his class don't seem to be covere in the countless pages I've read. I'm not looking for this to be done for me, I'm looking for answers on how exactly this stuff works. Forgive me if I do a horrible job at explaining what I'm trying to figure out.

    We are working on 2D character arrays. The lab is as follows:

    Obtain from the Instructor a list of the College faculty personnel (22 individuals, first name, comma[,], first letter of last name, period[.]) This list is presented in descending order of seniority. Enter the data in the same order, as a 2-dimensional array of character data, and then perform a bubble sort on the data so that it is listed in ascending (A-Z) alphabetical order. Have the sorted data print out on the screeen, one name per line. For 2 bonus points, sort the array with the data as a 1-dimensional array of strings.

    My code, and I hope it's good up to this point, is as follows;

    Code:
    Public Class Form1
    
        'Variables
        'count1, count2, people within array; respective order
        Dim c1, c2, c3, c4 As Integer
        Dim person As Char
    
        'Seniority List Array
        Dim seniorityList(,) As Char =
            {{"S", "h", "e", "p", "h", "e", "r", "d", ",", "W", ".", " "},
             {"S", "z", "y", "m", "a", "n", "s", "k", "i", ",", "D", "."},
             {"M", "a", "r", "k", "o", " ", " ", ",", "G", ".", " ", " "},
             {"M", "a", "h", "a", "s", ",", "T", ".", " ", " ", " ", " "},
             {"H", "o", "w", "a", "r", "d", ",", "J", ".", " ", " ", " "},
             {"F", "r", "a", "z", "i", "e", "r", ",", "R", ".", " ", " "},
             {"S", "h", "u", "l", "t", "z", ",", "N", ".", " ", " ", " "},
             {"K", "e", "n", "d", "a", "l", ",", "L", ".", " ", " ", " "},
             {"R", "a", "t", "l", "i", "f", "f", ",", "R", ".", " ", " "},
             {"M", "c", "R", "i", "t", "c", "h", "i", "e", ",", "T", "."},
             {"G", "o", "e", "d", "d", "e", ",", "B", ".", " ", " ", " "},
             {"W", "e", "d", "d", "i", "n", "g", ",", "D", ".", " ", " "},
             {"K", "a", "c", "z", "i", "n", "s", "k", "i", ",", "M", "."},
             {"P", "h", "i", "e", "l", "s", ",", "D", ".", " ", " ", " "},
             {"B", "e", "t", "h", "e", "a", ",", "A", ".", " ", " ", " "},
             {"F", "r", "a", "z", "i", "e", "r", ",", "B", ".", " ", " "},
             {"D", "u", "r", "i", "s", ",", "A", ".", " ", " ", " ", " "},
             {"M", "i", "c", "h", "a", "e", "l", "s", ",", "L", ".", " "},
             {"S", "h", "u", "s", "t", "e", "r", ",", "L", ".", " ", " "},
             {"K", "i", "n", "k", "a", "d", "e", ",", "C", ".", " ", " "},
             {"H", "a", "a", "r", ",", "R", ".", " ", " ", " ", " ", " "},
             {"W", "e", "i", "l", "n", "a", "u", ",", "H", ".", " ", " "}}
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            For c1 = 0 To 21
                For c2 = 21 To 1 Step -1
                    For c3 = 0 To 11
                        If (seniorityList(c2, c3) < seniorityList(c2 - 1, c3)) Then
                            For c4 = 0 To 11
                                person = seniorityList(c2, c4)
                                seniorityList(c2, c4) = seniorityList(c2 - 1, c4)
                                seniorityList(c2 - 1, c4) = person
                            Next c4
                            Exit For
                        ElseIf (seniorityList(c2, c3) = seniorityList(c2 - 1, c3)) Then
                            Continue For
                        Else : Exit For
                        End If
                    Next c3
                Next c2
            Next c1
        End Sub
    End Class
    I DO NOT know how to print it to the screen correctly. When I try, I use a multi-lined textbox and it ends up being all on one line, jumbled, and with extra W's and other random duplicated letters. I've no idea what to do next or what I'm doing wrong and the teacher won't explain other than, "Look at your handouts." and that's not helping me with this particular step of the lab.

    If someone could just point me in the right direction. I'm beyond frustrated with this.

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: 2D Character Array

    Welcome to VBForums

    Quote Originally Posted by Jaycoba View Post
    I use a multi-lined textbox and it ends up being all on one line,
    As you haven't shown code for the printing part (it is always best to, even if it doesn't work properly), I would guess that you aren't specifying where to add new lines, which you can do like this:

    Code:
    TextBox1.Text = TextBox1.Text & Environment.Newline
    jumbled, and with extra W's and other random duplicated letters.
    That is a bigger problem, and looking at your loops I can see an issue...

    You are looping columns/columns/rows, and at that point you check the order (of the column), and if they need to be swapped you loop rows.

    As you are supposed to be outputting names, you should be swapping the entire row, rather than one element of it. As such you should be looping rows/rows/columns, check the order (of the entire row), and if swapping loop columns.

  3. #3
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: 2D Character Array

    And it would be good to learn how to use the debugging capability of the IDE to step through your program and observer what it is doing at each step to see if it is doing what you expect at that step. You can find issues fairly quickly that way most of the time as opposed to looking at the code and "executing" the logic in your head.

    You can search for some links on debugging, one of the first ones I hit looks like it might cover the bases.

  4. #4
    Fanatic Member
    Join Date
    May 2004
    Location
    South Charleston, WV, USA
    Posts
    607

    Re: 2D Character Array

    Have you tried Console.Writeline?

    I realize your teacher isn't volunteering much information. But just to clarify, did the teacher specify that you're supposed to use a text box for this?

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    Re: 2D Character Array

    Quote Originally Posted by Jaycoba View Post
    A little back story... my teacher is the absolute pinnacle of monotony. His voice, his actions, his teaching style.
    BWHAHAHAHAHAHA!!!

    Wow do I remember a teacher like that. He was a nice guy, but his voice was so lulling and monotonous that it was hard to follow him. At one point he was giving us an oral test, and I realized I was forgetting the questions as he was asking them because I couldn't pay attention to his voice long enough for him to get through a question.

    As to the question, let me just second what passel said. There is NO greater tool that you can learn in this situation than how to set breakpoints, how to step through the code (F5, F10, and F11), and how to view variables (hovering, and also Shift+F9). Learn those few (and very simple) tools and it will make your life a LOT easier. Without those tools, you are left staring at the code wondering what it is doing. With those tools you can watch every step of the way and see what each line does to each variable.
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    New Member Jaycoba's Avatar
    Join Date
    Nov 2017
    Location
    In the sands of time.
    Posts
    13

    Re: 2D Character Array

    Quote Originally Posted by si_the_geek View Post
    Welcome to VBForums
    Thanks! I can stress a little less now that I've found this forum.

    Quote Originally Posted by si_the_geek View Post
    (it is always best to, even if it doesn't work properly)
    I'll remember to do that from now on. Pretty sure I had it right outside the Continue For.

    Quote Originally Posted by si_the_geek View Post
    As such you should be looping rows/rows/columns, check the order (of the entire row), and if swapping loop columns.
    It's funny because the way I have it is exactly how it's laid out on one of my handouts. The code structure is even labeled "Routine to bubble sort a 2D array of character data alphabetically".

    Quote Originally Posted by passel View Post
    And it would be good to learn how to use the debugging capability of the IDE
    Quote Originally Posted by Shaggy Hiker View Post
    ... let me just second what passel said ... Learn those few (and very simple) tools and it will make your life a LOT easier.
    I will definitely read over those links, and multiple times. Much appreciated!

    Quote Originally Posted by projecttoday View Post
    Have you tried Console.Writeline?

    I realize your teacher isn't volunteering much information. But just to clarify, did the teacher specify that you're supposed to use a text box for this?
    I'll give it a try. He did not specify in the lab paper itself, he only suggested that we use a multi-lined textbox during the lecture. I'm pretty sure he wouldn't care what we use, as long as the data prints on multiple lines in alphabetical order.

    Quote Originally Posted by Shaggy Hiker View Post
    He was a nice guy, but his voice was so lulling and monotonous that it was hard to follow him.
    Oh yeah, super nice but just as you said, his voice seriously makes my eyes heavy!



    I'm going to try and see if I can get this thing to work. I'll be sure to post if/when I get it running.
    Thank all of you very much for the quick replies and helpful insight!

  7. #7

    Thread Starter
    New Member Jaycoba's Avatar
    Join Date
    Nov 2017
    Location
    In the sands of time.
    Posts
    13

    Resolved Re: 2D Character Array

    I ended up getting it working. I tried multiple things but ended up with a solution that got me the output I was looking for.

    The code was the same as before, but at the very end, outside of the loops, I entered this code for the output to the multi-lined textbox:

    Code:
            For c1 = 0 To 21
                TextBox1.Text &= Chr(13) + Chr(10)
                For c3 = 0 To 11
                    TextBox1.Text &= seniorityList(c1, c3)
                Next
            Next
    and it worked.

    Again, I thank the 4 of you for your input.
    Until the next post!

  8. #8
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: 2D Character Array

    The &= operation you're using is "Concatenate and Assign".
    If you're going to use an operand to Concatenate strings you should use the "&" operator, which is the concatenate operator, not the "+" operator. The "+" operator is less efficient for concatenating strings because it stands for Addition so the VB has to first check to see if it can convert the strings to a number, and if so, it will add the numbers together and convert the sum back to a string instead of concatenating the strings.
    So your line of code should have been:
    TextBox1.Text &= Chr(13) & Chr(10)

    But rather than use Chr(13) & Chr(10), which also has to go through some conversions before being concatenated to your strings, you should use one of the predefined constants that are already correctly defined so don't have the overhead of converting two characters through the two Chr function calls and one or two hidden function calls into the proper character types.
    si_the_geek already showed you one of the available string constants available from the .Net framework in the first response, i.e. post #2, which is "Environment.Newline".

    So, your line of code preferably would be:
    TextBox1.Text &= Environment.Newline
    Last edited by passel; Nov 6th, 2017 at 02:16 AM.

  9. #9

    Thread Starter
    New Member Jaycoba's Avatar
    Join Date
    Nov 2017
    Location
    In the sands of time.
    Posts
    13

    Re: 2D Character Array

    Textbox1.Text &= Chr(13) & Chr(10)
    I changed it before I turned it in. I don't know if our teacher wants us using things outside of what we've learned in class (I'll ask to make sure this coming lecture), so I left it as quoted above.

    Thank you passel.

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