BBC BASIC for Windows
Programming >> BBC BASIC language >> Printng from a array
http://bb4w.conforums.com/index.cgi?board=language&action=display&num=1393734143

Printng from a array
Post by Wendell on Mar 2nd, 2014, 03:22am

I am trying to print from a Array to the screen. everthing good until i try printing to next line. Wrong variable printed.IS there a special procedure for printing tab from a array?
Re: Printng from a array
Post by Edja on Mar 2nd, 2014, 08:47am

Can you post the code so we can have a look at it ?

Eddy
Re: Printng from a array
Post by Wendell on Mar 2nd, 2014, 11:26am

INPUT NAME$
INPUT MATH$
right=0
(mul)
CLS
DIM num(10)
FOR x=1 TO 10
num(x)=RND(10)
DIM den(10)
FOR z=1 TO 10
den(z)=RND(10)
CLS
WAIT 100
*FONT TERMINAL,12
PRINT TAB(20,6);NAME$;"'s Math Quiz"
*FONT TERMINAL,10
PRINT""
IF MATH$="m" THEN PRINT TAB(39,11) "MULTIPLICATION"
IF MATH$="a" THEN PRINT TAB(41,11) "Addition"
IF MATH$="A" THEN PRINT TAB(41,11) "Addition"
IF MATH$="d" THEN PRINT TAB(41,11) "Divison"
IF MATH$="s" THEN PRINT TAB(41,11) "Subtraction"
*FONT TERMINAL,12
INSTALL @lib$+"FNUSING"
PRINT TAB(28,10);FNusing("##",num(x))
PRINT TAB(26,11);"x ";FNusing("##",den(z))
PRINT TAB(27,12);"____"
*FONT TERMINAL,10
pr$="PROBLEM"
count=count+1
PRINT TAB(39,25)laughr$;" ";"(";count;")"
*FONT TERMINAL,12
INPUT TAB(28,14)ans
IF count = 10 THEN GOTO (score)
NEXT x
NEXT z
GOTO (mul)
(score)
CLS
*FONT TERMINAL,10
PRINT TAB(20,17)"QUIZ OVER PLEASE WAIT WHILE TENMADS CALCULATE YOUR SCORE"
WAIT 200
CLS
PRINT ""
PRINT ""
PRINT" ";NAME$;"'s MATH QUIZ"
PRINT""
PRINT" ";"MULTIPLICATION"
PRINT""
PRINT" ________________________________________________________"
PRINT" (1) (2) (3) (4) (5)"
PRINT ""
INSTALL @lib$+"FNUSING"
PRINT TAB(18,9)FNusing("###",num(1))
PRINT TAB(18,10)FNusing("###",den(1))




den(1) not correct !!!!!





Re: Printng from a array
Post by admin on Mar 2nd, 2014, 12:03pm

on Mar 2nd, 2014, 11:26am, Wendell wrote:
den(1) not correct !!!!!

In what sense "not correct"? When I run your code den(1) is a random integer in the range 1 to 10, which is what it is evidently supposed to be, so I can't see anything wrong.

Incidentally, do not jump out of a loop using GOTO; in BBC BASIC this is dangerous (just as it is in Liberty BASIC too): it will leave the stack in a mess and may eventually cause you to run out of memory - in the jargon it is a 'memory leak'. Make the following changes to your program:

Code:
(mul)         to  REPEAT
GOTO (score)  to  EXIT REPEAT
GOTO (mul)    to  UNTIL FALSE
 

You are best to avoid GOTOs altogether, of course.

Richard.

Re: Printng from a array
Post by Wendell on Mar 2nd, 2014, 12:12pm

printing it,,den(1) with a tab . you come up with wrong answer. for that interger den(1)
Re: Printng from a array
Post by Edja on Mar 2nd, 2014, 12:24pm




But more fundamentally :
if you want to come to grips with BB4W you should eliminate all GOTO's and labels and replace these with PROC's and FN's.

Try to restructure the program this way. Then we can revisit the issues you seem to have with DIM and TAB

Eddy



Re: Printng from a array
Post by Edja on Mar 2nd, 2014, 12:40pm

on Mar 2nd, 2014, 12:24pm, Edja wrote:
This code can't work. You received a "No such variable" error message on the line
PRINT TAB(39,25)laughr$;" ";"(";count;")"
because laughr$ has never been assigned a value.

For some reason when I copy/paste Wendell's code into BB4W the "p" form variable pr$ is replaced by "laugh".
This already put me on the wrong foot twice.

I suppose this has something to do with the "smiley" that appears in the code in the post.

Is there a setting that I can change to avoid this ?

Eddy
Re: Printng from a array
Post by admin on Mar 2nd, 2014, 12:51pm

on Mar 2nd, 2014, 12:40pm, Edja wrote:
Is there a setting that I can change to avoid this ?

Yes, you (or in this case the OP) can click Disable smilies ('Check this if you'll be adding code (or don't like smilies)').

But I don't think it's necessary if the program is listed in [code] tags, which is what the OP should have done ideally.

Richard.
Re: Printng from a array
Post by admin on Mar 2nd, 2014, 1:02pm

on Mar 2nd, 2014, 12:12pm, Wendell wrote:
printing it,,den(1) with a tab . you come up with wrong answer. for that interger den(1)

Highly implausible! There's no way that changing the tabulation of the printout can alter a value printed.

Incidentally in BBC BASIC using two or more consecutive commas in a PRINT statement won't make any difference, because if it's already aligned on a 'column' a comma will have no effect. If you want to simulate the effect of tabulation in some other BASIC dialects add a space before each comma:

Code:
      DIM den(10)
      den(1) = 7
      PRINT " ", " ", den(1) 

Richard.
Re: Printng from a array
Post by JGHarston on Mar 2nd, 2014, 10:04pm

Stripping out some code:
Code:
      DIM num(10)
      FOR x=1 TO 10
        num(x)=RND(10)
        DIM den(10) 

You're dimensioning den() ten time. While in BB4W you can redimension an array if it is the same size, most Basics won't allow that, or will "leak memory" as each DIM abandons the old array and grabs more memory to start again. Even though BB4W does allow you to do that without leaking memmory, you're needlessly throwing away den() and redimensioning it nine times. You should DIM arrays before you use them, and other than some specialist circumstances that will always be outside any loops.

Code:
        FOR z=1 TO 10
          den(z)=RND(10) 

You're setting all ten items of den() ten times each as the loop setting den() is inside another loop. You don't do anything with den() until you print it outside the loop later on.

Code:
          INSTALL @lib$+"FNUSING" 

and you're loading the FNUSING library a hundred times. You should INSTALL your libraries at the start of the program, and certainly not inside any loops or subroutines.
Code:
        NEXT x
      NEXT z 

Actually, loading FNUSING ten times, and then breaking the FOR/NEXT loop. As pointed out by others, you've nested your NEXTs the wrong way around. It's only because you've jumped out of the loop stepping past the NEXTs that you happen to not get an error.

The BB4W editor indents loop so that it is simple to follow the layout and see where looping structures match up.

Code:
      INSTALL @lib$+"FNUSING" 

And now you've loaded FNUSING a 101th time.

The overall structure of your program should be something along these lines, but I don't know what den() is being used for.
Code:
      INSTALL @lib$+"FNUSING"
      :
      DIM num(10)
      DIM den(10)
      FOR x=1 TO 10
        num(x)=RND(10)
        FOR z=1 TO 10
          den(z)=RND(10)
          REM etc...
        NEXT z
      NEXT x
      REM etc.
      PRINT TAB(18,9); FNusing("###",num(1))
      PRINT TAB(18,10);FNusing("###",den(1)) 


Re: Printng from a array
Post by admin on Mar 2nd, 2014, 10:33pm

on Mar 2nd, 2014, 10:04pm, JGHarston wrote:
you're needlessly throwing away den() and redimensioning it nine times.

No you're not - it's perfectly OK to do that in BB4W: the second and subsequent DIMs are a no-op (that's a requirement for PRIVATE arrays to work, where necessarily the DIM statement is executed each time the PROC/FN containing the PRIVATE array is called).

Quote:
You should INSTALL your libraries at the start of the program, and certainly not inside any loops or subroutines.

Preferably, but again it's OK to do that in recent versions of BB4W where the second and subsequent INSTALLs don't actually do anything except waste a little time.

Richard.
Re: Printng from a array
Post by Wendell on Mar 3rd, 2014, 03:16am

Thanks for all the help almost got arrays and DIM down pretty good. one more question is the a way to Right Justify a number in bbc basic ?
Re: Printng from a array
Post by admin on Mar 3rd, 2014, 08:22am

on Mar 3rd, 2014, 03:16am, Wendell wrote:
one more question is the a way to Right Justify a number in bbc basic ?

So long as you are using a monospaced (fixed pitch) font, such as Courier New or Lucida Console, numbers are right justified by default. Try running this code:

Code:
      PRINT 1
      PRINT 23
      PRINT 456
      PRINT 7890 

The column width is set by the least-significant byte of @% (initially 10).

In a PRINT statement right-justification is switched off by a semicolon (;) and switched on by a comma (,).

You can, of course, also easily achieve right-justification using the FNusing function.

Richard.