The dimensions of my matrix are 4, 3. I want to input a text file into a listbox. Contents of the text file are:
4
3
1
2
3
4
5
6
7
8
9
10
11
12
I need the 1 thru 12 to display in the list box like this:
1 2 3
4 5 6
7 8 9
10 11 12
The 4 & 3 simply represent my rows and columns.
The code has to be so I can easily change the array size.
Here is my code so far:
dim a(4,3) as integer
dim row as integer
dim col as integer
open "a:\matrix.txt" for input as #10
do until eof (10)
can somebody please show me how to do this? My instructor is being a dick. Won't show us how to do this and no one can figure it out.
PLEASE HELP???????????????
We generally don't help with homework here unless someone tries. You posted some code to show you were trying so that should protect you against a pelting from the old-timers. (We get a lot of people here who pretty much ask us to do thier homework)
You are on the right track! You are going to use a nested loops to load your array
VB Code:
For intRow = 1 To 4
For intCol = 1 To 3
'get values and stick them in the array here
Next intCol
Next intRow
I made a working version, but I didn't use
VB Code:
do until eof (10)
You already know you are reading in a set number of items.
I've got a working sample, but you will learn nothing if we just throw code at you. See if you can get the nested loops to work and post back if you need more help.
"Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!
Resistance is futile, you will be compiled . . . Please!
I used the nested For-next loop and I think I put the values in right, but it is giving me a "Input past end of file" error.
Private Sub Form_Load()
Dim intMatrix(4, 3) As Integer
Dim introw As Integer, intcol As Integer
Open "a:\matrix.txt" For Input As #10
Do Until EOF(10)
For introw = 1 To 4
For intcol = 1 To 3
Input #10, intMatrix(introw, intcol)
Next intcol
Next introw
Loop
Close #10
End Sub
You already know you are reading in a set number of items.
"Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!
Resistance is futile, you will be compiled . . . Please!
"Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!
Resistance is futile, you will be compiled . . . Please!
I see what it is the code is doing, but how did you make your text file? Mine is in the exact format below:\
4
3
1
2
3
4
5
6
7
8
9
10
11
12
all numbers in a single column.
when I change the path you have from c: to a: which is where my txt file is stored, it is still giving the error "input past end of file" and that is using the exact code you have written.
"Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!
Resistance is futile, you will be compiled . . . Please!
First of all, as I understand the format of the text file, you shouldn't assume the dimensions of your array is (4, 3) since the first to numbers in the file give the dimension. So you should probably write your code so that if the file would look something like this:
2
3
1
2
3
4
5
6
The result in the list box would be:
1 2 3
4 5 6
and if the file would look like this (ALMOST the same)
BTW Armbruster, your code doesn't work since you can only redim one of the dimensions and you can't change the number of dimensions. Conclusion: You'll need to use a one-dimensional array (if you really think you need an array to start with)
Well thank you both, but the same error still comes up using either code. "Input past end of file."
on Armbrewsters code the error is hitting on the line in red
Open "c:\matrix.txt" For Input As #10
'get # or rows
Input #10, strTemp
intRow = Val(strTemp)
'get # of columns
Input #10, strTemp
intCol = Val(strTemp)
On Joacims code the same error is hitting on the line in red:
'Never use a constant file handle number, always use FreeFile
'to get an available file number instead
hFile = FreeFile
Open "c:\theFile.txt" For Input As #hFile
'first line is the number of rows
iRows = CInt(sText)
Line Input #hFile, sText
Originally posted by Joacim Andersson OK, so I posted a little late
BTW Armbruster, your code doesn't work since you can only redim one of the dimensions and you can't change the number of dimensions. Conclusion: You'll need to use a one-dimensional array (if you really think you need an array to start with)
I redimed both dimensions
'redimension the array
ReDim intMatrix(intRow, intCol)
thats a valid redimension after the initial dimension of a one-dimensional array
from MSDN . . .
You can use the ReDim statement repeatedly to change the number of elements and dimensions in an array
and I ran the code and it worked for me . . .
"Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!
Resistance is futile, you will be compiled . . . Please!
Originally posted by Joacim Andersson My bad, sorry!
It's ok, you're still a guru in my book and I haven't removed you from the christmas card list
"Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!
Resistance is futile, you will be compiled . . . Please!
Originally posted by garyb Could you do me a favor? Would you mind explaining to me what the following line of code is doing? I understand all the code except that.
First let me explain what the space does. The space(x) function adds x number of spaces. So:
“Carl” & space(12) & “Armbruster” would look like
Code:
“Carl Armbruster”
Next I concatenated the string. strTemp = strTemp & intMatrix(intRow, intCol) built the line of text to add to the list box.
On the first pass, intMatrix(intRow, intCol) = 1, so strTemp = “1”
On the next pass, intMatrix(intRow, intCol) = 2, so strTemp = “1 2”
On the next pass, intMatrix(intRow, intCol) = 3, so strTemp = “1 2 3”
I wanted the columns to line up and the columns to be right-justified. It is fine to left-justify text. But our “text” is numbers and numbers are usually right-justified.
for example you wouldn’t list numbers like this:
123
1
456789
12548
12
5
You would list them like this
Code:
123
1
456789
12548
12
5
I used and old-school method of right-justifying columns. I just padded the columns with spaces. I call this an old school method, because when you did this with old dos applications, the fonts were fixed-length (every letter takes up the same amount of space - like courier-new) so when you padded with spaces, they would all line up perfectly. So to make the columns line up I added 8 spaces – then length (that is where the len() function comes in) of the text that I was lining up. Len is a function that measure the length of a string so I made sure that it was a string by using the str() function to convert the integer to a string.
Hope this helps and good luck with the projects . . .
Last edited by Armbruster; May 14th, 2003 at 08:26 PM.
"Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!
Resistance is futile, you will be compiled . . . Please!