Author |
Message |
peterggmss
|
Posted: Thu Oct 26, 2006 12:44 pm Post subject: Each row will contain the row number |
|
|
Hi, sorry to join the site and the first thing I post is a request for help
I need to make a simple program in VB where the user can enter a number of rows, click cmdCalculate and the output will be printed on the form. The output is to be the number of rows which the user wants to see printed on the screen and each row number will contain it's row number. It should be in a pyramid shape. Hard to explain. Example:
code: |
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5
6 6 6 6 6 6
|
I hope that explained it. This is what I have so far:
code: |
Private Sub Form_Load()
Dim Rows As Double
End Sub
Private Sub cmdCalculate_Click()
For Rows = 1 To 100
Print
Next
End Sub
Private Sub cmdDone_Click()
Unload frmAssignment
End Sub
|
I hope you can help me, I read the textbook and tried everything for 2 days now |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
raptors892004

|
Posted: Thu Oct 26, 2006 2:33 pm Post subject: (No subject) |
|
|
Sorry that I cannot help with any code (my VB is getting rusty - I may attempt to code this - shouldn't be too long).. Basically what you can do is get the Integer value of the rows the user input (e.g.: 5) and run two loops.
One loop goes from 1 to row numbers they input. The inner for loop goes from 1 to current row which is an integer in the set of 1 to number of rows they input. Each time you go into the inner loop, you add current row number to the total number. Example:
They want 3 rows:
Loop 1 goes from 1 to 3 (3 times):
Inner loop goes from 1 to currentRow
When outer loop is 1,
Inner loop goes one time (1 to 1)
1 (current row) is added to total number which is to be printed onto the form when the outside loop (1 to max row numbers is complete)
Goes on to row 2:
Inner loop goes two times (1 to 2)
2 is added to total
2 is added to total
Same for row 3 except it adds 3 times the number 3 now..
At the end (outside of the big loop) you print the result onto the form. Hope this helps  |
|
|
|
|
 |
raptors892004

|
Posted: Thu Oct 26, 2006 2:49 pm Post subject: (No subject) |
|
|
code: | Private Sub cmdCalculate_Click()
Dim inputRows As String
Dim total As Integer
inputRows = Text1.Text
For x = 1 To inputRows
For y = 1 To x
total = total + x
Next y
Next x
Form1.Print (total)
End Sub
|
I hope this is self explanatory. It uses the method described in the post above.[/code] |
|
|
|
|
 |
Silent Avenger

|
Posted: Thu Oct 26, 2006 3:26 pm Post subject: (No subject) |
|
|
Umm raptors I think you need to put the Form1.print (total) in the loop you are running so that it will display the way peterggmss wanted. |
|
|
|
|
 |
raptors892004

|
Posted: Thu Oct 26, 2006 5:55 pm Post subject: (No subject) |
|
|
code: | Private Sub cmdStart_Click()
Dim inputRows As String
Dim outputString As String
inputRows = textBox.Text
For x = 1 To inputRows
For y = inputRows To x Step -1
outputString = outputString & " "
Next y
For z = 1 To x
outputString = outputString & x & " "
Next z
Form1.Print (outputString)
outputString = ""
Next x
End Sub |
This basically gets the input from the text field and using iteration prints the triangle. I would've made it print "tab" characters but I don't remember the code that did that. Basically how this works is:
If inputRows=3
P.S.: Step -1 indicates that when program comes back to the beginning of the loop, it will decrease the variable used for the condition (e.g.: y) by 1.
The most outer loop (x) runs 3 times (from 1 to 3)
1st run of big loop
y loop runs 3 times (going from 3 to 1) and adds the spaces 3 times
z loop runs 1 time (1 to 1) and prints the 1 one time
Pyramid line is printed onto the form
2nd run of big loop
y loop runs 2 times (going from 3 to 2) and adds the spaces 2 times to the reset (empty now) outputString variable
z loop runs twice and adds the number 2 and a space and the number 2 and a space again to the current pyramid line
Pyramid line is printed onto the form
3rd run of big loop
y loop runs once (3 to 3) therefore 1 space is printed
z loop runs 3 times therefore 3 integers and their spaces are printed
Pyramid line is printed onto the form
Now the program is done. Hope this is what the question was. |
|
|
|
|
 |
raptors892004

|
Posted: Thu Oct 26, 2006 6:01 pm Post subject: (No subject) |
|
|
An additional problem is the spacing. The fixed code is here. What is different is that when printing the integers, if the integer is less than 10, 3 spaces are printed with it. If it is over 10, only 1 space is printed with it.
code: | Private Sub cmdStart_Click()
Dim inputRows As String
Dim outputString As String
inputRows = textBox.Text
For x = 1 To inputRows
For y = inputRows To x Step -1
outputString = outputString & " "
Next y
For z = 1 To x
If (x < 10) Then
outputString = outputString & x & " "
Else
outputString = outputString & x & " "
End If
Next z
Form1.Print (outputString)
outputString = ""
Next x
End Sub |
|
|
|
|
|
 |
Silent Avenger

|
Posted: Thu Oct 26, 2006 6:12 pm Post subject: (No subject) |
|
|
Now I'm just wondering, did you want the numbers to appear on the form in the pyramid shape that you put up above peterggmss? |
|
|
|
|
 |
peterggmss
|
Posted: Fri Oct 27, 2006 12:40 pm Post subject: (No subject) |
|
|
Silent Avenger wrote: Now I'm just wondering, did you want the numbers to appear on the form in the pyramid shape that you put up above peterggmss? yes.
Thank you. |
|
|
|
|
 |
Sponsor Sponsor

|
|
 |
|