The Student Room Group

FOR loop exercise - Help appreciated.

Hi i am having trouble with a FOR loop exercise. My task was to first display the square of each integer from 1 to 10, and then to amend the program to display the square AND the cube of each integer. I managed to do the first task pretty easily, but am having trouble displaying the cube number to the side of the square number. Could somebody help me to see what is wrong with my code below? Thanks, much appreciated.


Private Sub Form_Load()
Dim number As Integer
Dim sqnumber As Integer
Dim cubenumber As Integer
Dim colnumber As Integer
Dim gap As Integer

frmsqnumber.Show

For colnum = 1 To 2

For number = 1 To 10
sqnumber = number * number
Print sqnumber

Next number

Next colnum
For number = 1 To 10
cubenumber = number * number * number
gap = 8 - Len(Str(sqnumber))
Print gap & cubenumber

Next number


End Sub
I think you may be overcomplicating matters slighty. Anyway, here is my solution;

Dim Number As Integer
Dim SquareNumber As Integer
Dim CubeNumber As Integer
Dim CombinedNumber As Integer

frmsqnumber.Show

For Number = 1 To 10

SquareNumber = Number * Number
CubeNumber = Number * Number * Number

Print "Square of"; Number; Tab; "Cube of"; Number
Print Trim(SquareNumber); Tab; Trim(CubeNumber)
Print

Next Number
Reply 2
Hi thanks for the help. I managed to finally get it by myself doing this:

Dim number As Integer
Dim sqnumber As Integer
Dim cubenumber As Integer
Dim gap As Integer

frmcubenumber.Show

For number = 1 To 10
sqnumber = number * number
cubenumber = number * number * number
gap = 8 - Len(Str(sqnumber))

frmcubenumber.Print sqnumber & Space(gap) & cubenumber


Next number
Reply 3
By the way i might need help doing another exercise which is on nested loops. I need to display the sum of row and column numbers for 4 columns and 5 rows, and there has to be "+" sign at the top left hand corner.

I will most likely need help, but i will have a bash at it first.

Thanks. :smile:
Jaswarbrick
By the way i might need help doing another exercise which is on nested loops. I need to display the sum of row and column numbers for 4 columns and 5 rows, and there has to be "+" sign at the top left hand corner.

I will most likely need help, but i will have a bash at it first.

Thanks. :smile:

Good luck, however there are numerous reference and revision books for Computing which you can use, especially ones relating to Computing Projects (Which, i found to be extremely useful). Which can help ease the process into learning a new language.
Oh, and by the way you really should be identing your code. This is especially useful for when you are completing your larger and more indepth Computing Projects. If you are taking the AQA syllabus, then you will be expected to produce a fully working system, and by using indentation it makes it much easier and efficient to tackle down bugs and problems within the system.
Reply 6
Thanks for the advice michael, really appreciated.

By the way i just cannot get my head round this nested sum. Here is my attempt:

Dim rownumber As Integer
Dim colnumber As Integer


frmnestedsum.Show

For rownumber = 1 To 5
For colnumber = 1 To 4
result = rownumber + colnumber
frmnestedsum.Print rownumber & result
Next colnumber
Next rownumber


By the way, do you recommened that book? I have read the reviews on Amazon and they all look really good. Did you use that book for learning Visual Basic. I think i might order it then if you recommend it, as sometimes i have trouble on understanding some concepts.

Thanks alot.
I used the aforementioned book throughout my AS AQA Computing course, and found it an extremely useful introduction into learning and applying Visual Basic. However, now that i am in my second year the book sort of becomes a little too, but for AS-Level especially the book was like a Bible when getting to grips with basic Visual Basic conventions and programming objects.
I am not quite sure what your quoted above is actually trying to achieve. Are you looking to produce a single row and column of numbers?
Reply 9
It is meant to have the column numbers along the top and the row numbers going down the side. And then the sum of the row and column numbers underneath like this:

+ 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
I'd have thought that a Control Array would be most appropriate for this sort of problem, however that might be a little advanced as an introduction to Visual Basic programming.
Jaswarbrick
Thanks for the advice michael, really appreciated.

By the way i just cannot get my head round this nested sum. Here is my attempt:

Dim rownumber As Integer
Dim colnumber As Integer


frmnestedsum.Show

For rownumber = 1 To 5
For colnumber = 1 To 4
result = rownumber + colnumber
frmnestedsum.Print rownumber & result
Next colnumber
Next rownumber


By the way, do you recommened that book? I have read the reviews on Amazon and they all look really good. Did you use that book for learning Visual Basic. I think i might order it then if you recommend it, as sometimes i have trouble on understanding some concepts.

Thanks alot.

Try this:


Dim rowNum As Integer
Dim colNum As Integer
Dim result As String

'print top line
result = "+ 1 2 3 4" & vbCrLf

For rowNum = 0 To 3

'add row header
result = result & (rowNum + 1).ToString & " "

For colNum = 0 To 3

'add results for each addition
result = result & ((colNum + 1) + (rowNum + 1)).ToString & " "


Next

'add row footer
result = result & vbCrLf


Next

TextBox1.Text = result


It might need changing if you want it to work in VB 6.

HTH :smile:
Reply 12
theArchitect
Try this:


Dim rowNum As Integer
Dim colNum As Integer
Dim result As String

'print top line
result = "+ 1 2 3 4" & vbCrLf

For rowNum = 0 To 3

'add row header
result = result & (rowNum + 1).ToString & " "

For colNum = 0 To 3

'add results for each addition
result = result & ((colNum + 1) + (rowNum + 1)).ToString & " "


Next

'add row footer
result = result & vbCrLf


Next

TextBox1.Text = result


It might need changing if you want it to work in VB 6.

HTH :smile:

**** me, even I'm slightly confused by that :s:
It's efficient, but no VB newbie is going to understand all of it...imagine if his teacher wants him to explain how a particular line of the program works (I certainly had this problem with my computing teacher) - he'd be pretty screwed, eh?

Anyway, a newbie-friendly version:
Dim colNum, rowNum, result as Integer

For rowNum = 0 to 4
For colNum = 0 to 4
result = rowNum + colNum
frmNestedSum.Print result;
Next
frmNestedSum.Print
Next
This code will give a printout that looks like this in VB6
0 1 2 3 4
1 2 3 4 5
2 3 4 5 6
3 4 5 6 7
4 5 6 7 8
No need to use things like the ToString command that you won't have come across yet, and it should be easy enough for you to follow and explain if you have the need to :smile:
The only things I will point out, just so you know:
Where it's printing result, the semicolon at the end of that line means that any input after it is added on the same line (I was expecting that I'd need to add a space, but this appears to happen automatically).
That's why there is another print command outside the for loop, which does absolutely nothing but move you onto the next line. If that print command was missing, the output would look like this:
0 1 2 3 4 1 2 3 4 5 2 3 4 5 6 3 4 5 6 7 4 5 6 7 8
Dalimyr
**** me, even I'm slightly confused by that :s:
It's efficient, but no VB newbie is going to understand all of it...imagine if his teacher wants him to explain how a particular line of the program works (I certainly had this problem with my computing teacher) - he'd be pretty screwed, eh?

Anyway, a newbie-friendly version:
Dim colNum, rowNum, result as Integer

For rowNum = 0 to 4
For colNum = 0 to 4
result = rowNum + colNum
frmNestedSum.Print result;
Next
frmNestedSum.Print
Next
This code will give a printout that looks like this in VB6
No need to use things like the ToString command that you won't have come across yet, and it should be easy enough for you to follow and explain if you have the need to :smile:
The only things I will point out, just so you know:
Where it's printing result, the semicolon at the end of that line means that any input after it is added on the same line (I was expecting that I'd need to add a space, but this appears to happen automatically).
That's why there is another print command outside the for loop, which does absolutely nothing but move you onto the next line. If that print command was missing, the output would look like this:

Didn't think it was that complicated actually.

I should have mentioned though that it assumes that there is a multi-line textbox called textbox1 on the form that this code is in. Also, vbCrLf is a new line.

Latest

Trending

Trending