To get an understanding, let's take these basic rules of addition and multiplication:
a_{0} a_{0} + b_{0} x b_{0}   c_{1}c_{0} c_{1}c_{0}Where a_{0} and b_{0} are single digit numbers, and c_{1}c_{0} can be a twodigit number.
In variable a_{0} is a onedigit number.
In variable b_{0} is a onedigit number.
In variable c_{0} is the ones portion of a twodigit number.
In variable c_{1} is the tens portion of a twodigit number.
Let a_{0} = 3 and b_{0} = 4
Add a_{0} and b_{0} and store the result in c_{0}, which is 7 in this case
Since c_{0} is less than 10, then moving the tens digit to the c_{1} variable is not necessary.
Multiply a_{0} and b_{0} and store the result in c_{0}, which is 12 in this case
Since c_{0} is greater than 9, you'll need to move the tens digit to the c_{1} variable.
To do that, assuming that c_{1} is already preinitialized to zero,
Then, you take the number in c_{0}, and divide it by 10, and leave off the decimal part, then
add the integer portion to c_{1} like this:
c_{1} = c_{1} + INT(c_{0} / 10) (if the "c" array are decimal numbers)
c_{1} = c_{1} + c_{0} \ 10 (if the "c" array are integer numbers)
To remove the tens portion of the number in c_{0},
you can use either method:
c_{0} = c_{0} MOD 10 (which gives the remainder of 2 in this case)
c_{0} = c_{0}  10 * INT(c_{0} / 10)
c_{0} = c_{0}  10 * (c_{0} \ 10)
So c_{1} = 1 and c_{0} = 2 for the multiplication problem.
a_{1}a_{0} a_{1}a_{0} + b_{1}b_{0} x b_{1}b_{0}   c_{3}c_{2}c_{1}c_{0} c_{3}c_{2}c_{1}c_{0}We're putting the "8" in a_{1}, the "5" in a_{0}, the "4" in b_{1}, and the "7" in b_{0}.
The rules for adding two numbers go like this:
a_{1}a_{0} 8 5 + b_{1}b_{0} 4 7  c_{1}c_{0} 1 2 (c_{0} = a_{0} + b_{0}, overflow digit from c_{0} goes to c_{1}) c_{2}c_{1} 1 3 (c_{1} = a_{1} + b_{1} + c_{1}, overflow digit from c_{1} goes to c_{2}) ======== c_{2}c_{1}c_{0} 1 3 2 (bring down all the lowermost variables)Doing the computerizd math in these steps: (note that the c array is preinitialized to zero before you add)
c_{0} = c_{0} + a_{0} + b_{0} 12 = 5 + 7 c_{1} = c_{1} + INT(c_{0} / 10) 1 = INT(12/10) c_{0} = c_{0}  10 * INT(c_{0} / 10) 2 = 12  10*INT(12/10) c_{1} = c_{1} + a_{1} + b_{1} 13 = 1 + 8 + 4 c_{2} = c_{2} + INT(c_{1} / 10) 1 = INT(11/10) c_{1} = c_{1}  10 * INT(c_{1} / 10) 3 = 13  10*INT(13/10)Now lets change gears to Multiplication!
We're putting
the "8" in a_{1},
the "5" in a_{0},
the "4" in b_{1}, and
the "7" in b_{0}.
The example is for multiplication.
We're going to store the answer 3,995 in the array "c" in positions.
c_{3}, c_{2}, c_{1}, and c_{0}
The rules for multiplying two numbers go like this:
a_{1}a_{0} 8 5 * b_{1}b_{0} 4 7  c_{1}c_{0} 3 5 (c_{0} = a_{0} x b_{0}, overflow digit from c_{0} added to c_{1}) c_{2}c_{1} 5 9 (c_{1} = a_{1} x b_{0} + c_{1}, overflow digit from c_{1} added to c_{2}) c_{2}c_{1} 5 29 (c_{1} = a_{0} x b_{1} + c_{1}, 7 9 overflow from c_{1} added to c_{2}) c_{3}c_{2} 3 9 (c_{2} = a_{1} x b_{1} + c_{2}, overflow digit from c_{2} added to c_{3}) ========= c_{3}c_{2}c_{1}c_{0} 3 9 9 5 (bring down all the lowermost variables)Doing the computerizd math in these steps: (note that the c array is preinitialized to zero before you add)
c_{0} = c_{0} + a_{0} x b_{0} 35 = 0 + 5 x 7 c_{1} = c_{1} + INT(c_{0} / 10) 3 = 0 + INT(35/10) c_{0} = c_{0}  10 * INT(c_{0} / 10) 5 = 35  10*INT(35/10) c_{1} = c_{1} + a_{1} x b_{0} 59 = 3 + 8 x 7 c_{2} = c_{2} + INT(c_{1} / 10) 5 = 0 + INT(59/10) c_{1} = c_{1}  10 * INT(c_{1} / 10) 9 = 59  10*INT(59/10) c_{1} = c_{1} + a_{0} x b_{1} 29 = 9 + 5 x 4 c_{2} = c_{2} + INT(c_{1} / 10) 7 = 5 + INT(29/10) c_{1} = c_{1}  10 * INT(c_{1} / 10) 9 = 29  10*INT(29/10) c_{2} = c_{2} + a_{1} x b_{1} 39 = 7 + 8 x 4 c_{3} = c_{3} + INT(c_{2} / 10) 3 = 0 + INT(39/10) c_{2} = c_{2}  10 * INT(c_{2} / 10) 9 = 39  10*INT(39/10)Doing math with two 3digit numbers is noticibly harder:
a_{2}a_{1}a_{0} * b_{2}b_{1}b_{0}  c_{1}c_{0} (c_{0} = c_{0} + a_{0} x b_{0}) c_{2}c_{1} (c_{1} = c_{1} + a_{1} x b_{0}) c_{3}c_{2} (c_{2} = c_{2} + a_{2} x b_{0}) c_{2}c_{1} (c_{1} = c_{1} + a_{0} x b_{1}) c_{3}c_{2} (c_{2} = c_{2} + a_{1} x b_{1}) c_{4}c_{3} (c_{3} = c_{3} + a_{2} x b_{1}) c_{3}c_{2} (c_{2} = c_{2} + a_{0} x b_{2}) c_{4}c_{3} (c_{3} = c_{3} + a_{1} x b_{2}) c_{5}c_{4} (c_{4} = c_{4} + a_{2} x b_{2}) ============= c_{5}c_{4}c_{3}c_{2}c_{1}c_{0}So we need to simplify the multiplication into a formula. We could multiply and add the numbers in each element in one part and move the overflows to the next elements to the left. The addition formula would be much easier to program.
For the multiplication table:
Dimension a(100), b(100), c(10000) * you may want to use smaller or larger values of array sizes, but be sure that the sizes of arrays a() and b() multiply to the size of array c(). Select number a$ and b$ (we put these in string variables instead of numbers since the numbers can hold up to 8 or 16 significant digits. Break up the digits in a$ and b$ and place each of them into the array elements of a() and b(). The ones digit goes in element 0 for 10^{0}, the tens go element 1 for 10^{1}, the hundreds in element 2, 1000's in 3, and so forth. Suppose we can put this complicated thing into a simple loop formula. This is what abc x def looks like when coded straight: c_{0} = c_{0} + a_{0} x b_{0} c_{1} = c_{1} + a_{1} x b_{0} c_{2} = c_{2} + a_{2} x b_{0} c_{1} = c_{1} + a_{0} x b_{1} c_{2} = c_{2} + a_{1} x b_{1} c_{3} = c_{3} + a_{2} x b_{1} c_{2} = c_{2} + a_{0} x b_{2} c_{3} = c_{3} + a_{1} x b_{2} c_{4} = c_{4} + a_{2} x b_{2} In the first third of the program, the b() array stays the same while the other arays increments by one In the second third of the program, the b() array is one higher, and the other arrays increments by one, but where the c() array begins is one higher. In the third third, the b() array is up one more, the c() array begins another one higher. So, let's place the formula in a loop: FOR x = 0 to 2 FOR y = 0 to 2 c(x + y) = c(x + y) + a(x) * b(y) NEXT y NEXT x The length of a$ and b$ varies, so we need to include their provisions: FOR x = 0 to LEN(a$) FOR y = 0 to LEN(b$) c(x + y) = c(x + y) + a(y) * b(x) NEXT y NEXT x What about the overflow digits? c_{1} = c_{1} + INT(c_{0} / 10) c_{0} = c_{0}  10 * INT(c_{0} / 10) c_{2} = c_{2} + INT(c_{1} / 10) c_{1} = c_{1}  10 * INT(c_{1} / 10) c_{3} = c_{3} + INT(c_{2} / 10) c_{2} = c_{2}  10 * INT(c_{2} / 10) Lets try this method. Bear in mind that we need to set the loop to run as many times as there are digits in a$ and b$ combined. CAUTION: Do not reinitialize the c() array to 0! For x = 0 to LEN(a$)+LEN(b$) c(x+1) = c(x+1) + INT(c(x) / 10) c(x) = c(x)  10 * INT(c(x) / 10) NEXT xTo add two really big numbers together, we'll use this program:
For the addition loop, we go from this: c_{0} = a_{0} + b_{0} c_{1} = a_{1} + b_{1} To this: FOR x = 0 to LEN(a$) + LEN(b$) c(x) = a(x) + b(x) NEXT xLet's put the whole program together!
DIM a(100), b(100), c(10000) CLS DO a$ = "": b$ = "" PRINT "INPUT two whole numbers:"; : INPUT a$, b$ FOR x = 0 TO LEN(a$) + LEN(b$): a(x) = 0: b(x) = 0: NEXT x FOR x = 1 TO LEN(a$) a(x  1) = VAL(MID$(a$, LEN(a$)  x + 1, 1)) NEXT x FOR x = 1 TO LEN(b$) b(x  1) = VAL(MID$(b$, LEN(b$)  x + 1, 1)) NEXT x ' operation$ = "x" FOR x = 0 TO LEN(a$) + LEN(b$): c(x) = 0: NEXT x FOR x = 0 TO LEN(a$)  1 FOR y = 0 TO LEN(b$)  1 c(x + y) = c(x + y) + a(x) * b(y) NEXT y NEXT x FOR x = 0 TO LEN(a$) + LEN(b$) c(x + 1) = c(x + 1) + INT(c(x) / 10) c(x) = c(x)  10 * INT(c(x) / 10) NEXT x c$ = "" FOR x = (LEN(a$) + LEN(b$)) TO 0 STEP 1 c$ = c$ + LTRIM$(STR$(c(x))) IF LEFT$(c$, 1) = "0" AND x > 0 THEN c$ = "" NEXT x PRINT a$; " x "; b$; " = "; c$ ' operation$ = "+" FOR x = 0 TO LEN(a$) + LEN(b$): c(x) = 0: NEXT x FOR x = 0 TO LEN(a$) + LEN(b$) c(x) = a(x) + b(x) NEXT x FOR x = 0 TO LEN(a$) + LEN(b$) c(x + 1) = c(x + 1) + INT(c(x) / 10) c(x) = c(x)  10 * INT(c(x) / 10) NEXT x c$ = "" FOR x = LEN(a$) + LEN(b$) TO 0 STEP 1 c$ = c$ + LTRIM$(STR$(c(x))) IF LEFT$(c$, 1) = "0" AND x > 0 THEN c$ = "" NEXT x PRINT a$; " + "; b$; " = "; c$ LOOP

© 19952017. davesfunstuff.com All Rights Reserved. Reproduction of any part of this website without expressed written consent is prohibited.