Qbasicnews.com

QBasic => QB Discussion & Programming Help => Topic started by: jelamb on January 02, 2008, 11:02:13 PM



Title: Sorting Alphabetically
Post by: jelamb on January 02, 2008, 11:02:13 PM
Old timer trying to recreat a sort routine I did back in the 80s to sort a list of items alphabetically according to the variable ID$. See code below that does not seem to work. Success seems to hinge on comparing value of the string variable.
Any Suggestions??
700 REM ALPHABETIZE DATA BY DESCRIPTION = ID$
        FOR N = 1 TO NOR
        FOR M = 1 TO NOR - 1
        IF ID$(M)> ID$(M + 1) THEN GOTO 720 ELSE GOTO 799
720     SCTEMP$ = SC$(M)
        IDTEMP$ = ID$(M)
        UCSTTEMP = UCST(M)
        TQTYTEMP = TQTY(M)
    REM SWAP LINE ITEMS IN LIST
        SC$(M) = SC$(M + 1)
        ID$(M) = ID$(M + 1)
        UCST(M) = UCST(M + 1)
        TQTY(M) = TQTY(M + 1)
        SC$(M + 1) = SCTEMP$
        ID$(M + 1) = IDTEMP$
        UCST(M + 1) = UCSTTEMP
        TQTY(M + 1) = TQTYTEMP
799    NEXT M
       NEXT N
        RETURN 502


Title: Re: Sorting Alphabetically
Post by: Mac on January 03, 2008, 12:02:43 PM
Well, I added some code to make your program function, but I don't have time right now to find the bug. Maybe later unless someone else finds it.

Run this and note that sort does not work.

Code:
CONST NOR = 5
DIM id$(NOR), sc$(NOR), UCST(NOR), TQTY(NOR)
FOR i = 1 TO NOR
  id$(i) = STR$(INT(RND * 1000))
  sc$(i) = "SC" + STR$(i)
  UCST$(i) = "UCST" + STR$(i)
  TQTY$(i) = "TQTY" + STR$(i)
NEXT i

CLS
FOR i = 1 TO NOR
  PRINT id$(i), sc$(i)
NEXT i
PRINT "--------"

REM ALPHABETIZE DATA BY DESCRIPTION = ID$
FOR N = 1 TO NOR
  FOR M = 1 TO NOR - 1
    IF id$(M) > id$(M + 1) THEN
      SCTEMP$ = sc$(M)
      IDTEMP$ = id$(M)
      UCSTTEMP = UCST(M)
      TQTYTEMP = TQTY(M)
      REM SWAP LINE ITEMS IN LIST
      sc$(M + 1) = SCTEMP$
      id$(M + 1) = IDTEMP$
      UCST(M + 1) = UCSTTEMP
      TQTY(M + 1) = TQTYTEMP
    END IF
  NEXT M
NEXT N

FOR i = 1 TO NOR
  PRINT id$(i), sc$(i)
NEXT i


Title: Re: Sorting Alphabetically
Post by: Mac on January 03, 2008, 02:21:24 PM
Somehow you lost these commands:
Code:
      REM Part 1 of the swap
      sc$(M) = sc$(M + 1)
      id$(M) = id$(M + 1)
      UCST(M) = UCST(M + 1)
      TQTY(M) = TQTY(M + 1)

Here they are inserted
Code:
CONST NOR = 5
DIM id$(NOR), sc$(NOR), UCST(NOR), TQTY(NOR)
FOR i = 1 TO NOR
  id$(i) = STR$(INT(RND * 1000))
  sc$(i) = "SC" + STR$(i)
  UCST$(i) = "UCST" + STR$(i)
  TQTY$(i) = "TQTY" + STR$(i)
NEXT i
CLS
FOR i = 1 TO NOR
  PRINT id$(i), sc$(i)
NEXT i
PRINT "--------"
REM ALPHABETIZE DATA BY DESCRIPTION = ID$
FOR N = 1 TO NOR
  FOR M = 1 TO NOR - 1
    IF id$(M) > id$(M + 1) THEN
      SCTEMP$ = sc$(M)
      IDTEMP$ = id$(M)
      UCSTTEMP = UCST(M)
      TQTYTEMP = TQTY(M)
      REM Part 1 of the swap
      sc$(M) = sc$(M + 1)
      id$(M) = id$(M + 1)
      UCST(M) = UCST(M + 1)
      TQTY(M) = TQTY(M + 1)
      REM Part 2 of the swap
      sc$(M + 1) = SCTEMP$
      id$(M + 1) = IDTEMP$
      UCST(M + 1) = UCSTTEMP
      TQTY(M + 1) = TQTYTEMP
    END IF
  NEXT M
NEXT N

FOR i = 1 TO NOR
  PRINT id$(i), sc$(i)
NEXT i


Title: Re: Sorting Alphabetically
Post by: Ralph on January 03, 2008, 06:47:34 PM
Good going, Mac! :) 
Only thing is, first two array variables are strings, but, second two are numerals, not strings :(


Title: Re: Sorting Alphabetically
Post by: Mac on January 03, 2008, 10:43:04 PM
Good going, Mac! :)  Only thing is, first two array variables are strings, but, second two are numerals, not strings :(

Good catch. I didn't bother to display those two, so didn't notice the bug.

I presume his main program has everything initialized properly.

Mac


Title: Re: Sorting Alphabetically
Post by: jelamb on January 12, 2008, 10:03:13 AM
Mac,
Thanks for your efforts. As it turns out, the code I gave works perfectly now, i.e. I did not change anything in that list. I did make some other small changes in my program and, as you suggest, it may have been an initialization error.
Sorry for the confusion and thanks again for your responses.
John