ASCII Value / CHR$ Question

If you have questions about any aspect of QBasic programming, or would like to help fellow programmers solve their problems, check out this board!

Moderators: Pete, Mods

Post Reply
User avatar
Pete
Site Admin
Posts: 887
Joined: Sun Dec 07, 2003 9:10 pm
Location: Candor, NY
Contact:

ASCII Value / CHR$ Question

Post by Pete »

Hey guys, here's a question that I just got. I'm at work and don't have time to look into it. It doesn't sound like too tough of a problem.

You can email Donn at this address: donn_s_miller [AT] hotmail.com

Comments: Following is an anomaly, discovered in the course of debugging, which I cannot fathom. For some reason, a(205).e is changed from its proper value of CHR$(1) to a value of CHR$(255). Wha hoppen? For this test, only the MAIN routine and the SHOW subroutine are used. The program follows.

Best regards,
Donn S. Miller

Code: Select all

'This program is intended to facilitate computations with large polynomials.
'For the purpose of this program, a polynomial is deemed to be made up of a
'group of terms. The group is defined by referring to the index of its first
'and its last term. A term consists of a coefficient -- a long integer -- and
'a series of n exponents of variables -- 1-byte strings grouped together in a
'STRING * n -- taken in a certain order.
'
DECLARE SUB add (af%, al%, bf%, bl%, cf%, cl%)
DECLARE SUB create (first%, last%)
DECLARE SUB edit (first%, oldlast%, newlast%)
DECLARE SUB multiply (af%, al%, bf%, bl%, cf%, cl%)
DECLARE SUB show (first%, last%)
DECLARE SUB subtract (af%, al%, bf%, bl%, cf%, cl%)
  CLS
n% = 1: GOTO main'  TODO:  After debugging, remove this line.
  DO
     INPUT "Enter number of variables.    ", n%
  LOOP WHILE ((n% < 0) OR (n% > 255))
TYPE term
  c AS LONG
  e AS STRING * n
END TYPE
DIM a(1999) AS term
main:
  a(205).c = 1&
  a(205).e = CHR$(1)
  a(206).c = -1&
  a(206).e = CHR$(0)
  show 205, 205
END

SUB add (af%, al%, bf%, bl%, cf%, cl%)
  SHARED a() AS term
  ptr% = cf%
  FOR ia% = af% TO al%
     a(ptr%) = a(ia%)
     ptr% = ptr% + 1
  NEXT
  FOR ib% = bf% TO bl%
     a(ptr%) = a(ib%)
     ptr% = ptr% + 1
  NEXT
  edit cf%, cf% + (al% - af% + 1) + (bl% - bf% + 1) - 1, d%
  cl% = d%
END SUB

SUB create (first%, last%)
  SHARED a() AS term, n%
  pointer% = first%
  INPUT "coefficient=", zc&
  DO UNTIL (zc& = 0&)
     a(pointer%).c = zc&
     ze$ = ""
     FOR i% = 1 TO n%
        DO
           PRINT "exponent"; STR$(i%); "=";
           INPUT u%
        LOOP UNTIL ((u% > -1) AND (u% < 256))
        ze$ = ze$ + CHR$(u%)
     NEXT
     a(pointer%).e = ze$
     pointer% = pointer% + 1
     INPUT "coefficient=", zc&
  LOOP
  last% = pointer% - 1
END SUB

SUB edit (first%, oldlast%, newlast%)
  SHARED a() AS term
  FOR i% = oldlast% - 1 TO first% STEP -1
     sw% = 0
     FOR j% = first% TO i%
        IF (a(j%).e < a(j% + 1).e) THEN
           SWAP a(j%), a(j% + 1)
           sw% = -1
        END IF
     NEXT
     IF (NOT sw%) THEN EXIT FOR
  NEXT
  coefficient& = a(first%).c
  exponent$ = a(first%).e
  pointer% = first%
  FOR i% = first% + 1 TO oldlast%
     IF (a(i%).e = exponent$) THEN
        coefficient& = coefficient& + a(i%).c
     ELSE
        IF (coefficient& <> 0&) THEN
           a(pointer%).c = coefficient&
           a(pointer%).e = exponent$
           pointer% = pointer% + 1
        END IF
        coefficient& = a(i%).c
        exponent$ = a(i%).e
     END IF
  NEXT
  a(pointer%).c = coefficient&
  a(pointer%).e = exponent$
  newlast% = pointer%
END SUB

SUB multiply (af%, al%, bf%, bl%, cf%, cl%)
  DIM wk AS term
  SHARED a() AS term, n%
  ptr% = cf%
  FOR ia% = af% TO al%
     wk = a(ia%)
     FOR ib% = bf% TO bl%
        a(ptr%).c = wk.c * a(ib%).c
        FOR jb% = 1 TO n%
           wke% = ASC(MID$(wk.e, jb%, 1))
           aibe% = ASC(MID$(a(ib%).e, jb%, 1))
           MID$(a(ptr%).e, jb%, 1) = CHR$((wke% + aibe%) MOD 256)
        NEXT
        ptr% = ptr% + 1
     NEXT
  NEXT
  edit cf%, cf% + (al% - af% + 1) * (bl% - bf% + 1) - 1, d%
  cl% = d%
END SUB

SUB show (first%, last%)
'  TODO:  After debugging, remove all COLOR statements.
  SHARED a() AS term, n%
  FOR i% = first% TO last%
     COLOR 12: PRINT a(i%).c; "("; : COLOR 7
     FOR j% = 1 TO n%
        ae$ = a(i%).e
        COLOR 12: PRINT ASC(MID$(ae$, j%, 1)); : COLOR 7
        IF (j% < n%) THEN
        COLOR 12: PRINT ","; : COLOR 7
        END IF
     NEXT
     COLOR 12: PRINT ")   "; : COLOR 7
  NEXT
  PRINT
END SUB

SUB subtract (af%, al%, bf%, bl%, cf%, cl%)
  SHARED a() AS term
  ptr% = cf%
  FOR ia% = af% TO al%
     a(ptr%).c = a(ia%).c
     a(ptr%).e = a(ia%).e
     ptr% = ptr% + 1
  NEXT
  FOR ib% = bf% TO bl%
     a(ptr%).c = -a(ib%).c
     a(ptr%).e = a(ib%).e
     ptr% = ptr% + 1
  NEXT
  edit cf%, cf% + (al% - af% + 1) + (bl% - bf% + 1) - 1, d%
  cl% = d%
END SUB
Bulldog
Newbie
Posts: 7
Joined: Wed Aug 09, 2006 9:30 am

Post by Bulldog »

I don't know why the compiler lets you do this but

TYPE term
c AS LONG
e AS STRING * n ' this should be illegal
END TYPE

n is undefined at this point so n = 0 at compile time, If you change that to
e AS STRING * 1 it works fine

Very odd.
Z!re
Veteran
Posts: 887
Joined: Wed Aug 04, 2004 11:15 am

Post by Z!re »

Bulldog wrote:I don't know why the compiler lets you do this but

TYPE term
c AS LONG
e AS STRING * n ' this should be illegal
END TYPE

n is undefined at this point so n = 0 at compile time, If you change that to
e AS STRING * 1 it works fine

Very odd.

Code: Select all

n% = 1
But it's not constant, so the logic fails.

Logic error, as bulldog pointed out.
I have left this dump.
Post Reply