1 / 66

Factorials to N digits.

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.

diem
Download Presentation

Factorials to N digits.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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

  2. 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);}

  3. 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]

  4. 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

  5. Warning Warning Warning Instructor about to sneak in new concept which will lead to homework problem

  6. [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.

  7. [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 ?*/

  8. (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

  9. (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

  10. (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

  11. (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

  12. (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

  13. (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

  14. (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

  15. 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);

  16. 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);}

  17. 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); }}

  18. 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); }}

  19. 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

  20. 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

  21. 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

  22. 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

  23. 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

  24. 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

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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

  30. 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

  31. 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

  32. 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

  33. 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

  34. 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

  35. 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

  36. 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

  37. 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

  38. 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

  39. 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

  40. 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

  41. 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;

  42. 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;

  43. 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;

  44. 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;

  45. 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;

  46. 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;}

  47. 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);}

  48. 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);}

  49. Multiply (Way 2) • Multiply an “ArrayNum” by a “scalar” • In example we’ll use an ArrayNum * int • Similar to ArrayNum * ArrayNum, but easier

  50. 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

More Related