660 likes | 859 Vues
Factorials to N digits. N! = 1 * 2 * 3 * 4 …. (N-2) * (N-1) * N Factorials get very big very fast long = 32 bits on most computers 32 bits gives a range of ± 2 31 -1 Note ± 2 31 -1 = ± 2147483647 = ~ 2.14 x 10 9 Note 13! > 4 x 10 9. Example factorial program.
E N D
Factorials to N digits. • N! = 1 * 2 * 3 * 4 …. (N-2) * (N-1) * N • Factorials get very big very fast • long = 32 bits on most computers • 32 bits gives a range of ±231-1 • Note ±231-1 = ± 2147483647 = ~ 2.14 x 109 • Note 13! > 4 x 109
Example factorial program #include <stdio.h>main(){long I, N, Nfact; printf(“N Factorial program\nEnter a number: \n”); scanf(“%d”,&N); Nfact = 1;for (I = 1, I <= N; I++) { Nfact = Nfact * I; } printf(“N!=%d”,Nfact);}
Review of one dimensional arrays Mult 5 [0] 256 [1] An array is a group of memory locations with a name…any single memory location may be accessed by means of a subscript 97 [2] 4 [3] 85 [4] int Mult[10] /* declares this array *//* Depending on version of C, array may be initially zero, or initially undefined */ 2976 [5] 84 [6] 0 [7] /* Find sum of elements in Mult array */ Sum = 0;for (I=0; I<10; I++) Sum = Sum + Mult[I]; 0 [8] 80 [9]
Another Application of Arrays Although 1-D Arrays are generally viewed as a column, they could be viewed as a row…for example: unsigned short int Multiplicand[10]; [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 3 6 2 8 8 0 Multiplicand Each element of Multiplicand[ ] may hold a value between 0 and 255. I could choose to interpret the above array to represent the value 362,880
Warning Warning Warning Instructor about to sneak in new concept which will lead to homework problem
[9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 3 6 2 8 8 0 NumA unsigned short int NumA[10]; If I choose to view the above array as a single value of 362,880…Then I need to develop ways to perform arthmetic operations on this new data type.
[9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 3 6 2 8 8 0 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 7 9 2 5 NumB unsigned short int NumA[10];unsigned short int NumB[10];unsigned short int NumC[10]; /* How could NumC = NumA + NumB be implimented ?*/
(before) 0 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 3 6 2 8 8 0 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 7 9 2 5 NumB NumC[0] = (NumA[0]+NumB[0]+Carry) % 10;Carry =(int)((NumA[0]+NumB[0]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] ? ? ? ? ? ? ? ? ? 5 NumC (after) 0 Carry
(before) 0 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 3 6 2 8 8 0 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 7 9 2 5 NumB NumC[1] = (NumA[1]+NumB[1]+Carry) % 10; Carry =(int)((NumA[1]+NumB[1]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] ? ? ? ? ? ? ? ? 0 5 NumC (after) 1 Carry
(before) 1 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 3 6 2 8 8 0 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 7 9 2 5 NumB NumC[2] = (NumA[2]+NumB[2]+Carry) % 10; Carry =(int)((NumA[2]+NumB[2]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] ? ? ? ? ? ? ? 8 0 5 NumC (after) 1 Carry
(before) 1 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 3 6 2 8 8 0 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 7 9 2 5 NumB NumC[3] = (NumA[3]+NumB[3]+Carry) % 10; Carry =(int)((NumA[3]+NumB[3]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] ? ? ? ? ? ? 0 8 0 5 NumC (after) 1 Carry
(before) 1 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 3 6 2 8 8 0 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 7 9 2 5 NumB NumC[4] = (NumA[4]+NumB[4]+Carry) % 10; Carry =(int)((NumA[4]+NumB[4]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] ? ? ? ? ? 7 0 8 0 5 NumC (after) 0 Carry
(before) 0 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 3 6 2 8 8 0 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 7 9 2 5 NumB NumC[5] = (NumA[5]+NumB[5]+Carry) % 10; Carry =(int)((NumA[5]+NumB[5]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] ? ? ? ? 3 7 0 8 0 5 NumC (after) 0 Carry
(before) 0 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 3 6 2 8 8 0 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 7 9 2 5 NumB NumC[6] = (NumA[6]+NumB[6]+Carry) % 10; Carry =(int)((NumA[6]+NumB[6]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] ? ? ? 0 3 7 0 8 0 5 NumC Note: cases 7, 8, and 9 are similar (identical) to case 6 (after) 0 Carry
In SUMmary Carry = 0; NumC[0] = (NumA[0]+NumB[0]+Carry) % 10; Carry =(int)((NumA[0]+NumB[0]+Carry) / 10); NumC[1] = (NumA[1]+NumB[1]+Carry) % 10; Carry =(int)((NumA[1]+NumB[1]+Carry) / 10); NumC[2] = (NumA[2]+NumB[2]+Carry) % 10; Carry =(int)((NumA[2]+NumB[2]+Carry) / 10); NumC[3] = (NumA[3]+NumB[3]+Carry) % 10; Carry =(int)((NumA[3]+NumB[3]+Carry) / 10); : :NumC[9] = (NumA[9]+NumB[9]+Carry) % 10; Carry =(int)((NumA[9]+NumB[9]+Carry) / 10);
In SUMmary Carry = 0; for (J=0; J<10; J++){NumC[J] = (NumA[J]+NumB[J]+Carry) % 10; Carry =(int)((NumA[J]+NumB[J]+Carry) / 10);}
In SUMmary /*Routine to sum three items of my data type*/ void MySum(unsigned short int NumA[ ],unsigned short int NumB[ ],unsigned short int NumC[ ] ){unsigned short int Carry = 0;unsigned short int J; for (J=0; J<10; J++) { NumC[J]= (NumA[J]+NumB[J]+Carry) % 10; Carry=(int)((NumA[J]+NumB[J]+Carry) / 10); }}
In SUMmary (w typedef) /* near top of source file */typedef unsigned short intuByte;/*Routine to sum three items of my data type*/void MySum(uByte NumA[], uByte NumB[], uByte NumC[]){ uByte Carry = 0; uByte J; for (J=0; J<10; J++) { NumC[J]= (NumA[J]+NumB[J]+Carry) % 10; Carry=(int)((NumA[J]+NumB[J]+Carry) / 10); }}
Multiplication – A bit trickier Answer = NumA * NumBNumZ will hold intermediate values Start by initializing NumZ and Answer to zeroCarry should also be initialized to zero [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 0 0 0 NumZ [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 0 0 0 Answer (initial) 0 Carry
Multiplication (before) 0 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 6 5 NumB NumZ[0] = (NumA[0]*NumB[0]+Carry) % 10; Carry =(int)((NumA[0]*NumB[0]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 0 0 5 NumZ (after) 1 Carry
Multiplication (before) 1 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 6 5 NumB NumZ[1] = (NumA[1]*NumB[0]+Carry) % 10; Carry =(int)((NumA[1]*NumB[0]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 0 1 5 NumZ (after) 4 Carry
Multiplication (before) 4 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 6 5 NumB NumZ[2] = (NumA[2]*NumB[0]+Carry) % 10; Carry =(int)((NumA[2]*NumB[0]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 1 5 NumZ (after) 4 Carry
Multiplication (before) 4 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 6 5 NumB NumZ[3] = (NumA[3]*NumB[0]+Carry) % 10; Carry =(int)((NumA[3]*NumB[0]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 4 4 1 5 NumZ (after) 1 Carry
Multiplication (before) 1 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 6 5 NumB NumZ[4] = (NumA[4]*NumB[0]+Carry) % 10; Carry =(int)((NumA[4]*NumB[0]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 1 4 4 1 5 NumZ (after) 0 Carry
Multiplication (before) 1 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 6 5 NumB NumZ[5] = (NumA[5]*NumB[0]+Carry) % 10; Carry =(int)((NumA[5]*NumB[0]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 1 4 4 1 5 NumZ (after) 0 Carry Note: Cases 6, 7, 8, 9 are similar to case 5
Next step: Sum “Answer” and NumZ [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 0 0 0 Answer + [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 1 4 4 1 5 NumZ [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 1 4 4 1 5 Answer
Multiplication (before) 0 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 6 5 NumB NumZ[1] = (NumA[0]*NumB[1]+Carry) % 10; Carry =(int)((NumA[0]*NumB[1]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 0 8 0 NumZ (after) 1 Carry
Multiplication (before) 1 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 6 5 NumB NumZ[2] = (NumA[1]*NumB[1]+Carry) % 10; Carry =(int)((NumA[1]*NumB[1]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 9 8 0 NumZ (after) 4 Carry
Multiplication (before) 4 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 6 5 NumB NumZ[3] = (NumA[2]*NumB[1]+Carry) % 10; Carry =(int)((NumA[2]*NumB[1]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 9 8 0 NumZ (after) 5 Carry
Multiplication (before) 5 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 6 5 NumB NumZ[4] = (NumA[3]*NumB[1]+Carry) % 10; Carry =(int)((NumA[3]*NumB[1]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 7 2 9 8 0 NumZ (after) 1 Carry
Multiplication (before) 1 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 6 5 NumB NumZ[5] = (NumA[4]*NumB[1]+Carry) % 10; Carry =(int)((NumA[4]*NumB[1]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 1 7 2 9 8 0 NumZ (after) 0 Carry
Multiplication (before) 1 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 6 5 NumB NumZ[6] = (NumA[5]*NumB[1]+Carry) % 10; Carry =(int)((NumA[5]*NumB[1]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 1 7 2 9 8 0 NumZ (after) 0 Carry Note: Cases 6, 7, 8, 9 are similar to case 5
Next step: Sum “Answer” and NumZ [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 1 4 4 1 5 Answer + [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 1 7 2 9 8 0 NumZ [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 1 8 7 3 9 5 Answer
Multiplication (before) 0 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 6 5 NumB NumZ[2] = (NumA[0]*NumB[2]+Carry) % 10; Carry =(int)((NumA[0]*NumB[2]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 2 0 0 NumZ (after) 1 Carry
Multiplication (before) 1 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 6 5 NumB NumZ[3] = (NumA[1]*NumB[2]+Carry) % 10; Carry =(int)((NumA[1]*NumB[2]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 3 2 0 0 NumZ (after) 3 Carry
Multiplication (before) 3 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 6 5 NumB NumZ[4] = (NumA[2]*NumB[2]+Carry) % 10; Carry =(int)((NumA[2]*NumB[2]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 5 3 2 0 0 NumZ (after) 3 Carry
Multiplication (before) 3 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 6 5 NumB NumZ[5] = (NumA[3]*NumB[2]+Carry) % 10; Carry =(int)((NumA[3]*NumB[2]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 1 5 3 2 0 0 NumZ (after) 1 Carry
Multiplication (before) 1 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 6 5 NumB NumZ[6] = (NumA[4]*NumB[2]+Carry) % 10; Carry =(int)((NumA[4]*NumB[2]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 1 1 5 3 2 0 0 NumZ (after) 0 Carry
Multiplication (before) 0 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 4 6 5 NumB NumZ[7] = (NumA[5]*NumB[2]+Carry) % 10; Carry =(int)((NumA[5]*NumB[2]+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 1 1 5 3 2 0 0 NumZ (after) 0 Carry Note: Cases 6, 7, 8, 9 are similar to case 5
Next step: Sum “Answer” and NumZ [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 1 8 7 3 9 5 Answer + [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 1 1 5 3 2 0 0 NumZ [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 1 3 4 0 5 9 5 Answer
Multiply Summary Carry=0; Answer[]=0; NumZ[]=0; NumZ[0] = (NumA[0]*NumB[0]+Carry) % 10;Carry =(int)((NumA[0]*NumB[0]+Carry) / 10); NumZ[1] = (NumA[1]*NumB[0]+Carry) % 10;Carry =(int)((NumA[1]*NumB[0]+Carry) / 10); NumZ[2] = (NumA[2]*NumB[0]+Carry) % 10; Carry =(int)((NumA[2]*NumB[0]+Carry) / 10); : :NumZ[8] = (NumA[8]*NumB[0]+Carry) % 10;Carry =(int)((NumA[8]*NumB[0]+Carry) / 10); NumZ[9] = (NumA[9]*NumB[0]+Carry) % 10;Carry =(int)((NumA[9]*NumB[0]+Carry) / 10);Answer = Answer + NumZ;
Multiply Summary NumZ = 0 NumZ[1] = (NumA[0]*NumB[1]+Carry) % 10;Carry =(int)((NumA[0]*NumB[1]+Carry) / 10); NumZ[2] = (NumA[1]*NumB[1]+Carry) % 10;Carry =(int)((NumA[1]*NumB[1]+Carry) / 10); NumZ[3] = (NumA[2]*NumB[1]+Carry) % 10; Carry =(int)((NumA[2]*NumB[1]+Carry) / 10); : :NumZ[9] = (NumA[8]*NumB[1]+Carry) % 10;Carry =(int)((NumA[8]*NumB[1]+Carry) / 10); Answer = Answer + NumZ;
Multiply Summary NumZ = 0 NumZ[2] = (NumA[0]*NumB[2]+Carry) % 10;Carry =(int)((NumA[0]*NumB[2]+Carry) / 10); NumZ[3] = (NumA[1]*NumB[2]+Carry) % 10;Carry =(int)((NumA[1]*NumB[2]+Carry) / 10); NumZ[4] = (NumA[2]*NumB[2]+Carry) % 10; Carry =(int)((NumA[2]*NumB[2]+Carry) / 10); : :NumZ[9] = (NumA[7]*NumB[2]+Carry) % 10;Carry =(int)((NumA[7]*NumB[2]+Carry) / 10); Answer = Answer + NumZ;
Multiply Summary NumZ = 0 NumZ[3] = (NumA[0]*NumB[3]+Carry) % 10;Carry =(int)((NumA[0]*NumB[3]+Carry) / 10); NumZ[4] = (NumA[1]*NumB[3]+Carry) % 10;Carry =(int)((NumA[1]*NumB[3]+Carry) / 10); NumZ[5] = (NumA[2]*NumB[3]+Carry) % 10; Carry =(int)((NumA[2]*NumB[3]+Carry) / 10); : :NumZ[9] = (NumA[6]*NumB[3]+Carry) % 10;Carry =(int)((NumA[6]*NumB[3]+Carry) / 10); Answer = Answer + NumZ;
Multiply Summary NumZ = 0 NumZ[4] = (NumA[0]*NumB[4]+Carry) % 10;Carry =(int)((NumA[0]*NumB[4]+Carry) / 10); NumZ[5] = (NumA[1]*NumB[4]+Carry) % 10;Carry =(int)((NumA[1]*NumB[4]+Carry) / 10); NumZ[6] = (NumA[2]*NumB[4]+Carry) % 10; Carry =(int)((NumA[2]*NumB[4]+Carry) / 10); : :NumZ[9] = (NumA[5]*NumB[4]+Carry) % 10;Carry =(int)((NumA[5]*NumB[4]+Carry) / 10); Answer = Answer + NumZ;
Multiply Summary /* At top of program */ #define MAXDIG 10typedef unsigned short int uByte /* Set an value to zero */ void SetZero(uByte X[ ]){int I;for (I=0; I<MAXDIG; I++) X[I] = 0;}
Multiply Summary #define MAXDIG 10 SetZero(Answer); /* Answer = 0 */for (I=0; I<MAXDIG/2; I++){ Carry=0; SetZero(NumZ); /* NumZ = 0 */for (J=0; J<MAXDIG/2; J++) { NumZ[I+J]= (NumA[J]*NumB[I]+Carry)%10; Carry=(int)((NumA[J]*NumB[I]+Carry)/10); } MySum(Answer,NumZ,Answer);}
Multiply Summary /* Near top of source file */#define MAXDIG 10typedef unsigned short int uBytevoid ArrayNumMult(uByte NumA[ ],uByte NumB[ ], uByte Ans[ ]){ /* procedure to multiply two ArrayNum's together */int I,J; uByte NumZ[MAXDIG],Carry=0; SetZero(Ans); for (I=0; I<MAXDIG/2; I++) { Carry=0; SetZero(NumZ); for (J=0; J<MAXDIG/2; J++) { NumZ[I+J]= (NumA[J]*NumB[I]+Carry)%10; Carry=(int)((NumA[J]*NumB[I]+Carry)/10); } MySum(Ans,NumZ,Ans);}
Multiply (Way 2) • Multiply an “ArrayNum” by a “scalar” • In example we’ll use an ArrayNum * int • Similar to ArrayNum * ArrayNum, but easier
Multiplication (Way2) (before) 0 Carry [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 2 8 8 3 NumA 25 Multplr Ans[0]= (NumA[0]*Multplr+Carry) % 10; Carry =(int)((NumA[0]*Multplr+Carry) / 10); [9] [8] [7] [6] [5] [4] [3] [2] [1] [0] 0 0 0 0 0 0 0 0 0 5 Ans (after) 7 Carry