Simple Vs Complicated

Tuesday, December 9, 2008

Logical Looping Programming v2

Today the topic is print the * like the previous vertion one, but this time it's reverse. ow to print it without using text allingn?


here is the code ^ ^ : ( this time i only put the short one only, coz kinda busy lately) 
Dim i As Integer
        Dim j As Integer
        Dim a As Integer
        TextBox2.Text = ""
        For i = 1 To ((TextBox1.Text * 2) - 1)
            a = IIf(i > TextBox1.Text, (2 * TextBox1.Text) - i, i)
            For j = TextBox1.Text To 0 Step -1
                If j = 0 Then
                    TextBox2.Text = TextBox2.Text & "*" & vbCrLf
                Else
                    If j >= a Then
                        TextBox2.Text = TextBox2.Text & " "
                    Else
                        TextBox2.Text = TextBox2.Text & "*"
                    End If
                End If
            Next
        Next

Hope u enjoy the content ^ ^.
someone ask for exercise. how about print the star in a Diamond shape like this? 

Have  a nice Try.

Saturday, November 15, 2008

Timer Logic Programming

This Post is special for my student. This is the answer for the test yesterday. This Time we also make the aplication to print "*" like the one before, but the star appear one by one. to make the star appear one by one, we don't need to use looping. We replace the Looping with Timer. 

Interface of the application : 

To make the Application we only need to put coding in 3 place.
1. after public class form1 to store the current Row and collumn.

Dim i As Integer
   Dim j As Integer

2. in the timer event
'if current row is bigger then the textbox value, then make it repeat
If i = TextBox1.Text + 1 Then
i = 1 
j = 1 
TextBox2.Text = "" 
Exit Sub 
End If 

' if i = j make the next "*" appear in the next row
If j = i Then 
TextBox2.Text = TextBox2.Text & "*" & vbCrLf 
'make the Column start from 1 again
j = 1 
'Add current row by 1
i = i + 1 
Exit Sub 
Else 
TextBox2.Text = TextBox2.Text & "*" 
End If

  j = j + 1

3. in the button
i = 1
j = 1 
TextBox2.Text = "" 
Timer1.Start()

Have a nice try. and hope u like the content 
if  there any code u don't understand just leave the question in comment. ^^

Saturday, November 8, 2008

Simple Logic Programing (Looping) Part 1

This is the first time for me to create a blog. So i really don't know what event or things should be post at here. Finally, i decided to post something that related with my job.

the interface of the application 
 
To make the application above, we only need to write the code in the command button event. The simplest code to have a result like the interface above.

Dim i As Integer
        Dim j As Integer

'Clear The text box
        TextBox2.Text = ""

        For i = 1 To TextBox1.Text
            For j = 1 To i
                If i = j Then
                    TextBox2.Text = TextBox2.Text & "*" & vbCrLf '(enter)
                Else
                    TextBox2.Text = TextBox2.Text & "*"
                End If
            Next
        Next

' step -1 looping from bigger number to smaller number
        For i = TextBox1.Text - 1 To 1 Step -1
            For j = 1 To i
                If i = j Then
                    TextBox2.Text = TextBox2.Text & "*" & vbCrLf
                Else
                    TextBox2.Text = TextBox2.Text & "*"
                End If
            Next
        Next

The Shorter(more Complicated) coding for the application above, we try to change the Looping method. The first one we use 4 looping to make the result, this time With 2 looping can also have the same result.

         Dim i As Integer
         Dim j As Integer
         Dim a As Integer
         TextBox2.Text = ""
         For i = 1 To ((TextBox1.Text * 2) - 1)
           a = IIf(i > TextBox1.Text, (2 * TextBox1.Text) - i, i)
             For j = 1 To a
                 If a = j Then
                     TextBox2.Text = TextBox2.Text & "*" & vbCrLf
                 Else
                    TextBox2.Text = TextBox2.Text & "*"
                 End If
             Next
         Next

Have a nice try :D.
hope you like the content.