Download presentation
Presentation is loading. Please wait.
PublishBritton Young Modified over 8 years ago
1
1D Arrays-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 2 31 -1 Note 2 31 -1 = 2147483647 = ~ 2.14 x 10 9 Note 13! > 4 x 10 9
2
1D Arrays-2 Example factorial program #include 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
1D Arrays-3 80 0 0 84 2976 85 256 4 97 5 Review of one dimensional arrays An array is a group of memory locations with a name…any single memory location may be accessed by means of a subscript [9] [8] [7] [6] [5] [4] [1] [3] [2] [0] Mult int Mult[10] /* declares this array */ /* Depending on version of C, array may be initially zero, or initially undefined */ /* Find sum of elements in Mult array */ Sum = 0; for (I=0; I<10; I++) Sum = Sum + Mult[I];
4
1D Arrays-4 [0] 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]; [4][3][2][1][5][9][8][7][6] 0628830000 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
1D Arrays-5 Warning Instructor about to sneak in new concept which will lead to homework problem
6
1D Arrays-6 [0][4][3][2][1][5][9][8][7][6] 0628830000 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
1D Arrays-7 [0][4][3][2][1][5][9][8][7][6] 0628830000 NumA unsigned short int NumA[10]; unsigned short int NumB[10]; unsigned short int NumC[10]; /* How could NumC = NumA + NumB be implimented ?*/ [0][4][3][2][1][5][9][8][7][6] 5079200000 NumB
8
1D Arrays-8 [0][4][3][2][1][5][9][8][7][6] 0 628830000 NumA [0][4][3][2][1][5][9][8][7][6] 5 079200000 NumB [0][4][3][2][1][5][9][8][7][6] 5 ????????? NumC NumC[0] = (NumA[0]+NumB[0]+Carry) % 10; Carry =(int)((NumA[0]+NumB[0]+Carry) / 10); 0 Carry 0 (before) (after)
9
1D Arrays-9 [0][4][3][2][1][5][9][8][7][6] 0628 8 30000 NumA [0][4][3][2][1][5][9][8][7][6] 5079 2 00000 NumB [0][4][3][2][1][5][9][8][7][6] 5??? 0 ????? NumC NumC[1] = (NumA[1]+NumB[1]+Carry) % 10; Carry =(int)((NumA[1]+NumB[1]+Carry) / 10); 0 Carry 1 (before) (after)
10
1D Arrays-10 [0][4][3][2][1][5][9][8][7][6] 062 8 830000 NumA [0][4][3][2][1][5][9][8][7][6] 507 9 200000 NumB [0][4][3][2][1][5][9][8][7][6] 5?? 8 0????? NumC NumC[2] = (NumA[2]+NumB[2]+Carry) % 10; Carry =(int)((NumA[2]+NumB[2]+Carry) / 10); 1 Carry 1 (before) (after)
11
1D Arrays-11 [0][4][3][2][1][5][9][8][7][6] 06 2 8830000 NumA [0][4][3][2][1][5][9][8][7][6] 50 7 9200000 NumB [0][4][3][2][1][5][9][8][7][6] 5? 0 80????? NumC NumC[3] = (NumA[3]+NumB[3]+Carry) % 10; Carry =(int)((NumA[3]+NumB[3]+Carry) / 10); 1 Carry 1 (before) (after)
12
1D Arrays-12 [0][4][3][2][1][5][9][8][7][6] 0 6 28830000 NumA [0][4][3][2][1][5][9][8][7][6] 5 0 79200000 NumB [0][4][3][2][1][5][9][8][7][6] 5 7 080????? NumC NumC[4] = (NumA[4]+NumB[4]+Carry) % 10; Carry =(int)((NumA[4]+NumB[4]+Carry) / 10); 1 Carry 0 (before) (after)
13
1D Arrays-13 [0][4][3][2][1][5][9][8][7][6] 06288 3 0000 NumA [0][4][3][2][1][5][9][8][7][6] 50792 0 0000 NumB [0][4][3][2][1][5][9][8][7][6] 57080 3 ???? NumC NumC[5] = (NumA[5]+NumB[5]+Carry) % 10; Carry =(int)((NumA[5]+NumB[5]+Carry) / 10); 0 Carry 0 (before) (after)
14
1D Arrays-14 [0][4][3][2][1][5][9][8][7][6] 062883000 0 NumA [0][4][3][2][1][5][9][8][7][6] 507920000 0 NumB [0][4][3][2][1][5][9][8][7][6] 570803??? 0 NumC NumC[6] = (NumA[6]+NumB[6]+Carry) % 10; Carry =(int)((NumA[6]+NumB[6]+Carry) / 10); 0 Carry 0 (before) (after) Note: cases 7, 8, and 9 are similar (identical) to case 6
15
1D Arrays-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
1D Arrays-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
1D Arrays-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
1D Arrays-18 In SUMmary (w typedef) /* near top of source file */ typedef unsigned short int uByte; /*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
1D Arrays-19 [0][4][3][2][1][5][9][8][7][6] 0000000000 NumZ [0][4][3][2][1][5][9][8][7][6] 0000000000 Answer 0 Carry(initial) Multiplication – A bit trickier Answer = NumA * NumB NumZ will hold intermediate values Start by initializing NumZ and Answer to zero Carry should also be initialized to zero
20
1D Arrays-20 [0][4][3][2][1][5][9][8][7][6] 3 028800000 NumA [0][4][3][2][1][5][9][8][7][6] 5 004600000 NumB [0][4][3][2][1][5][9][8][7][6] 5 000000000 NumZ NumZ[0] = (NumA[0]*NumB[0]+Carry) % 10; Carry =(int)((NumA[0]*NumB[0]+Carry) / 10); 0 Carry 1 (before) (after) Multiplication
21
1D Arrays-21 [0][4][3][2][1][5][9][8][7][6] 3028 8 00000 NumA [0][4][3][2][1][5][9][8][7][6] 5 004600000 NumB [0][4][3][2][1][5][9][8][7][6] 5000 1 00000 NumZ NumZ[1] = (NumA[1]*NumB[0]+Carry) % 10; Carry =(int)((NumA[1]*NumB[0]+Carry) / 10); 1 Carry 4 (before) (after) Multiplication
22
1D Arrays-22 [0][4][3][2][1][5][9][8][7][6] 302 8 800000 NumA [0][4][3][2][1][5][9][8][7][6] 5 004600000 NumB [0][4][3][2][1][5][9][8][7][6] 500 4 100000 NumZ NumZ[2] = (NumA[2]*NumB[0]+Carry) % 10; Carry =(int)((NumA[2]*NumB[0]+Carry) / 10); 4 Carry 4 (before) (after) Multiplication
23
1D Arrays-23 [0][4][3][2][1][5][9][8][7][6] 30 2 8800000 NumA [0][4][3][2][1][5][9][8][7][6] 5 004600000 NumB [0][4][3][2][1][5][9][8][7][6] 50 4 4100000 NumZ NumZ[3] = (NumA[3]*NumB[0]+Carry) % 10; Carry =(int)((NumA[3]*NumB[0]+Carry) / 10); 4 Carry 1 (before) (after) Multiplication
24
1D Arrays-24 [0][4][3][2][1][5][9][8][7][6] 3 0 28800000 NumA [0][4][3][2][1][5][9][8][7][6] 5 004600000 NumB [0][4][3][2][1][5][9][8][7][6] 5 1 44100000 NumZ NumZ[4] = (NumA[4]*NumB[0]+Carry) % 10; Carry =(int)((NumA[4]*NumB[0]+Carry) / 10); 1 Carry 0 (before) (after) Multiplication
25
1D Arrays-25 [0][4][3][2][1][5][9][8][7][6] 30288 0 0000 NumA [0][4][3][2][1][5][9][8][7][6] 5 004600000 NumB [0][4][3][2][1][5][9][8][7][6] 51441 0 0000 NumZ NumZ[5] = (NumA[5]*NumB[0]+Carry) % 10; Carry =(int)((NumA[5]*NumB[0]+Carry) / 10); 1 Carry 0 (before) (after) Multiplication Note: Cases 6, 7, 8, 9 are similar to case 5
26
1D Arrays-26 [0][4][3][2][1][5][9][8][7][6] 0000000000 Answer [0][4][3][2][1][5][9][8][7][6] 5144100000 NumZ [0][4][3][2][1][5][9][8][7][6] 5144100000 Answer Next step: Sum “Answer” and NumZ +
27
1D Arrays-27 [0][4][3][2][1][5][9][8][7][6] 3 028800000 NumA [0][4][3][2][1][5][9][8][7][6] 5004 6 00000 NumB [0][4][3][2][1][5][9][8][7][6] 0000 8 00000 NumZ NumZ[1] = (NumA[0]*NumB[1]+Carry) % 10; Carry =(int)((NumA[0]*NumB[1]+Carry) / 10); 0 Carry 1 (before) (after) Multiplication
28
1D Arrays-28 [0][4][3][2][1][5][9][8][7][6] 3028 8 00000 NumA [0][4][3][2][1][5][9][8][7][6] 5004 6 00000 NumB [0][4][3][2][1][5][9][8][7][6] 000 9 800000 NumZ NumZ[2] = (NumA[1]*NumB[1]+Carry) % 10; Carry =(int)((NumA[1]*NumB[1]+Carry) / 10); 1 Carry 4 (before) (after) Multiplication
29
1D Arrays-29 [0][4][3][2][1][5][9][8][7][6] 302 8 800000 NumA [0][4][3][2][1][5][9][8][7][6] 5004 6 00000 NumB [0][4][3][2][1][5][9][8][7][6] 00 2 9800000 NumZ NumZ[3] = (NumA[2]*NumB[1]+Carry) % 10; Carry =(int)((NumA[2]*NumB[1]+Carry) / 10); 4 Carry 5 (before) (after) Multiplication
30
1D Arrays-30 [0][4][3][2][1][5][9][8][7][6] 30 2 8800000 NumA [0][4][3][2][1][5][9][8][7][6] 5004 6 00000 NumB [0][4][3][2][1][5][9][8][7][6] 0 7 29800000 NumZ NumZ[4] = (NumA[3]*NumB[1]+Carry) % 10; Carry =(int)((NumA[3]*NumB[1]+Carry) / 10); 5 Carry 1 (before) (after) Multiplication
31
1D Arrays-31 [0][4][3][2][1][5][9][8][7][6] 3 0 28800000 NumA [0][4][3][2][1][5][9][8][7][6] 5004 6 00000 NumB [0][4][3][2][1][5][9][8][7][6] 07298 1 0000 NumZ NumZ[5] = (NumA[4]*NumB[1]+Carry) % 10; Carry =(int)((NumA[4]*NumB[1]+Carry) / 10); 1 Carry 0 (before) (after) Multiplication
32
1D Arrays-32 [0][4][3][2][1][5][9][8][7][6] 30288 0 0000 NumA [0][4][3][2][1][5][9][8][7][6] 5004 6 00000 NumB [0][4][3][2][1][5][9][8][7][6] 072981000 0 NumZ NumZ[6] = (NumA[5]*NumB[1]+Carry) % 10; Carry =(int)((NumA[5]*NumB[1]+Carry) / 10); 1 Carry 0 (before) (after) Multiplication Note: Cases 6, 7, 8, 9 are similar to case 5
33
1D Arrays-33 [0][4][3][2][1][5][9][8][7][6] 5144100000 Answer [0][4][3][2][1][5][9][8][7][6] 0729810000 NumZ [0][4][3][2][1][5][9][8][7][6] 5873910000 Answer Next step: Sum “Answer” and NumZ +
34
1D Arrays-34 [0][4][3][2][1][5][9][8][7][6] 3 028800000 NumA [0][4][3][2][1][5][9][8][7][6] 500 4 600000 NumB [0][4][3][2][1][5][9][8][7][6] 000 2 000000 NumZ NumZ[2] = (NumA[0]*NumB[2]+Carry) % 10; Carry =(int)((NumA[0]*NumB[2]+Carry) / 10); 0 Carry 1 (before) (after) Multiplication
35
1D Arrays-35 [0][4][3][2][1][5][9][8][7][6] 3028 8 00000 NumA [0][4][3][2][1][5][9][8][7][6] 500 4 600000 NumB [0][4][3][2][1][5][9][8][7][6] 00 3 2000000 NumZ NumZ[3] = (NumA[1]*NumB[2]+Carry) % 10; Carry =(int)((NumA[1]*NumB[2]+Carry) / 10); 1 Carry 3 (before) (after) Multiplication
36
1D Arrays-36 [0][4][3][2][1][5][9][8][7][6] 302 8 800000 NumA [0][4][3][2][1][5][9][8][7][6] 500 4 600000 NumB [0][4][3][2][1][5][9][8][7][6] 0 5 32000000 NumZ NumZ[4] = (NumA[2]*NumB[2]+Carry) % 10; Carry =(int)((NumA[2]*NumB[2]+Carry) / 10); 3 Carry 3 (before) (after) Multiplication
37
1D Arrays-37 [0][4][3][2][1][5][9][8][7][6] 30 2 8800000 NumA [0][4][3][2][1][5][9][8][7][6] 500 4 600000 NumB [0][4][3][2][1][5][9][8][7][6] 05320 1 0000 NumZ NumZ[5] = (NumA[3]*NumB[2]+Carry) % 10; Carry =(int)((NumA[3]*NumB[2]+Carry) / 10); 3 Carry 1 (before) (after) Multiplication
38
1D Arrays-38 [0][4][3][2][1][5][9][8][7][6] 3 0 28800000 NumA [0][4][3][2][1][5][9][8][7][6] 500 4 600000 NumB [0][4][3][2][1][5][9][8][7][6] 053201000 1 NumZ NumZ[6] = (NumA[4]*NumB[2]+Carry) % 10; Carry =(int)((NumA[4]*NumB[2]+Carry) / 10); 1 Carry 0 (before) (after) Multiplication
39
1D Arrays-39 [0][4][3][2][1][5][9][8][7][6] 30288 0 0000 NumA [0][4][3][2][1][5][9][8][7][6] 500 4 600000 NumB [0][4][3][2][1][5][9][8][7][6] 05320100 0 1 NumZ NumZ[7] = (NumA[5]*NumB[2]+Carry) % 10; Carry =(int)((NumA[5]*NumB[2]+Carry) / 10); 0 Carry 0 (before) (after) Multiplication Note: Cases 6, 7, 8, 9 are similar to case 5
40
1D Arrays-40 [0][4][3][2][1][5][9][8][7][6] 5873910000 Answer [0][4][3][2][1][5][9][8][7][6] 0532010001 NumZ [0][4][3][2][1][5][9][8][7][6] 5405930001 Answer Next step: Sum “Answer” and NumZ +
41
1D Arrays-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
1D Arrays-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
1D Arrays-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
1D Arrays-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
1D Arrays-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
1D Arrays-46 Multiply Summary /* At top of program */ #define MAXDIG 10 typedef 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
1D Arrays-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
1D Arrays-48 Multiply Summary /* Near top of source file */ #define MAXDIG 10 typedef unsigned short int uByte void 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
1D Arrays-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
1D Arrays-50 [0][4][3][2][1][5][9][8][7][6] 3 028800000 NumA 25 Multplr [0][4][3][2][1][5][9][8][7][6] 5 000000000 Ans Ans[0]= (NumA[0]*Multplr+Carry) % 10; Carry =(int)((NumA[0]*Multplr+Carry) / 10); 0 Carry 7 (before) (after) Multiplication (Way2)
51
1D Arrays-51 [0][4][3][2][1][5][9][8][7][6] 3028 8 00000 NumA 25 Multplr [0][4][3][2][1][5][9][8][7][6] 5000 7 00000 Ans Ans[1]= (NumA[1]*Multplr+Carry) % 10; Carry =(int)((NumA[1]*Multplr+Carry) / 10); 7 Carry 20 Carry (before) (after) Multiplication (Way2)
52
1D Arrays-52 [0][4][3][2][1][5][9][8][7][6] 302 8 800000 NumA 25 Multplr [0][4][3][2][1][5][9][8][7][6] 500 0 700000 Ans Ans[2]= (NumA[2]*Multplr+Carry) % 10; Carry =(int)((NumA[2]*Multplr+Carry) / 10); 20 Carry 22 Carry (before) (after) Multiplication (Way2)
53
1D Arrays-53 [0][4][3][2][1][5][9][8][7][6] 30 2 8800000 NumA 25 Multplr [0][4][3][2][1][5][9][8][7][6] 50 2 0700000 Ans Ans[3]= (NumA[3]*Multplr+Carry) % 10; Carry =(int)((NumA[3]*Multplr+Carry) / 10); 22 Carry 7 (before) (after) Multiplication (Way2)
54
1D Arrays-54 [0][4][3][2][1][5][9][8][7][6] 3 0 28800000 NumA 25 Multplr [0][4][3][2][1][5][9][8][7][6] 5 7 20700000 Ans Ans[4]= (NumA[4]*Multplr+Carry) % 10; Carry =(int)((NumA[4]*Multplr+Carry) / 10); 7 Carry 0 (before) (after) Multiplication (Way2)
55
1D Arrays-55 [0][4][3][2][1][5][9][8][7][6] 30288 0 0000 NumA 25 Multplr [0][4][3][2][1][5][9][8][7][6] 57207 0 0000 Ans Ans[5]= (NumA[5]*Multplr+Carry) % 10; Carry =(int)((NumA[5]*Multplr+Carry) / 10); 0 Carry 0 (before) (after) Multiplication (Way2) Cases 5,6,7,8,9 all identical
56
1D Arrays-56 Multiplication Summary (Way 2) Carry = 0; Ans[0]= (NumA[0]*Multplr+Carry) % 10; Carry =(int)((NumA[0]*Multplr+Carry) / 10); Ans[1]= (NumA[1]*Multplr+Carry) % 10; Carry =(int)((NumA[1]*Multplr+Carry) / 10); Ans[2]= (NumA[2]*Multplr+Carry) % 10; Carry =(int)((NumA[2]*Multplr+Carry) / 10); Ans[3]= (NumA[3]*Multplr+Carry) % 10; Carry =(int)((NumA[3]*Multplr+Carry) / 10); Ans[4]= (NumA[4]*Multplr+Carry) % 10; Carry =(int)((NumA[4]*Multplr+Carry) / 10); Ans[5]= (NumA[5]*Multplr+Carry) % 10; Carry =(int)((NumA[5]*Multplr+Carry) / 10);
57
1D Arrays-57 Multiplication Summary (Way 2) void SMult(uByte NumA[],int Multplr,uByte Ans[]) { int j, Carry=0; for (j=0; j<MAXDIG; j++) { Ans[j]= (NumA[j]*Multplr+Carry) % 10; Carry =(int)((NumA[j]*Multplr+Carry) / 10); } }
58
1D Arrays-58 Program Case Study Factorials 1! To 100! No “built in” data type in C will represent 100 factorial (or much beyond 13!) Note that (N+1)! = N! * (N+1) (If I know 51!, then multiplying it by 52 will give me 52 factorial) No input needed Need an output procedure (PrintNum) Have SMult, and SetZero procedures May need a CopyXtoY procedure (copy one uByte array to another)
59
1D Arrays-59 Case Study (1! To 100!) Variables needed: uByte Answer[MAXDIG], T[MAXDIG] int N Routines needed: SetZero(X), SMult(X,Multplr,Z), PrintNum(X) Possible Routines: CopyXtoY(X,Y) (X, Y, and Z above represent formal parameters of a uByte array) Overall Logic: T = 1; for N=1 to 100 Answer = T * N print N “!=“ Answer CopyXtoY(Answer,T)
60
1D Arrays-60 Case Study (1! To 100!) /* Needed global defs */ #include #define MAXDIG 200 #define MAXFACT 30 /* for testing */ typedef unsigned short int uByte;
61
1D Arrays-61 Case Study (1! To 100!) /* PrintNum – print an uByte array */ void PrintNum(uByte X[ ]) { int j, NonZeroSeen = 0; for (j=MAXDIG-1; j>=0; j--) { if (X[j] != 0) NonZeroSeen = 1; if (NonZeroSeen) printf(“%1d”,X[j]); else printf(“ “); } }
62
1D Arrays-62 Case Study (1! To 100!) /* Multiply a uByte array by a scalar */ void SMult(uByte X[],int M,uByte Z[]) { int j, Carry=0; for (j=0; j<MAXDIG; j++) { Z[j]= (X[j]*M+Carry) % 10; Carry =(int)((X[j]*M+Carry) / 10); } }
63
1D Arrays-63 Case Study (1! To 100!) /* Set an value to zero */ void SetZero(uByte X[ ]) { int j; for (j=0; j<MAXDIG; j++) X[j] = 0; }
64
1D Arrays-64 Case Study (1! To 100!) /* copy one uByte to another */ void CopyXtoY(uByte X[ ], uByte Y[ ]) { int j; for (j=0; j<MAXDIG; j++) Y[j] = X[j]; }
65
1D Arrays-65 Case Study (1! To 100!) /***************************************** * * * ECE 161 Case Study * * Factorials (1! To 100!) * * * * P. Viall – Spring 2001 * * * ****** Main Program - Overall Logic ****** * T=1 * * for N=1 to 100 * * Answer = T * N * * print N “!=“ Answer * * CopyAtoB(Answer,T) * *****************************************/
66
1D Arrays-66 Case Study (1! To 100!) void main(void) { uByte Ans[MAXDIG], T[MAXDIG]; int N; SetZero(T); T[0]=1; for (N=1; N<=MAXFACT; N++) { SMult(T,N,Ans); printf(“%4d != “,N); PrintNum(Ans); printf(“\n”); CopyXtoY(Ans,T); } }
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.