Page 1 of 1

2d arrays

Posted: Wed Oct 11, 2006 9:44 pm
by Anonymous
if i have an array like this

2,2,2
3,5,3
4,4,4

inside a file, how would i write to the file and replace the 5 with a 6?

ive tried everything with the open for output as #1bla bla bla
write #1, yada yada

but i just rewrites the file and i cant replace anything

Posted: Thu Oct 12, 2006 7:50 am
by Quibbler
1. Read the numbers into a matrix "2D array".
2. Change the number that is wrong
3. Write the numbers back into the same file.

open.....for input
for i=1 to 3
input#1, M(i,1),M(i,2),M(i,3)
next i
close #1
M(2,2)=3
open...for output
for i=...
write ....
etc...etc

Posted: Thu Oct 12, 2006 8:21 pm
by Anonymous
so if i have 10 arrays, each 40x40, i have to write to each file every time? thats 1600 numbers!!!

Posted: Thu Oct 12, 2006 8:25 pm
by Anonymous
what i want it to do is edit a 40x40 array so i can just change the number in the array, not have to rewritw the entire thing every time

Posted: Fri Oct 13, 2006 3:01 am
by Z!re
mar_angry wrote:what i want it to do is edit a 40x40 array so i can just change the number in the array, not have to rewritw the entire thing every time
You can use binary and write data to only the position you want.

Code: Select all

open xx for binary as #f
put #f, offset, data

Posted: Fri Oct 13, 2006 6:37 am
by Quibbler
Yes you could use a binary/random access file, but this is much more tricky, and you seem to be having trouble with the sequential files.

1600 numbers are nothing to a computer and your numbers are short integers as well.

Short answer is don't be afraid of over-taxing your computer. You are, I presume, going to have to read in the array anyway to do what ever it is you want to do with a 40x40 matrix.

Maybe you would be better using a text editor rather than qbasic for this problem.

Posted: Fri Oct 13, 2006 9:57 pm
by Anonymous
yeah with the 1600 numbers, is that i dont want to re write them personally. And if i write and algorithm that does it for me, the map will be too symmetrical or too random (ie the map wont be the design i want it to be)