AllExperts > Experts 
Search      

Qbasic, Quickbasic

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More Qbasic, Quickbasic Answers
Question Library

Ask a question about Qbasic, Quickbasic
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Alex Barry
Expertise
I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, c/c++, python, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.

Experience
Hobby programming since 2000

I no longer belong to any community programming groups, but do have knowledge of *basic dialect and C/C++
 
   

You are here:  Experts > Computing/Technology > Basic > Qbasic, Quickbasic > saving array to file

Topic: Qbasic, Quickbasic



Expert: Alex Barry
Date: 5/26/2008
Subject: saving array to file

Question
QUESTION: Hi Alex: I want to create a new array (C) whose elements are the result of subtracting elements of another array (A) from corresponding elements of another yet another array (B). All three have two dimensions. How can one write the the new array as a two dimensional array to a file? Hope this is clear.  Thanks so much for your help.

ANSWER: Hi, Larry,

This sounds suspiciously like a homework question, but I think I can help you out a little bit.

To create an array, it looks something like this:
Dim somearray(1 to number_of_elements) as some_type

With two dimenstions:
Dim somearray(1 to number_of_elements, 1 to more_elements) as some_type

With three, you add another comment, and the number of elements, etc, etc, etc.

I'm sure you can do the math of subtracting, but you will need a few FOR/NEXT loops to cycle through the arrays.

To write to a file, you'll need to look into OPEN/PRINT #/CLOSE commands.

I won't come up with more answers for you, as this sounds quite a bit like homework, but this should start you off quite well, I think.

Alex

---------- FOLLOW-UP ----------

QUESTION: Thanks for quick reponse. I'm not quite sure about the structure of the code for the writing part. When I do it, I get 1 column with 96 rows where I want 12 column with 8 rows. If you could expand that part of your answer I'd appreciate the help. BTW, I laughed about the allusion to a homework assignment. I am an agricultural scientist with the USDA. I have 30 years of professional experience. If you that confirmed, google my name (Larry Zibilske). I'm still chuckling about that, but I really do appreciate your help. Thanks, Larry Zibilske

Answer
Hi, Larry,

Sorry for mistaking this as homework.  This is the time of year when many students have projects, and have no clue what they are doing, so my humble apologies.

Well, if this is for business, I may suggest that another programming language may be able to get the job done quite a bit easier, but I'll help you out in QBasic.

If you let me know what sorts of data you are putting in these rows and columns, I can help you quite a bit more.

I will say that if you want to put columns and rows, and you are using the various arrays, I may suggest a 3d array, which gets a little more confusing, but it will make printing to a file quite a bit easier, so it would look something like this:

Dim arrays(1 to 3, 1 to [num of 1st dimension], 1 to [num of 2nd dimension] ) as single ' or whatever number type you are using.

Then, to do the math, it even makes things easier:

Const First% = 1
Const Second% = 2
Const Result% = 3

arrays( Result%, 1, 1 ) = arrays( First%, 1, 1 ) - arrays( Second%, 1, 1 )
Then, all you have to do is put them in a file, which would look something like this:

Const NumInFirstD% = 12
Const NumInSecondD% = 8

Dim i as integer
Dim ii as integer
Dim x as integer

x = FreeFile
Open "file" for Output as #x

For i = 1 to NumInFirstD%
 For ii = 1 to NumInSecondD%
   arrays( Result%, i, ii ) = arrays( First%, i, ii ) - arrays( Second%, i, ii )
   ' Using "," at the end of the print statement forces a TAB or a column to be formed without a linebreak
   print #x, arrays( Result%, i, ii ),

 Next ii
 ' Printing this blank will ensure that we move to the next row
 Print #x, ""
Next i

close #x

If you will keep it with the 3 arrays, you'll have to do a few loops instead of one, but the same principle still applies.

I think that should put you on the right track.  Just make sure you read over my code, so you know about the columns and rows things.

Hope that helps infinitly more than my first.  Sorry, you just have to realize that I weed out quite a bit of homework stuff.

Thanks,
Alex

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.