Author |
Topic: Printng from a array (Read 997 times) |
|
JGHarston
Junior Member
member is offline


Gender: 
Posts: 52
|
 |
Re: Printng from a array
« Reply #9 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: 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: 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: 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))
|
| « Last Edit: Mar 2nd, 2014, 10:07pm by JGHarston » |
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Printng from a array
« Reply #10 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.
|
|
Logged
|
|
|
|
Wendell
New Member
member is offline


Posts: 28
|
 |
Re: Printng from a array
« Reply #11 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 ?
|
|
Logged
|
|
|
|
admin
Administrator
member is offline


Posts: 1145
|
 |
Re: Printng from a array
« Reply #12 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.
|
|
Logged
|
|
|
|
|