d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Windows Form With Loops > Diso Help
Add Reply New Topic New Poll
Member
Posts: 17,775
Joined: Apr 4 2010
Gold: 35.00
Nov 17 2014 10:43am
So I missed 2 days of programming class and Im pretty much 100% lost and cant seem to get google to help me since all that comes up is pyramids.

Basically when I input any number ex. 5 into Textbox1, I need the output to be

55555
4444
333
22
1
22
333
4444
55555

in Textbox2


Member
Posts: 5,167
Joined: Nov 23 2006
Gold: 11.01
Nov 17 2014 03:40pm
You're going to have to use a nested for loop.

Something like this:
Code
int num = **get textbox values etc etc
int i = num;
//controls what # is printed
for(i; i >= 0; i--)
{
//this for loop controls the # of times the number is printed
for(int j = 0; j < i; j++)
{
//print out the number that you're on (ex 55555)
print(i);
}
//print out a new line to go down to next line
print(new line);
}


This will create the

55555
4444
333
22
1

Now you can figure out how to print it back down from 1-22-333-4444-55555.

There are several ways to count back UP the pyramid.

What I suggest doing is writing it all out on paper. Understand the logic before you even begin coding. Professors used to tell me this and I ignored them. Now 3 years later I do it with EVERYTHING (except this post so if it's wrong I'm blaming it on that, but it should be correct).

This post was edited by HoneyBadger on Nov 17 2014 03:42pm
Member
Posts: 17,775
Joined: Apr 4 2010
Gold: 35.00
Nov 17 2014 04:45pm
Quote (HoneyBadger @ Nov 17 2014 03:40pm)
You're going to have to use a nested for loop.

Something like this:
Code
int num = **get textbox values etc etc
int i = num;
//controls what # is printed
for(i; i >= 0; i--)
{
    //this for loop controls the # of times the number is printed
    for(int j = 0; j < i; j++)
    {
          //print out the number that you're on (ex 55555)
          print(i);
    }
//print out a new line to go down to next line
  print(new line);
}


This will create the

55555
4444
333
22
1

Now you can figure out how to print it back down from 1-22-333-4444-55555.

There are several ways to count back UP the pyramid.

What I suggest doing is writing it all out on paper. Understand the logic before you even begin coding. Professors used to tell me this and I ignored them. Now 3 years later I do it with EVERYTHING (except this post so if it's wrong I'm blaming it on that, but it should be correct).


Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TextBox1.Text = ""
TextBox2.Text = ""
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Dim NUM1 As String = ""
Dim NUM2 = Val(TextBox1.Text)
For row As Integer = NUM2 To 1 Step -1
For column As Integer = 1 To row
NUM1 = NUM1 & row
Next
NUM1 = NUM1 & vbNewLine
Next
TextBox2.Text = NUM1
End Sub

Sub Output(Value As String)
TextBox2.Text += Value + vbCrLf
End Sub

is currently what I have and it creates
55555
4444
333
22
1

but I still dont see the logic in it
Member
Posts: 5,167
Joined: Nov 23 2006
Gold: 11.01
Nov 18 2014 11:10am
The inner most loop is just a loop to print the # of numbers you want printed. You're starting at 0 and telling it to print out j amount of #s until j becomes = to i (which in the first line would be 5). Once it's equal to i it stops that for loop and goes to the outer for. So this will print

55555 the first time through and then it breaks the inner and goes to the outer one.

Just change i/j/num in the for loops with their respective numbers for that iteration.

So the first pass will be:

for(5; 5 >= 0; 5--)
{
for(0; 0 < 5, 0++)
{
print( i );
}
}
So you're telling the outer for that you want the # to 5 be and once it completes the code inside of it (the internal for loop) you want it to decrease 5 by 1 down to 4 and then execute the code inside of it again (the internal for again).
The internal for loop then completes all of the instructions inside of it and increments. So while j is less than the number from the outer for loop it'll print out the # j times.

It's sort of confusing when it's your first time doing this stuff and takes practice. It took me a full semester to fully grasp the concepts as I was just coding it up and not writing out the logic.

Seriously your best bet is to replace the i/j/nums with the actual numbers and write it out to see the output.

Reversing the pyramid to go

1
22
333
4444
5555

can be done in several ways as well. Think about the logic of reversing it once you've already counted it down. You could simply do another nested for loop and go from 1-5 (but you'd have to skip 1 since you already printed it so it would actually be 2-5). There are other ways as well such as a do-while or just a simple while loop but I'm not sure exactly what your class would want.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll