Qbasicnews.com

Full Version: Spaced number
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
This is probably a simple problem, but here:
Code:
SCREEN 13
DIM map(1 TO 10, 1 TO 10)
DATA 1,1,1,1,1,1,1,1,1,1
DATA 1,0,0,0,0,0,0,0,0,1
DATA 1,0,0,0,0,0,0,0,0,1
DATA 1,0,0,0,0,0,0,0,0,1
DATA 1,0,0,0,0,0,0,0,0,1
DATA 1,0,0,0,0,0,0,0,0,1
DATA 1,0,0,0,0,0,0,0,0,1
DATA 1,0,0,0,0,0,0,0,0,1
DATA 1,0,0,0,0,0,0,0,0,1
DATA 1,1,1,1,1,1,1,1,1,1
FOR y = 1 TO 10
FOR x = 1 TO 10
  READ z
  map(x, y) = z
NEXT
NEXT
FOR y = 1 TO 10
FOR x = 1 TO 10
  IF x = 10 THEN
   PRINT map(x, y)
   ELSE
    PRINT map(x, y);
  END IF
NEXT
NEXT
How do I get it to stop printint spaces in between each number?
Code:
PRINT LTRIM$(STR$(variable%))
Wink
LTRIM$(STR$(map(x, y)))

or rtrim$......................
Nope, LTRIM$. A space is reserved at the front of the string equiv of a number for a -ve sign.
Ah, now I remember reading that! I never figured out why it added a space.
By putting a PRINT after the FOR x loop, you can save yourself the IF ... THEN ... ELSE.
Code:
FOR y = 1 TO 10
FOR x = 1 TO 10
   PRINT map(x, y);
NEXT
PRINT
NEXT
Quote:By putting a PRINT after the FOR x loop, you can save yourself the IF ... THEN ... ELSE.
Code:
FOR y = 1 TO 10
FOR x = 1 TO 10
   PRINT map(x, y);
NEXT
PRINT
NEXT
I just needed to test whether a newline was needed, i.e. end of a row of the map. Notice that I put ";" on one of the PRINTs?
they both do the same thing, its just that SCM does it a cleaner way.

yours goes through the x loop, and if x = 10, then it prints the number and then goes to the next line.

SCM's goes through the x loop, and then after it has gone through, it goes to the next line.

now if the x loop went to a number bigger than 10, but you still wanted it to print a new line at the 10th loop, then your way would be the only way.
I thought mine would be more efficient, because it avoids the IF statements being repeated. After I thought about it, I realized I had traded 10 IF's for 1 PRINT, and Print's are slow. I tested it both ways and found the performance to be the same. I agree that mine looks cleaner, but it doesn't work any better.
I'm a geek Big Grin

Code:
FOR y = 1 TO 10
FOR x = 1 TO 10
   LOCATE y,x
   PRINT map(x, y)
NEXT
NEXT
Pages: 1 2