Everything for Electronics

Tech Forum





November 2014

QuickBASIC Program

Since you have various articles dedicated to programming PICs in Basic as well as coding for Arduino, I thought that maybe a proficient N&V reader could help me with a sorting program by correctly coding in Microsoft QuickBASIC.


What I'd like to accomplish is by electing choice (1), sort in ascending order just the six-numbered string, paired with the correct and correspoding "date" and "odd/even" strings. Therefore, the six-numbered string is sorted in ascending order while the corresponding date and odd/even string must stay with that six-numbered string (the dates and odd/even strings must also be swapped).


Also, I'd like to print to the screen — bit by bit/segment by segment — the entire generated output by depressing any keyboard button to advance to the next screen segment until I reach the end of the generated sortition. The way I've coded it, it just goes automatically to the last part of the screen output, without being able to ever visualize either the start nor the middle of the generated screen output.


As for choice (2), I’d like to only sort the string dates — in ascending order — paired with the correct six-numbered strings and corresponding odd/even strings. Lastly, I'd like to print to screen (just as for choice 1 — bit by bit) by depressing some button in order to advance to the next screen segment, until the entire generated sortition would have appeared on the screen.


I'm planning to add more data lines in the future, where I'll have to update lines 200 and 390.


Listing 1 below indicates what I'd like. I learned QuickBASIC in the early ‘80s. My coding partially works, but I can't remember how to correctly do everything at my seasoned age of 56.


1 CLS
192 REM:
*******************************************************************
193 REM: (1) SORT IN ASCENDING ORDER: ALL ACTUAL NUMBERS DRAWN (and the corresponding 'dates drawn')
194 REM: (2) SORT IN ASCENDING ORDER: DATES DRAWN (and the corresponding 'numbers drawn')
195 REM: *******************************************************************
196 REM: C$ = RATIO OF EVEN/ODD NUMBERS CONTAINED
197 REM: B$ = ACTUAL NUMBER DRAWN
198 REM: N$ = DATE DRAWN
199 REM: *******************************************************************
200 DIM N$(200), B$(200), C$(200)
201 REM: *******************************
210 REM - READ number of records
211 REM: *******************************
220 READ K
221 REM: **********************************
230 REM - READ records into ARRAYS
231 REM: **********************************
240 FOR I = 1 TO K
250 READ N$(I), B$(I), C$(I)
255 NEXT I
260 PRINT "****************************************************************"
261 PRINT "SORT based on ALL ACTUAL NUMBERS DRAWN by ENTERing (1) OR "
262 PRINT "SORT based on DATES DRAWN by ENTERing (2) ";
263 INPUT C
264 IF C <> 1 AND C <> 2 THEN PRINT "---- < OOPS!.WRONG CHOICE ENTERED!.TRY AGAIN!. ENTER either (1) or (2)!. >----": GOTO 260
265 IF C = 1 THEN 270
266 IF C = 2 THEN 345
267 REM: *******************************************************************
268 REM : SORT based on ALL ACTUAL NUMBERS DRAWN by ENTERing (1)
269 REM: *******************************************************************
270 FOR I = 1 TO K
280 FOR J = 1 TO K - 1
290 IF B$(J) > B$(J + 1) THEN SWAP B$(J), B$(J + 1)
300 NEXT J
310 NEXT I
311 D$ = N$(I) + " , " +B$(I) + ", " + C$(I)
320 REM - sorted in ascending order: ACTUAL NUMBERS DRAWN (with corresponding 'dates drawn') to be printed
330 FOR I = 1 TO K
335 PRINT I, D$
339 NEXT I
340 GOTO 390
342 REM: *******************************************************************
343 REM: SORT based on DATES DRAWN by entering (2)
344 REM: *******************************************************************
345 FOR I = 1 TO K
346 FOR J = 1 TO K - 1
347 IF N$(J) > N$(J + 1) THEN SWAP N$(J), N$(J + 1)
348 NEXT J
349 NEXT I
350 REM - sorted in ascending order: DATES DRAWN (with corresponding 'numbers drawn') to be printed
355 FOR I = 1 TO K
360 PRINT I, N$(I), B$(I), C$(I)
365 NEXT I
390 DATA 200
10001 DATA "10/22/1994", "10,16,20,28,39,40", "(5Even/1Odd)"
10002 DATA "12/10/1994", "21,23,35,37,39,48", "(1Even/5Odd)"
10003 DATA "01/07/1995", "24,31,33,35,40,44", "(3Even/3Odd)"
10004 DATA "02/04/1995", "11,20,21,24,29,42", "(3Even/3Odd)"
10005 DATA "02/11/1995", "14,17,25,30,31,34", "(3Even/3Odd)"
10006 DATA "05/06/1995", "15,18,23,24,36,48", "(4Even/2Odd)"
10007 DATA "05/20/1995", "11,27,34,35,37,45", "(2Even/4Odd)"
.............................................................
10196 DATA "06/25/2003", "10,16,31,35,38,43", "(3Even/3Odd)"
10197 DATA "06/28/2003", "18,19,26,34,46,47", "(4Even/2Odd)"
10198 DATA "07/02/2003", "20,25,26,36,37,47", "(3Even/3Odd)"
10199 DATA "07/16/2003", "10,16,22,24,26,37", "(5Even/1Odd)"
10200 DATA "08/23/2003", "17,24,26,28,41,44", "(4Even/2Odd)"
40000000 END

#11143
Don Franklin
via email



Answers

Attached is a zip file containing two files in response to Don Franklin’s question regarding a Quick Basic Program he wanted to develop. The code is “Very Well” documented with comments and should not require any additional explanation.

Listing_2.zip

 

'**************************************************************************
'*                     PROGRAM DESCRIPTION                               *
'**************************************************************************
'Program asks for the PATHNAME of the DATAFILE.
'   1.  The PATHNAME can have any name that conforms to the 8+3 character
'       limit of the earlier versions of DOS.
'   2.  The Default PATHNAME is "C:\QB\DATAFILE.TXT"
'   3.  The Datafile can have any number of Records but, they must conform                                                
'       to the following Spec.
'    
'       "mm/dd/yyyy", "nn, nn, nn, nn, nn, nn", "(nEVEN/nOdd)" cr-lf
'       | 12 chars |2 |      19 Chars        | 2|  14 Chars  |  2  |
'
'       for a total of 51 characters per record.
'
'The program opens the DATAFILE, determines how many records it contains
'   and loads them into an array of special TYPE "Records"
'While loading the data, the order of elements in the DATE are re-arranged
'       mm/dd/yyyy to yyyy/mm/dd
'
'The program then asks the user to input a 1, 2 or 3 as defined below.
'
'   (1) SORT IN ASCENDING ORDER: ALL ACTUAL NUMBERS DRAWN
'       (and the corresponding 'dates drawn')
'   (2) SORT IN ASCENDING ORDER: DATES DRAWN
'       (and the corresponding 'numbers drawn')+
'   (3) EXIT PROGRAM
'
'The program then prints 20 Data Records at a time waiting for the user
'   to hit any key to move on the the next screen.
'
' AllData()N = DATE DRAWN
' AllData()B = ACTUAL NUMBERS DRAWN
' AllData()C = RATIO OF EVEN/ODD NUMBERS CONTAINED
'
' Edited by Raymond Perry
' [email protected]
'**************************************************************************
'
'**************************************************************************
'*        DESCRIBE a MULTI DIMENSIONAL ARRAY TO HOLD THE DATA             *
'**************************************************************************
'
TYPE Records
    N AS STRING * 12
    Spcr1 AS STRING * 2
    B AS STRING * 19
    Spcr2 AS STRING * 2
    C AS STRING * 14
    CRLF AS STRING * 2
END TYPE
'
'**************************************************************************
'*        Designate the AllData() array to be re-dimensionable            *
'**************************************************************************
'
'$DYNAMIC AllData() AS Records
'
'**************************************************************************
'*               [DIMENSION other variables]                              *
'**************************************************************************
'
DEFINT A-Z
DIM IK AS STRING, TEMP  AS STRING
DIM Pathname AS STRING
'
'**************************************************************************
'*                     PROGRAM STARTS HERE                                *
'**************************************************************************
'
CLS
'
'**************************************************************************
'*                     PROGRAM STARTS HERE                                *
'**************************************************************************
'
'
'**************************************************************************
'*            Get the FileSpec of the File containing the DATA            *
'**************************************************************************
'
PRINT "Enter the complete PATHNAME of the file containing the data."
PRINT "EXAMPLE: C:\QB\DATAFILE.TXT"
INPUT Pathname
'
'**************************************************************************
'*            Use Default FileSpec if input is null                       *
'**************************************************************************
'
IF Pathname = "" THEN
    Pathname = "C:\qb\datafile.txt"
END IF
'
'**************************************************************************
'*            OPEN the File containing the DATA                           *
'*            BINARY access simplifies GETTING DATA                       *
'**************************************************************************
'
OPEN Pathname FOR BINARY AS #1
'
'**************************************************************************
'*            There are 51 characters in each record                      *
'*            There are K Records in the DATA FILE                        *
'**************************************************************************
'
K = LOF(1) / 51
'
'**************************************************************************
'*         REDIMENSION AllData() Array to make room for K elements.       *
'**************************************************************************
'
REDIM AllData(K) AS Records
'
'**************************************************************************
'*         GET the DATA RECORDS and hold them in the AllData(K) array     *
'*         Re-arrange the .N element to YYYY\MM\DD                        *
'**************************************************************************
'
FOR I = 1 TO K
    GET #1, , AllData(I)
    TEMP = CHR$(34) + MID$(AllData(I).N, 8, 4) + "/"
    AllData(I).N = TEMP + MID$(AllData(I).N, 2, 5) + CHR$(34)
NEXT
'
'**************************************************************************
'*         GET the User input to determine sort method or exit prog       *
'*         This is a "DO Forever" loop until the user enters "3" to exit  *
'**************************************************************************
'
DO
    CLS
    PRINT "Enter 1 to sort by NUMBER."
    PRINT "Enter 2 to sort by DATE."
    PRINT "Enter 3 to EXIT the program"
    IK = ""
    '**** Loop Here until user Enters 1, 2 or 3  **************************
    DO WHILE (IK <> "1") AND (IK <> "2") AND (IK <> "3")
        IK = ""
        IK = INKEY$
    LOOP
'
    IF IK = "3" THEN END
'
'**************************************************************************
'*         Continue if user entered a 1 or a 2                            *
'*         Swapping AllData(J) keeps all elements together                *
'**************************************************************************
'
    IF IK = "1" THEN ' SORT BASED ON NUMBERS DRAWN
        FOR I = 1 TO K - 1
            FOR J = 1 TO K - I
                IF AllData(J).B > AllData(J + 1).B THEN
                    SWAP AllData(J), AllData(J + 1)
                END IF
            NEXT J
        NEXT I
    ELSE        ' SORT BASED ON DATE
        FOR I = 1 TO K - 1
            FOR J = 1 TO K - I
                IF AllData(J).N > AllData(J + 1).N THEN
                    SWAP AllData(J), AllData(J + 1)
                END IF
            NEXT J
        NEXT I
    END IF
'
'**************************************************************************
'*         It is time to Print                                            *
'*         Swapping AllData(J) keeps all elements together                *
'**************************************************************************
'
' *********************[ TIME TO PRINT 20 lines ]**************************
    CLS
    FOR Z = 0 TO INT(K / 20) 'THIS CAUSES MULTIPLE PAGES TO PRINT
        FOR I = 1 TO 20  ' THIS LIMITS TO 20 ITEMS PER SCREEN
            IF (Z * 20 + I) <= K THEN
                Y = I + Z * 20
                PRINT Y, AllData(Y).N; AllData(Y).Spcr1; AllData(Y).B;
                PRINT AllData(Y).Spcr2; AllData(Y).C ' ALLDATA(Y).CRLF;
            ELSE
                EXIT FOR
            END IF
        NEXT I
' ***************[ BREAK AT 20 AND WAIT FOR ANY KEY ]************
        PRINT
        PRINT "PRESS ANY KEY TO PRINT THE NEXT SCREEN."
        IK$ = ""
        DO WHILE IK$ = ""
            IK$ = INKEY$
        LOOP
        CLS
    NEXT
LOOP
'
END

 

DATAFILE.TXT

"10/22/1994", "10,16,20,28,39,40", "(5Even/1Odd)"
"12/10/1994", "21,23,35,37,39,48", "(1Even/5Odd)"
"01/07/1995", "24,31,33,35,40,44", "(3Even/3Odd)"
"02/04/1995", "11,20,21,24,29,42", "(3Even/3Odd)"
"02/11/1995", "14,17,25,30,31,34", "(3Even/3Odd)"
"05/06/1995", "15,18,23,24,36,48", "(4Even/2Odd)"
"05/20/1995", "11,27,34,35,37,45", "(2Even/4Odd)"
"06/25/2003", "10,16,31,35,38,43", "(3Even/3Odd)"
"06/28/2003", "18,19,26,34,46,47", "(4Even/2Odd)"
"07/02/2003", "20,25,26,36,37,47", "(3Even/3Odd)"
"07/16/2003", "10,16,22,24,26,37", "(5Even/1Odd)"
"08/23/2003", "17,24,26,28,41,44", "(4Even/2Odd)"
"06/25/2004", "11,16,31,35,38,43", "(3Even/3Odd)"
"06/28/2004", "12,19,26,34,46,47", "(4Even/2Odd)"
"07/02/2004", "24,25,26,36,37,47", "(3Even/3Odd)"
"07/16/2004", "15,16,22,24,26,37", "(5Even/1Odd)"
"08/23/2004", "17,24,26,28,41,44", "(4Even/2Odd)"
"06/25/2005", "15,16,31,35,38,43", "(3Even/3Odd)"
"06/28/2005", "12,19,26,34,46,47", "(4Even/2Odd)"
"07/02/2005", "24,25,26,36,37,47", "(3Even/3Odd)"
"07/16/2005", "14,16,22,24,26,37", "(5Even/1Odd)"
"08/23/2005", "20,24,26,28,41,44", "(4Even/2Odd)"
"06/25/2006", "15,16,31,35,38,43", "(3Even/3Odd)"
"06/28/2006", "12,19,26,34,46,47", "(4Even/2Odd)"
"07/02/2007", "24,25,26,36,37,47", "(3Even/3Odd)"
"07/16/2007", "14,16,22,24,26,37", "(5Even/1Odd)"
"08/23/2008", "20,24,26,28,41,44", "(4Even/2Odd)"
"10/22/1994", "10,16,20,28,39,40", "(5Even/1Odd)"
"12/10/1994", "21,23,35,37,39,48", "(1Even/5Odd)"
"01/07/1995", "24,31,33,35,40,44", "(3Even/3Odd)"
"02/04/1995", "11,20,21,24,29,42", "(3Even/3Odd)"
"02/11/1995", "14,17,25,30,31,34", "(3Even/3Odd)"
"05/06/1995", "15,18,23,24,36,48", "(4Even/2Odd)"
"05/20/1995", "11,27,34,35,37,45", "(2Even/4Odd)"
"06/25/2003", "10,16,31,35,38,43", "(3Even/3Odd)"
"06/28/2003", "18,19,26,34,46,47", "(4Even/2Odd)"
"07/02/2003", "20,25,26,36,37,47", "(3Even/3Odd)"
"07/16/2003", "10,16,22,24,26,37", "(5Even/1Odd)"
"08/23/2003", "17,24,26,28,41,44", "(4Even/2Odd)"
"06/25/2004", "11,16,31,35,38,43", "(3Even/3Odd)"
"06/28/2004", "12,19,26,34,46,47", "(4Even/2Odd)"
"07/02/2004", "24,25,26,36,37,47", "(3Even/3Odd)"
"07/16/2004", "15,16,22,24,26,37", "(5Even/1Odd)"
"08/23/2004", "17,24,26,28,41,44", "(4Even/2Odd)"
"06/25/2005", "15,16,31,35,38,43", "(3Even/3Odd)"
"06/28/2005", "12,19,26,34,46,47", "(4Even/2Odd)"
"07/02/2005", "24,25,26,36,37,47", "(3Even/3Odd)"
"07/16/2005", "14,16,22,24,26,37", "(5Even/1Odd)"
"08/23/2005", "20,24,26,28,41,44", "(4Even/2Odd)"
"06/25/2006", "15,16,31,35,38,43", "(3Even/3Odd)"
"06/28/2006", "12,19,26,34,46,47", "(4Even/2Odd)"
"07/02/2007", "24,25,26,36,37,47", "(3Even/3Odd)"
"07/16/2007", "14,16,22,24,26,37", "(5Even/1Odd)"
"08/23/2008", "20,24,26,28,41,44", "(4Even/2Odd)"

Raymond Perry
Jacksonville, FL

I don't have a quick answer for you, but the version of QuickBASIC you are using is quite out of date. Of the BASICs running on micros these days, I don't think any still use line numbers, REM statements and the like. The language is more structured with subroutine and function names. I too cut my teeth on HP educational BASIC from that era, but really don't miss RENUM as a edit feature. Now you compose programs in an editor and download them to a target. Our BASIC which targets various ARM processors can also be run on a PC in emulation mode, so you can start out writing programs there. www.coridium.us/files/setupBASIC.exe

Bruce Eisenhard
Tahoe Vista, CA

Don, I'm not a QuickBASIC user, but what I see is you need matching SWAP statements (just like you say) in 290 & 347 when true, SWAP N$, SWAP B$, SWAP C$ then your arrays should stay in sync.


I'm not sure I follow your screen print requirement, however, the INPUT statement should allow you to both print an output and then wait for the Enter key to be pressed before continuing. Place the statement at the point you want to interupt. One more thing, the loop 330-339 will print the same value for D$ over and over, you want to move 311 to 331.

Doug Arndt
Chicago, Il