locked
Help with making a dimond pattern using a nested loop

    Question

  • Hello

    I am having some difficulties in making a diamond pattern using the for....next loop.  I have created a diamond pattern so far, however, I need to eliminate one of the rows that displays the most highest amount of asterisks.  For example, if I enter 7 for the user input, the console will display two rows that has 7 asterisks.  I only want to display one of the rows of the seven aterisks.  The set up of the other astersiks are fine. And also note that these are made of odd numbers. Here are my full codes: the first .vb contains the main module and the second.vb contains the class:

     

    Code Snippet

    'This is a Console Application that uses nested for loops

    'to draw a diamond pattern of asterisks.

    'The program prompts the user for any odd number

    'and then displays the corresponding diamond shape.

     

    Module DiamondModule

     

    ' Main begins program execution

    Sub Main()

     

    Dim diamondClass As New DiamondClass()

     

    ' Obtain and Request an odd number

    Console.Write("Enter an odd number: ")

     

    diamondClass.SideOddInput = Console.ReadLine()

     

    diamondClass.Display() ' display box

     

    End Sub ' Main

    End Module

     

     

     

     

    Code Snippet

    'This is a Console Application that uses nested for loops

    'to draw a diamond pattern of asterisks.

    'The program prompts the user for any odd number

    'and then displays the corresponding diamond shape.

     

    Public Class DiamondClass

    Private sideOddInputValue As Integer ' length of side of asterisks

    Private fillCharacterValue As String = "*" ' character used to draw asterisks

     

    ' property provides access to side length of diamond

    ' From the Request of an odd number from input

     

    Public Property SideOddInput() As Integer

    Get

    Return sideOddInputValue ' return side OddInput

    End Get

    Set(ByVal value As Integer)

    'Test for odd values

    If (value And 1) = 1 Then

    sideOddInputValue = value

    End If

    'if even values then diplay message

    If (value And 1) <> 1 Then

    Console.WriteLine("Your input is a Even number, " & value)

    Console.WriteLine("Please Enter an Odd number only. " & vbCrLf)

    End If

    End Set

    End Property ' End SideOddInput

     

     

    ' property provides access to fill character for drawing box

     

    Public Property FillCharacter() As String

    Get

    Return fillCharacterValue ' return fill character

    End Get

    Set(ByVal value As String)

    fillCharacterValue = value ' modify fill character

    End Set

    End Property ' FillCharacter

     

     

    ' display diamond

    Public Sub Display()

    Dim row As Integer ' variable holding the number of rows current row

    Dim column As Integer ' current column loop

     

    For row = 1 To SideOddInput

    For column = 1 To row

    ' prints out all odd

    ' numbers from 0 to input

     

    If row Mod 2 = 1 Then

    ' fill character

    Console.Write(FillCharacter)

    Else ' skip or do not write evens

    Continue For

    End If

     

    Next column

    Console.WriteLine() ' position cursor to next line

     

    Next row

     

    For row = SideOddInput To 1 Step -1

    For column = row To 1 Step -1

    ' prints out all odd

    ' numbers from 0 to input

    If row Mod 2 = 1 Then

    ' fill character

    Console.Write(FillCharacter)

    Else ' skip or do not write evens

    Continue For

    End If

     

    Next column

    Console.WriteLine() ' position cursor to next line

     

    Next row

     

    End Sub ' Display

    End Class

     

     

    Wednesday, November 26, 2008 12:01 AM

Answers

  • Replace the for loop

    For row = SideOddInput - 1 To 1 Step -1

     

    To

     

    For row = (SideOddInput - 1) To 1 Step -1

    Wednesday, November 26, 2008 4:34 AM
  • Thank You.

    Can hardly believe that all it needed was three common symbols.

    Again Thank You and much appreciated.

    Recency

    Wednesday, November 26, 2008 10:43 AM

All replies

  • Replace the for loop

    For row = SideOddInput - 1 To 1 Step -1

     

    To

     

    For row = (SideOddInput - 1) To 1 Step -1

    Wednesday, November 26, 2008 4:34 AM
  • Thank You.

    Can hardly believe that all it needed was three common symbols.

    Again Thank You and much appreciated.

    Recency

    Wednesday, November 26, 2008 10:43 AM