Need help with random files and sorting them

Please use this Board for QBasic related requests ( file research, programming, etc.)

Moderators:Administrator, Global Moderator

Post Reply
Guest
Need help with random files and sorting them

Post by Guest » Wed Apr 02, 2003 8:31 am

For a computer science project/test thing I have to make this program creating a database stored in a random access file.  We have to include a bunch of different options that the user can choose and one is to sort the database by atleast 2 methods.  We've learned all about merge sort and bubble sort and all that with arrays, but I'm not sure on how to sort files.  I've been told that I have to put the contents of the file into an array which makes sense to me but I can't think of how that should/would be done.  any help here would be appreciated.  Thanks

Jared

Guest

Re: Need help with random files and sorting them

Post by Guest » Wed Apr 02, 2003 2:05 pm

If you are going to use RANDOM access for a file, you'll need to know "how the records are structured".

'An example of a record's data structure

Type ClientStruct
   Fullname as String * 50
   Age as Integer
   Salary as Single
End Type

Then it's a matter of saving it to the array and sorting it.

'An example (not including the IO syntax nor
'the sorting routines...since you have already
'learned them.)
'Random File Data Structure
TYPE ClientStruct
   FullName AS STRING * 50
   Age AS INTEGER
   Salary AS SINGLE
END TYPE

'Make some room in memory for the "Clients" array
'and have it set up to accept information based
'on the same type(s) of data that is in the
' "ClientStructure" above.
DIM Clients(1 TO 3) AS ClientStruct

CLS
'Gather information to store in the array.
FOR Cycle = 1 TO 3
   INPUT "Enter Client Name:", Clients(Cycle).FullName
   INPUT "Enter Client Age: ", Clients(Cycle).Age
   INPUT "Enter Client Pay: ", Clients(Cycle).Salary
   PRINT
NEXT
PRINT STRING$(60, 220)

'Display the information that is stored in each array.
FOR Cycle = 1 TO 3
   PRINT Clients(Cycle).FullName
   PRINT Clients(Cycle).Salary
   PRINT Clients(Cycle).Age
   PRINT
NEXT
PRINT STRING$(60, 220)

'Shows that you can work with numbers in an array
'that has been TYPE casted...
PRINT "Total Salaries: ";
FOR CycleAgain = 1 TO 3
   TotalSal = TotalSal + Clients(CycleAgain).Salary
NEXT
PRINT TotalSal
END
Note that you have different kinds of information stored into that array.  Don't confuse it with DIM SomeString$(x, x). This kind of dimensionalizing will only allow strings, but TYPE allows other kinds to be mixed with it.

Hopefully, this will help you.
--MiggyD

Zymatic
Newbie
Posts:6
Joined:Wed Apr 02, 2003 8:32 am

Re: Need help with random files and sorting them

Post by Zymatic » Thu Apr 03, 2003 1:44 am

Hey, thanks for your help.  I actually figured it out today in class but your method seemed a lot easier and more efficient than mine so I'll probably use your way of doing it.  Thanks for you help!

Zymatic  a.k.a Jared

Guest

Glad you figuered it out. (nt)

Post by Guest » Sun Apr 06, 2003 2:18 pm


Post Reply