I believe Veena's code is still needlessly complex. Try this version...
[CODE=vb]Private Sub Command1_Click( )
Dim i As Integer, j As Integer, s As String
For i = 0 To 49 Step 5
For j = i To i + 4
s = s & " " & j
Next
s = s & vbNewLine
Next
MsgBox s
End Sub[/CODE]This is part of the beauty of nested loops, of course - the inner one can make use of the counter value of the outer one. It can also change the value, but that's another story.
Nested For Loop to populate matrix
Collapse
X
-
I'm impressed, I couldnt work this out myself, i ould get the result but couldnt see how to use 2 for loops.Originally posted by QVeen72Hi,
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
VeenaLeave a comment:
-
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
VeenaLeave a comment:
-
I appreciate the help but that is far too advanced for what i'm doing lol, I just need to use 2 for statementsLeave a comment:
-
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
VeenaLeave a comment:
-
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:
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 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
Of course now i'm on time crunch and need to get this in by tomorrow (tuesday) by 6pm (pst)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 ClassLeave a comment:
-
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.Leave a comment:
-
Did you try taking the code from post #18 and making the changes I suggested in #19?Originally posted by slvr02scoobyIt still is giving me a headache...
We're trying to help you understand how to go about it, but we're not allowed to just hand you the answer.Leave a comment:
-
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.Originally posted by Killer42Though it doesn't actually specify nesting, the original post says...
:SLeave a comment:
-
Though it doesn't actually specify nesting, the original post says...Originally posted by jamesd0142... anyways my question is why do you need to use nested for loops when this simple code below works?
Originally posted by OPwe're required to use two For StatementsLeave a comment:
-
ok so i gave this a go also without the array, which i agree was a waste of time :POriginally posted by Killer42jamesd0142, 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?
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]Leave a comment:
-
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]Leave a comment:
-
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?Leave a comment:
-
Ok this is where i'm at now:
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?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 ClassLeave a comment:
Leave a comment: