Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
multiples table
#1
ok we need help we have to create a multiple table using for/next loops how can i do this. also i have to line them all up.
Reply
#2
A multiplication table is a 2D-array whose elements are the products of their cross indexes.

First, you have to determine what your bounds are. How wide is the table? The lowest number we'll call lowerbound, and the highest number we'll call upperbound. These are the limits of the width (columns) of our table. The same can be done for the height (rows), but assuming a square table, this isn't necessary (the height will have the same bounds as the width).

Next, we need figure out the numbers that make up our table. Using our above definition, we can just multiply the column and row indexes together to get our element. Since QB (by default) prints from left to right, top to bottom, that's how we should print our table, ie, from left to right (column 0 and up), top to bottom (row 0 and up).

In constructing your 2D loops, you've got to determine the dimension of most variance, or the dimension that changes the most often. In our case, the columns will be changing most often, since we are going line by line on down the screen. The columns then will be our inner loop, since the inner loop is the loop that changes most often, and the rows will be our outer loop, since it will only change until after our inner loop has completed a full iteration.

Given the above information, implementing this becomes rather trivial:

Code:
'' table metrics
CONST COLUMNOFFSET% = 1
CONST ROWOFFSET% = 1
CONST TABLEWIDTH% = 10
CONST TABLEHEIGHT% = 10

CLS

'' calculate and display table
for i% = ROWOFFSET to TABLEHEIGHT
   for j% = COLUMNOFFSET to TABLEWIDTH

      print i% * j%;

   next j%
   print
next i%
And to line everything up, change the inner print statement to this:
Code:
print tab( ( j% - COLUMNOFFSET ) * 5 ); i% * j%;
stylin:
Reply
#3
i typed that all in and it didnt work it is just printing straight down i will try later at home though just to see but thanx stylin
Reply
#4
Quote:i typed that all in and it didnt work it is just printing straight down i will try later at home though just to see but thanx stylin
Hmm ... I copied it directly from FBIDE. Make sure you inlude both semicolons on the inner print.
stylin:
Reply
#5
Perhaps using the LOCATE statement would work better. I'll think of something in a bit.
quote="Deleter"]judging gameplay, you can adaquately compare quake 4 with pong[/quote]
Reply
#6
TorahTeen: i did one like that, i think Stylin's is better.

Code:
CONST COLUMNOFFSET% = 1
CONST ROWOFFSET% = 1
CONST TABLEWIDTH% = 10
CONST TABLEHEIGHT% = 10

CLS

for dx = rowoffset% to Tablewidth%
    for dy = columnoffset% to Tablewidth%
        locate dx,dy*4
        print dx*dy
    next
next
sleep
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)