QB / QB64 Discussion Forum      Other Subforums, Links and Downloads
 Return to Index  

And then there's the modern memory-consuming methods

January 28 2008 at 5:53 PM
MONTREALER  (no login)


Response to Sorting by primary, secondary, etc. fields.

An even more "today" alternative.

Since we know that the user can only request one of 3 data sets - he can only sort the data according to 3 different fields - we can create a program that will create as many data sets as there are fields involved.

From the user's perspective, it will look as though he is in fact sorting the data, when in fact he's only requesting to see pre-sorted data.

The drawbacks:

It uses memory. Fortunately, today's computers can handle it.
Note that this version is bigger than the previous version and also larger than SOLITAIRE's original version.

It takes up execution time. Fortunately, today's computers can handle that too.

The advantages:
Speed for the user. When the users make a selection, no actual sorting occurs, they will only be shown the data as pre-sorted by the program. The time it takes to pre-sort all the data is negligible to the user who probably won't notice.
When they ask to see the lists, they appear quickly without delay.

For the programmer, it reduces the "USER-Area" to the last 16 lines of the program (1/3). Everything above that (2/3) is self-executing by the program. In terms of maintanability, the programmer who refers to this later on has two distinct sections to work on. Let's see some disagreements ...


=============================================================================

CLS

CONST totalrecords% = 6
CONST totalfields% = 3
DIM record$(totalfields%, totalrecords%)

PRINT TAB(6); "Unsorted Lists:": PRINT

FOR l% = 1 TO totalrecords%
READ record$(0, l%)
PRINT TAB(6); RIGHT$(record$(0, l%), LEN(record$(0, l%)) - 10);
PRINT TAB(14); LEFT$(record$(0, l%), 3);
PRINT TAB(20); MID$(record$(0, l%), 5, 5)
NEXT l%
PRINT : PRINT

FOR j% = 1 TO totalfields%
FOR l% = 1 TO totalrecords%

record$(j%, l%) = record$(0, l%)' copy the original version into each set

NEXT l%

IF j% = 1 THEN first% = 10: longg% = 3
IF j% = 2 THEN first% = 1: longg% = 3
IF j% = 3 THEN first% = 4: longg% = 5

FOR i% = 1 TO totalrecords% - 1
FOR l% = 2 TO totalrecords%
IF MID$(record$(j%, l%), first%, longg%) < MID$(record$(j%, l% - 1), first%, longg%) THEN SWAP record$(j%, l%), record$(j%, l% - 1)
NEXT l%
NEXT i%
NEXT j%

'================ The User begins using the program here ==================

k$ = "2" ' Ensures the WHILE/WEND loop will run at least once
WHILE k$ >= "1" AND k$ <= "3"

PRINT TAB(6); "Which field do you wish to use as primary field for sorting:"
INPUT "1- name, 2- ID, or 3- cost? Enter 1, 2, 3, or anything else to QUIT: ", k$
PRINT : PRINT

IF k$ >= "1" AND k$ <= "3" THEN

j% = VAL(k$)
PRINT TAB(6); "Sorted lists:": PRINT

FOR l% = 1 TO totalrecords%

PRINT TAB(6); RIGHT$(record$(j%, l%), LEN(record$(j%, l%)) - 10);
PRINT TAB(14); LEFT$(record$(j%, l%), 3);
PRINT TAB(20); MID$(record$(j%, l%), 5, 5)

NEXT l%

PRINT : PRINT

END IF

WEND

DATA "789/34.56/Mary"
DATA "123/99.31/Joe"
DATA "567/67.99/Alice"
DATA "385/23.45/Tom"
DATA "987/12.98/Helen"
DATA "471/75.23/Barry"

END

 
 Respond to this message   

Newbies usually go to www.qbasic.com and click on The QBasic Forum
Forum regulars have their own ways, which include The QBasic Community Forums