-
Jan 25th, 2014, 11:18 AM
#1
Thread Starter
Lively Member
[Help] Multidimensional array
Q:Write a program to add x and y using multidimensional array and display the result.
what does it actually mean?
i know how to do if it doesnt use multidimensional array.
-
Jan 25th, 2014, 10:17 PM
#2
Re: [Help] Multidimensional array
Ok, how would you do it, not using a multidimesional array(s)? Perhaps once you show that, then how a 2-d array might be used may reveal itself to those of us familiar with multi-dimensional arrays, but not necessarily with vector or matrix math.
-
Jan 26th, 2014, 06:12 AM
#3
Thread Starter
Lively Member
Re: [Help] Multidimensional array
by using the 4 input box for x,7 and output
x=
box1 box2
box3 box4
y=
box1_1 box2_2
box3_3 box4_4
then the output =
box1 + box1_1
.....
but this does not used the array
-
Jan 26th, 2014, 09:45 PM
#4
Re: [Help] Multidimensional array
The numbers are shown in a 2x2 format, so I expect they want you to use a 2x2 2-d array.
Dim X(0 to 1, 0 to 1) as Integer 'choose what ever type you need to use, Integer or Long, or Single, etc.
Dim Y(0 to 1, 0 to 1) as Integer
You need to decide which dimension, the first or second will be your row, and which your column. I guess, I'll choose (Row, Column)
So, now you can do your adds in-line as you would if they were not in an array.
Dim z(0 to 1, 0 to 1) as Integer
z(0,0) = x(0,0) + y(0,0)
z(0,1) = x(0,1) + y(0,1)
z(1,0) = x(1,0) + y(1,0)
z(1,1) = x(1,1) + y(1,1)
But the point of using arrays, is so you can loop through them, which would save some coding, a lot more if the matrix was larger than 2x2
Code:
For row = 0 to 1
For col = 0 to 1
z(row,col) = x(row,col) + y(row,col)
Next
Next
-
Jan 27th, 2014, 11:44 AM
#5
Thread Starter
Lively Member
Re: [Help] Multidimensional array
i did it in the form and it look so messy and if we continue to other number, the result will keep going down the form
can we make the output show on the label and replace each time we calculate another number?
-
Jan 27th, 2014, 02:39 PM
#6
Re: [Help] Multidimensional array
Yes, you can.
I don't know how you're trying to display it now, but this will display the result in two rows in a label.
Code:
Dim z(,) As Integer = {{1, 2}, {3, 4}} 'just initializing some numbers for output, you would calculate them of course
Label1.Text = String.Format("{1} , {2} {0}{3} , {4}", Environment.NewLine, z(0, 0), z(0, 1), z(1, 0), z(1, 1))
The {x} number is a relative index into the parameters following the string and will be replace by the parameter. So the first trailing parameter is "Environment.NewLine" which will put the insert a newline sequence into the string, thus splitting the string onto a new line. We print {1} (the second trailing parameter), {2} then newline then {3} , {4}
So, you could something like:
Label1.Text = String.Format("Line 1: {1}{0}Line 2: {2}{0}Line 3: {3}{0} Last Line: {4}",Environment.NewLine," Five","+ Four"," equals"," Nine")
and get the following in your label (note the {0} used multiple times to insert newlines)
Code:
Line 1: Five
Line 2: + Four
Line 3: -------
Last Line: Nine
Last edited by passel; Jan 27th, 2014 at 02:54 PM.
-
Jan 29th, 2014, 09:37 AM
#7
Thread Starter
Lively Member
Re: [Help] Multidimensional array
Thank, i already passup the assesment
our lecturer didn't teach us about string.format and enviroment.newline yet, so i didnt use it.
Here is my code
Code:
Private Sub cmdAdd_Click()
Dim x(1, 1) As Integer
Dim y(1, 1) As Integer
Dim add(1, 1) As Integer
x(0, 0) = Val(Text1.Text)
x(0, 1) = Val(Text2.Text)
x(1, 0) = Val(Text3.Text)
x(1, 1) = Val(Text4.Text)
y(0, 0) = Val(Text5.Text)
y(0, 1) = Val(Text6.Text)
y(1, 0) = Val(Text7.Text)
y(1, 1) = Val(Text8.Text)
For rol = 0 To 1
For col = 0 To 1
add(rol, col) = x(rol, col) + y(rol, col)
Next col
Next rol
Print " Z = "
For rol = 0 To 1
For col = 0 To 1
Print add(rol, col);
Next col
Print " "
Next rol
Print newline
End Sub
-
Jan 29th, 2014, 11:42 AM
#8
Re: [Help] Multidimensional array
Well, I have to appologize because I messed up and gave you VB.Net code.
VB6 doesn't have String.Format or Environment.Newline (or presetting arrays like I did).
You can print in a specific column by using the Tab() statement.
If the column you specify is less than the CurrentX, print will automatically move to the next line (increment CurrentY) and print in the specified column.
This makes it easy to print columns of data.
An example (VB6 for sure, this time) which clears the form, then prints in column 6 and 10, on two lines.
Code:
Option Explicit
Dim add(0 To 1, 0 To 1)
Private Sub Form_Load()
add(0, 0) = 1: add(0, 1) = 2
add(1, 0) = 3: add(1, 1) = 4
End Sub
Private Sub Command1_Click()
Dim rol As Integer, col As Integer
Cls
Print " Z = ";
For rol = 0 To 1
For col = 0 To 1
Print Tab(6 + col * 4); add(rol, col);
Next
Next
End Sub
Last edited by passel; Jan 29th, 2014 at 11:47 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|