Results 1 to 3 of 3

Thread: Extremely Easy Program That I Am Making Hard

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Oct 2010
    Posts
    16

    Extremely Easy Program That I Am Making Hard

    Hello all!

    I am working on a program which should be easy, but I am making it harder than it should be on myself.



    I need to make a program that if I press the button below each ListBox that it displays the corresponding output.

    How my teacher would like us to do this is to actually make each one a rectangle and half of the characters be spaces and the other half be A, B, C or D. Does that make sense?

    So for the first output is should be like this...

    lets say space = #

    A#########
    AA########
    AAA#######
    AAAA######
    AAAAA#####
    AAAAAA####
    AAAAAAA###
    AAAAAAAA##
    AAAAAAAAA#
    AAAAAAAAAA

    Here is a flowchart we made in class in Visual Logic, I am not sure if any of who have heard of this, but either way here is the flowchart.



    Here is the corresponding code implemented into Visual Basic.

    vb.net Code:
    1. Dim number As Integer
    2.         Dim FirstCharacter As String
    3.         Dim SecondCharacter As String
    4.         Dim row As String
    5.  
    6.         number = 5
    7.         FirstCharacter = "A"
    8.         SecondCharacter = "*"
    9.         row = ""
    10.  
    11.         For count = 1 To number
    12.  
    13.             For firstCount = 1 To count
    14.                 row = row & FirstCharacter
    15.  
    16.                 For secondCount = 1 To ((number - firstCount) + 1)
    17.                     row = row & SecondCharacter
    18.  
    19.  
    20.                 Next
    21.                
    22.             Next
    23.             lstOutputA.Items.Add(row)
    24.             row = ""
    25.         Nextt

    Now I am not sure what I did wrong, but this code doesn't work in Visual Basic. I The output is...

    A*****
    A*****A****
    A*****A****A***
    A*****A****A***A**
    A*****A****A***A**A*

    NOTE: I am using the "*" to represent spaces so I can see where they are.

    I am pretty sure it is because these...
    vb.net Code:
    1. For firstCount = 1 To count
    2.                 row = row & FirstCharacter
    3.  
    4.                 For secondCount = 1 To ((number - firstCount) + 1)
    5.                     row = row & SecondCharacter
    6.  
    7.  
    8.                 Next
    9.                
    10.             Next

    Need to be separated loops instead of nested loops. So they should be like this...
    vb.net Code:
    1. For firstCount = 1 To count
    2.                 row = row & FirstCharacter
    3.             Next
    4.  
    5.             For secondCount = 1 To ((number - firstCount) + 1)
    6.                 row = row & SecondCharacter
    7.  
    8.  
    9.             Next
    10.             lstOuput.Items.Add(row)
    11.             row = ""

    The only problem is firstCount in the secondCount loop becomes invalid because it is not yet declared.

    So how would I get around that?

    Any help would be greatly appreciated!

    Thanks!
    Last edited by Adampff; Dec 13th, 2010 at 05:50 PM.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Extremely Easy Program That I Am Making Hard

    The first two loops are pretty good, except that both should go to N where N is the number of lines you want (you use 5, but I see no part of your example that has only 5 lines). The loops would look like:
    Code:
    For x = 1 to N
     For Y = 1 to N
      If Y>X Then
       'Add the space
      Else
       'Add the character
      End If
     Next
    Next
    My usual boring signature: Nothing

  3. #3
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Extremely Easy Program That I Am Making Hard

    If you're looking to do a literal translation of that flowchart into Visual Basic, then you've made a mistake. The flowchart shows a loop from 1 to number (looping the rows). Inside that loop are two loops, one to make the "first char"s and one to make the "second char"s. It is these two loops that you've made a mistake in translating.

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