Nested For Loop to populate matrix

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Killer42
    Recognized Expert Expert
    • Oct 2006
    • 8429

    #16
    The original question asked for nested loops. Post #4 showed you're getting the idea. One technique which would work is to have the outer loop (let's say X) step by 5 at a time, and have the inner Y loop go from X to X + 4.

    It's important that you learn to understand and be comfortable with loops, including nested loops. They're a very important and heavily-used feature of the language.

    jamesd0142, I'd like to make two points. First, using an array seems somewhat over the top for such a simple task. And second, please remember that we don't hand out answers to homework questions (though helping to understand the problem is fine). Oh, and a third point I just thought of. What purpose does your array serve? How is using hold(i) any different to using i?

    Oh, and werks... I'm watching you too... ;)

    Comment

    • Killer42
      Recognized Expert Expert
      • Oct 2006
      • 8429

      #17
      Originally posted by slvr02scooby
      ... Form and Design are nowhere to be found ... I know this is simple for some but man this program is frustrating lol
      From what I've heard, while there are undoubtedly improvements in the later versions, VB6 is much quicker and simpler to work with. It's a shame M$ had to make such a radical change of direction. :(

      The crazy thing is that schools all over the place are still teaching VB6, which is now well over ten years old and at least 3 or 4 versions behind the times.

      Comment

      • slvr02scooby
        New Member
        • Feb 2008
        • 9

        #18
        Ok this is where i'm at now:
        Code:
        Dim A As Integer
            Dim B As Integer = 0
            Dim g_str As String = "The matrix is:" & vbNewLine
            Private Sub Button1_Click(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles Button1.Click
                Dim B As Integer = 0
                For A = 0 To 49
                    For B = 0 To 4
                        g_str &= " 0 "
                    Next
                    For B = 0 To 49
                        A += 1
                    Next
                    g_str &= vbNewLine
                Next
                MsgBox(g_str)
            End Sub
        End Class
        I know line 9 is wrong, right now I get the output of "0 0 0 0 0" So I have the 5 numbers on one line, but I can't figure out the formula I need to put in what I believe goes on line 9... Do I need to add another farther down?

        Comment

        • Killer42
          Recognized Expert Expert
          • Oct 2006
          • 8429

          #19
          I'd suggest the following...
          • On line 9, simply use B.
          • On line 8, don't count from 0 to 4. Count from A to A + 4.
          • Remove lines 11-13, look up the STEP clause of the FOR statement, and fix line 7.

          By the way, you need to decide on the scope of your variables. You've got some defined at the form level, and B also defined locally in the Sub. The definitions are a bit inconsistent, too. Why are you initialising B to 0, but not A?

          Comment

          • jamesd0142
            Contributor
            • Sep 2007
            • 471

            #20
            vb2005 stores the project files in:

            My Documents > Visual Studio 2005 > Projects

            and to display it in a msg make changes like this:

            [code=vbnet]
            Public Class Form1
            Dim i As Integer
            Dim a As Integer = 0
            Dim hold(49) As Integer

            Dim output As String 'new line

            Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
            Dim a As Integer = 0
            For i = 0 To 49
            hold(i) = i
            Next
            For i = 0 To 49
            a += 1
            output = output & hold(i) & " " 'chenges
            If a = 5 Then
            output = output & vbCrLf ' changes
            a = 0
            End If
            Next
            MsgBox(Mid(outp ut, 1, Len(output) - 2)) 'changes
            End Sub
            End Class

            [/code]

            Comment

            • jamesd0142
              Contributor
              • Sep 2007
              • 471

              #21
              Originally posted by Killer42
              jamesd0142, I'd like to make two points. First, using an array seems somewhat over the top for such a simple task. And second, please remember that we don't hand out answers to homework questions (though helping to understand the problem is fine). Oh, and a third point I just thought of. What purpose does your array serve? How is using hold(i) any different to using i?
              ok so i gave this a go also without the array, which i agree was a waste of time :P

              anyways my question is why do you need to use nested for loops when this simple code below works?
              [code=vbnet]
              Dim i As Integer
              Dim j As Integer = 0
              For i = 0 To 49
              j = j + 1
              If j = 6 Then
              j = 1
              TextBox1.Text = TextBox1.Text & vbCrLf
              TextBox1.Text = TextBox1.Text & i & " "
              Else
              If i = 49 Then
              TextBox1.Text = TextBox1.Text & i
              Else
              TextBox1.Text = TextBox1.Text & i & " "
              End If
              End If
              Next
              [/code]

              Comment

              • Killer42
                Recognized Expert Expert
                • Oct 2006
                • 8429

                #22
                Originally posted by jamesd0142
                ... anyways my question is why do you need to use nested for loops when this simple code below works?
                Though it doesn't actually specify nesting, the original post says...
                Originally posted by OP
                we're required to use two For Statements

                Comment

                • jamesd0142
                  Contributor
                  • Sep 2007
                  • 471

                  #23
                  Originally posted by Killer42
                  Though it doesn't actually specify nesting, the original post says...
                  ah i see. it kind of gave me a head ache yesterday trying for bout an hour to work out how to do it with two for loops.

                  :S

                  Comment

                  • slvr02scooby
                    New Member
                    • Feb 2008
                    • 9

                    #24
                    It still is giving me a headache...

                    Comment

                    • Killer42
                      Recognized Expert Expert
                      • Oct 2006
                      • 8429

                      #25
                      Originally posted by slvr02scooby
                      It still is giving me a headache...
                      Did you try taking the code from post #18 and making the changes I suggested in #19?

                      We're trying to help you understand how to go about it, but we're not allowed to just hand you the answer.

                      Comment

                      • jamesd0142
                        Contributor
                        • Sep 2007
                        • 471

                        #26
                        Do you use the debug tool to step through your program?

                        This is a vital step when learning to use VB as it shows you exactly what your code is doing.
                        Last edited by Killer42; Feb 18 '08, 09:42 PM.

                        Comment

                        • slvr02scooby
                          New Member
                          • Feb 2008
                          • 9

                          #27
                          Yes thank you for the tips killer, i've made the changes, but at this point it just displays "0" and in fact freezes once it's running, have to use task manager to close it.

                          This is what I have now:
                          Code:
                          Dim A As Integer
                              Dim B As Integer
                              Dim g_str As String = ""
                              Private Sub Button1_Click(ByVal sender As System.Object, _
                              ByVal e As System.EventArgs) Handles Button1.Click
                                  For A = 0 To 49
                                      If A < 10 Then
                                          g_str &= A & "   "
                                      Else
                                          g_str &= A & " "
                                      End If
                                      If A Mod 10 = 0 Then
                                          g_str &= vbNewLine
                                      End If
                                      For B = 0 To 4
                                      Next
                                      g_str &= vbNewLine
                                  Next
                                  MsgBox(g_str)
                              End Sub
                          End Class
                          I tried to integrate some of the lessons we learned last class but now it just freezes so i've only gone down hill... What I don't understand is this code that we did in class will display 1 through 100, but how do I put in another For statement and obviously change the numbers to 0 through 49?

                          Code:
                          Dim s As String = ""
                              Dim i As Integer
                              Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
                                  For i = 1 To 100
                                      If i < 10 Then
                                          s &= i & "   "
                                      Else
                                          s &= i & " "
                                      End If
                                      If i Mod 10 = 0 Then
                                          s &= vbNewLine
                                      End If
                                  Next
                                  MsgBox(s)
                                  s = ""
                              End Sub
                          End Class
                          Of course now i'm on time crunch and need to get this in by tomorrow (tuesday) by 6pm (pst)

                          Comment

                          • QVeen72
                            Recognized Expert Top Contributor
                            • Oct 2006
                            • 1445

                            #28
                            Hi,

                            Is this what you are looking for :

                            [code=vb]
                            Private Sub Command1_Click( )
                            Dim i As Integer
                            Dim s As String
                            Dim k As Integer
                            Dim j As Integer
                            s = ""
                            k = 0
                            i = -1
                            j = 0
                            Do
                            i = i + 1
                            s = s & Space(3 - Len(CStr(i))) & CStr(i)
                            k = k + 1
                            If k >= 5 Then
                            j = j + 1
                            s = s & vbCrLf
                            k = 0
                            If j >= 10 Then Exit Do
                            End If
                            Loop
                            MsgBox s
                            End Sub
                            [/code]

                            My code is for VB6 just change vbCrLF to vbNewLine in above code, it should work for VB.net also..


                            Regards
                            Veena

                            Comment

                            • slvr02scooby
                              New Member
                              • Feb 2008
                              • 9

                              #29
                              I appreciate the help but that is far too advanced for what i'm doing lol, I just need to use 2 for statements

                              Comment

                              • QVeen72
                                Recognized Expert Top Contributor
                                • Oct 2006
                                • 1445

                                #30
                                Hi,

                                How about this :

                                [code=vb]
                                Private Sub Command1_Click( )
                                Dim i As Integer
                                Dim s As String
                                Dim k As Integer
                                Dim j As Integer
                                s = ""
                                k = -1
                                For i = 1 To 10
                                For j = 1 To 5
                                k = k + 1
                                s = s & " " & CStr(k)
                                Next
                                s = s & vbCrLf
                                Next
                                MsgBox s
                                End Sub

                                [/code]

                                REgards
                                Veena

                                Comment

                                Working...