Results 1 to 3 of 3
  1. #1
    2 Star Lounger
    Join Date
    Oct 2005
    Location
    Calgary, Alberta
    Posts
    205
    Thanks
    0
    Thanked 1 Time in 1 Post

    Making a diamond in a console app (vb 2005 express)

    Hi there,

    I just bought a book on VB.net and currently learning about control structures.

    While going through an assignment which is
    Create a program which prints a diamond shape in *'s. allow the user to input an odd number from 1 to 19

    I have got the top part of the diamond but stuck making the bottom part of the diamond

    my code
    Module Module1

    Sub Main()

    Dim i As Integer
    Dim rows As Integer
    Dim midpoint As Integer
    Dim starNum As Integer

    Console.WriteLine("Welcome! Please enter an odd number from 1 to 19")
    rows = Console.ReadLine()

    midpoint = (rows + 1) / 2
    i = 1
    For line As Integer = 1 To rows Step 2

    Do Until i = midpoint
    Console.Write(" ")
    i = (i + 1)
    Loop
    'write stars
    Do Until starNum = line
    Console.Write("*")
    starNum = starNum + 1
    Loop
    Console.WriteLine()
    i = 1
    midpoint = midpoint - 1
    starNum = 0
    Next


    Console.ReadLine()
    End Sub

    End Module

  2. The Following User Says Thank You to Matix For This Useful Post:

    geralee (2013-07-11)

  3. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Making a diamond in a console app (vb 2005 express)

    I'm only going to give you some hints, not the complete code:
    - You have to write another loop like the For ... Next loop you already have, only this time, you must loop backwards.
    - You must increase Midpoint within the new loop instead of decreasing it.
    - And you'll have to initialize Midpoint before starting the second loop, otherwise you'll get an overflow error.

  4. #3
    2 Star Lounger
    Join Date
    Oct 2005
    Location
    Calgary, Alberta
    Posts
    205
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: Making a diamond in a console app (vb 2005 express)

    Module Module1

    Sub Main()

    Dim i As Integer
    Dim rowNum As Integer
    Dim midpoint As Integer
    Dim starNum As Integer

    Console.WriteLine("Welcome! Please enter an odd number from 1 to 19")
    rowNum = Console.ReadLine()

    If (rowNum < 1) Or (rowNum > 19) Or (rowNum Mod 2 = 0) Then
    Do While (rowNum < 1) Or (rowNum > 19) Or (rowNum Mod 2 = 0)
    Console.WriteLine("Welcome! Please enter an odd number from 1 to 19")
    rowNum = Console.ReadLine()
    Loop

    End If

    midpoint = (rowNum + 1) / 2
    i = 1

    For line As Integer = 1 To rowNum Step 2

    Do Until i = midpoint
    Console.Write(" ")
    i = (i + 1)
    Loop
    'write stars
    Do Until starNum = line
    Console.Write("*")
    starNum += 1
    Loop
    Console.WriteLine()

    i = 1
    midpoint -= 1
    starNum = 0
    Next

    midpoint += 1
    i = 0


    For line2 As Integer = (rowNum - 2) To 1 Step -2

    Do Until i = midpoint
    Console.Write(" ")
    i += 1
    Loop
    'write stars
    Do Until starNum = line2
    Console.Write("*")
    starNum += 1
    Loop
    Console.WriteLine()

    i = 0
    midpoint += 1
    starNum = 0
    Next
    Console.ReadLine()
    End Sub

    End Module

    Thank you

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •