Results 1 to 12 of 12

Thread: Extremely simple looping Q

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Posts
    5

    Question Extremely simple looping Q

    I'm JUST starting to learn VB and JUST starting to learn looping. Anyways I have a little program I need to make using simple looping (probably laughable to you) and I'm really confused . I havn't understood the written explanations (codeless) so I'm hoping someone could provide some code for a begginner. Here it is

    Create a program that will print the integers between 1 and 100 according to the descriptions
    a) Print them on the sceen in ten rows of ten digits.
    b)Print only the even numbers between 1 and 100 in five rows of 10 digits
    c)Print only every fifth number between 1 and 100 in four rows of five digits

    Any help would be greatly appreciated, btw it's in VB6

  2. #2
    Addicted Member run_GMoney's Avatar
    Join Date
    May 2002
    Location
    Detroit
    Posts
    186
    This is untested so it may not work properly. First draw a label on your form. Then in the Form_Load event type:
    VB Code:
    1. Dim i, count as Integer
    2.  
    3. For i = 1 to 100
    4.  
    5.     count = count + 1
    6.    
    7.     If count > 10 Then
    8.       count = 0
    9.       Label1.Caption = Label1.Caption + vbCrLf
    10.     End If
    11.    
    12.     Label1.Caption = Label1.Caption + i
    13.  
    14. Next

    That should get you on your way.

  3. #3
    Addicted Member Sheppe's Avatar
    Join Date
    Sep 2002
    Location
    Kelowna, BC
    Posts
    245
    Here's something to get you started:

    VB Code:
    1. Dim X As Long
    2.  
    3. ' Ten rows of ten.
    4. For X = 1 To 100
    5.     Me.Print CStr(X)
    6.  
    7.     If X Mod 10 = 0 Then
    8.         Me.CurrentX = X * 10
    9.         Me.CurrentY = 0
    10.     End If
    11. Next
    12.  
    13. ' Five rows of ten, evens only.
    14. For X = 2 To 100 Step 2
    15.     Me.Print CStr(X)
    16.  
    17.     If X Mod 20 = 0 Then
    18.         Me.CurrentX = X * 10
    19.         Me.CurrentY = 0
    20.     End If
    21. Next

    I'll let you figure out that last condition. Also, this is all off the top of my head, so test it out first.
    [vbcode]
    On Error Goto Hell
    [/vbcode]
    Sheppe Pharis, MCSD
    Check out http://www.vb-faq.com
    Click here for access to the free Code-Express source code and component sharing network for VB6
    Want a better way to skin your .NET applications? Click here!

  4. #4

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Posts
    5

    Smile Thanks

    Thanks guys this should help get me started

  5. #5
    Addicted Member Jakys's Avatar
    Join Date
    Dec 1999
    Location
    Norway
    Posts
    180
    Originally posted by run_GMoney
    This is untested so it may not work properly. First draw a label on your form. Then in the Form_Load event type:
    VB Code:
    1. Dim i, count as Integer

    That should get you on your way.

    Does that make both "i" and "count" an integer?
    MSN: [email protected]

    "Enhetssirkelen løser alle problemer" - ÅJT

  6. #6
    Fanatic Member scr0p's Avatar
    Join Date
    Oct 2002
    Location
    VA
    Posts
    720
    no, only count, you need to do each one, like so:

    VB Code:
    1. dim i as string, k as string, b as integer, LgD as long, oD as byte

    or this is kool, you can do all variables from lets say, A to D, as Integer

    VB Code:
    1. DefInt A-D
    2. DefStr A-Z '(makes all var's starting with A to Z strings, I think)

  7. #7

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Posts
    5
    Once again thanks for the help but I've come across another one I can't seem to figure out. Using looping (I'm pretty sure a for..next loop or a do...loop) I have to ask the user for a number between 1 and 20 then use that number as the value to create the following patterns
    a) make the pattern - e.g the user entered 5
    *
    **
    ***
    ****
    *****
    b) Make this pattern e.g the user entered 4
    * *
    ** **
    *** ***
    ********
    c) Make this pattern e.g the user entered 3
    *
    **
    ***
    **
    *

    uggh! I give up. The last 2 shouldn't look like that . b)should look like a crown and c) should look like 2 triangles put together at the base.

    I've tried all three but with no luck.
    Last edited by Hotstreak; Oct 31st, 2002 at 03:55 PM.

  8. #8
    Addicted Member PhilRob56's Avatar
    Join Date
    Oct 2002
    Location
    New York
    Posts
    249
    Let's keep this simple.

    Here is an example of number 3.
    The code is very basic, to help you understand.
    Put a Label on your form, and set multiline to true.

    Put this code in the Form Load event:

    VB Code:
    1. Dim sNum    As Long
    2. Dim sText   As String
    3. Dim sAsters As String
    4. Dim i       As Long
    5.    
    6.  ' string of 20 asterisks
    7.  sAsters = "********************"
    8.    
    9.  ' ask for a number
    10. sNum = InputBox("Enter a number", "Input")
    11.        
    12. ' loop up to form the first part of the triangle    
    13. For i = 1 To sNum
    14.        sText = sText & Left(sAsters, i) & vbNewLine
    15. Next i
    16.  
    17. ' take 1 off the imput number and step down to create the other triangle
    18.  
    19. sNum = sNum - 1
    20.    
    21. For i = sNum To 1 Step -1
    22.         sText = sText & Left(sAsters, i) & vbNewLine
    23. Next i
    24.    
    25. Label1.Caption = sText

    Now I know that there are better ways to do this but hopefully it demonstrates to you how to get started.
    Obviously Number 1 can be acheived with just the first Loop.
    If you want me to do 2 let me know...

    Some days you're the dog,
    and some days you're the hydrant.


    VB6 Enterprise

  9. #9

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Posts
    5

    Red face Ignore all (((((((

    Thanks! The simpler it is right now, the better but hmm. . . I tried to get number 3
    to () *
    look **
    like ***
    this **
    And *
    2
    look * ) )))))) *
    like) ** )))) **
    this *** ) ***
    but ********
    in my last post but couldn't get the **'s to line up right(I don't know if the code you provided was set up to display it like that)
    Also if you could show me #2 thatwould help me a lot.
    IGNORE all the ))() that's the only way I could get the **'s to set up right

  10. #10
    Addicted Member PhilRob56's Avatar
    Join Date
    Oct 2002
    Location
    New York
    Posts
    249


    Ok, I get the idea. I will do those examples for you.
    Some days you're the dog,
    and some days you're the hydrant.


    VB6 Enterprise

  11. #11
    Addicted Member PhilRob56's Avatar
    Join Date
    Oct 2002
    Location
    New York
    Posts
    249

    Finally...

    This really had me stumped. I could not get the crown to display properly...for some reason I kept getting an extra space in the first string....anyway...here is the code.

    For this to work, make the Label Font Courier New. The default MS Sans Serif font is (I think) variable pitch and won't display properly. Courier New is fixed pitch and does the job.

    This example should be enough for you to figure out the last example on your own.

    VB Code:
    1. Dim sNum As Long
    2.     Dim sText  As String
    3.     Dim sAsters As String
    4.  
    5.     Dim lSpc  As Long
    6.     Dim lTotalSpc As Long
    7.     Dim i   As Long
    8.    
    9.     ' string of 20 asterisks
    10.     sAsters = "********************"
    11.    
    12.     sText = ""
    13.    
    14.     ' ask for a number
    15.     sNum = InputBox("Enter a number", "Input")
    16.    
    17.    
    18.     lTotalSpc = sNum * 2
    19.     Label1.Caption = ""
    20.        
    21.     For i = 1 To sNum
    22.        
    23.         lSpc = lTotalSpc - (i * 2)
    24.            
    25.         sText = Left(sAsters, i) & Space(lSpc) & Left(sAsters, i)
    26.  
    27.          Label1.Caption = Label1.Caption & sText & vbNewLine
    28.        
    29.     Next i

    Good Luck and let me know if you need any more help.

    Some days you're the dog,
    and some days you're the hydrant.


    VB6 Enterprise

  12. #12

    Thread Starter
    New Member
    Join Date
    Oct 2002
    Posts
    5

    Smile Thanks

    It sounds like you went to a bit of trouble just for me!
    Thanks a lot , I probably can't try it out until tommorow but then I'll tell you how it worked out.

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