2d arrays

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
Anonymous

2d arrays

Post by Anonymous » Wed Oct 11, 2006 9:44 pm

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

User avatar
Quibbler
Coder
Posts: 16
Joined: Tue Jan 24, 2006 7:29 am
Location: Trinidad and Tobago

Post by Quibbler » Thu Oct 12, 2006 7:50 am

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

Anonymous

Post by Anonymous » Thu Oct 12, 2006 8:21 pm

so if i have 10 arrays, each 40x40, i have to write to each file every time? thats 1600 numbers!!!

Anonymous

Post by Anonymous » Thu Oct 12, 2006 8:25 pm

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

Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re » Fri Oct 13, 2006 3:01 am

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
I have left this dump.

User avatar
Quibbler
Coder
Posts: 16
Joined: Tue Jan 24, 2006 7:29 am
Location: Trinidad and Tobago

Post by Quibbler » Fri Oct 13, 2006 6:37 am

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.

Anonymous

Post by Anonymous » Fri Oct 13, 2006 9:57 pm

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)

Post Reply