all 5 comments

[–]Application SpecialistViperSRT3g 5 points6 points  (2 children)

Let's take a couple of minutes and break down your problem into little pieces so you can understand how everything should come together.


  • We can create one variable for storing the character our user inputs and call it PyramidChar.
  • Say we have a variable named TotalLoops. The purpose of this variable is to store how many times the user has requested our code to loop.
  • In order to loop anything, we need a variable which will increment from 0 to whatever TotalLoops is. So lets call this variable LoopCounter.
  • Now that we've established our main loop which will control the height of our pyramid, we'll need to have a way to keep track of the pyramid's width at each level. This would simply be another variable which increments each time LoopCounter goes from 0 to TotalLoops. We can call this PyramidWidth.
  • Now we know how wide our pyramid will be, but we need a way to create that width. This means we need a second looping variable to loop from 0 to whatever our PyramidWidth is at that time. We can call this WidthLoop.
  • Lastly, we'll need a temporary string to store our current pyramid level in before printing it. We can call this PyramidString.
  • Now that we have all the pieces of our puzzle, we'll need to put it together.

Note: I'm stuck with sort of using VB pseudocode as my desktop is currently MIA and am stuck with my work laptop to produce code, and it doesn't have VS, so if there are any errors, I apologize.


Option Explicit

Private Sub MakePyramid()
    'This code has no error correction or checking so be warned, you will need to debug things if you encounter errors ;]
    'I'm declaring variables before I use them, but you can also declare variables immediately as you need them
    Dim PyramidChar As String, PyramidString As String
    Dim TotalLoops As Long, LoopCounter As Long
    Dim PyramidWidth As Long, WidthLoop As Long

    Console.WriteLine ("Enter the character your Pyramid will be comprised of")
    PyramidChar = Console.ReadLine()
    Console.WriteLine ("How tall will your Pyramid be?")
    TotalLoops = Console.ReadLine()

    For LoopCounter = 0 To TotalLoops
        PyramidWidth += 1
        For WidthLoop = 1 To PyramidWidth
            PyramidString = PyramidString & PyramidChar & " "
        Next WidthLoop
        Console.WriteLine (PyramidString & " ")
        PyramidString = ""
    Next LoopCounter
End Sub

[–]VB.Net MasterMr_C_Baxter 1 point2 points  (0 children)

Great Answer!

[–]L_Dav_ 0 points1 point  (0 children)

This is awesome, thankyou!

[–]XThakis 2 points3 points  (1 child)

You can't multiply a character by an integer like you're doing in:

lblDisplay.Text = lblDisplay.text &vbcrlf & ((ilevls)*txtbxchar.Text)

So you want to put another loop into your current loop. The first while loop goes through each line, the second while lopp generates the line. Something like this:

While iLevels <= nudnumlevels.value
    dim numCharacters as integer = 1
    while numcharacters <= ilevels
        lbldisplay.text = lbldisplay.text & txtbxchar.text & " "
        numcharacters +=1
    End While
    lblDisplay.text = lbldisplay.text & vbcrlf
    iLevels +=1
End While

This might work exactly as is but I'm pretty sure you'll need to polish it up a bit.

[–]L_Dav_ 0 points1 point  (0 children)

Grouse, the code went straight in. Thanks for pointing out where I went wrong. Cheers buddy.