CHAPTER 02 2 CHAPTER
2-1 2-4 2-2 2-5 2-3 2-6
2-1 2-1-1 2-2
02 int A[3] = {10, 20, 30; A[0] 10 A[1] 20 A[2] 30 int *pa[3], A[3]; C 3 pa pa[0]pa[1]pa[2] 3 A A[0]A[1]A[2] 3 A A[0] A + i A[i] A + i &A[i]*(A + i) A[i] 2-3
2-1-2 int A[m][n]; [0][0] [0][1] [0][2] [0][n-1] [1][0] [1][1] [1][2] [1][n-1] [2][0] [2][1] [2][2] [2][n-1] [m-1][0] [m-1][1] [m-1][2] [m-1][n-1] 2-4
02 2-1-3 A[upper0] upper0 A[upper0][upper1] upper0upper1 A[upper0][upper1][upper2] upper0upper1 upper2 n A[upper0][upper1] [uppern-1] upper0upper1 uppern-1 2-5
2-2 int A[10]; A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] int X = A[0]; A[0] = 100; A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] 25 22 8 19 32 40 6 15 55 10 A[4] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] 25 22 8 19 10 32 40 6 15 55 2-6
02 A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] 25 22 8 19 32 40 6 15 55 A[2] A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] 25 22 19 32 40 6 15 55 A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] 25 22 8 19 32 40 6 15 55 A B B0] B[1] B[2] B[3] B[4] B[5] B[6] B[7] B[8] B[9] 25 22 8 19 32 40 6 15 55 A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] A[8] A[9] 25 22 8 19 32 40 6 15 55 ( 32) 2-7
2.1 [] \Ch02\ex2_1.c /* A n */ array_traverse(int A[], int n) { int i; for(i = 0; i < n; i++) printf("%d\n", A[i]); main() { int A[5] = {10, 20, 30, 40, 50; array_traverse(a, 5); 2.2 [] /* A n i value*/ array_insert(int A[], int n, int i, int value) { int j; if (i < 0 i >= n) return; /* i */ for(j = n - 1; j > i; j--) /* i */ A[j] = A[j - 1]; A[i] = value; /* i value*/ 2-8
02 2.3 [] /* A n i */ array_delete(int A[], int n, int i) { int j; if (i < 0 i >= n) return; /* i */ for(j = i; j < n - 1; j++) /**/ A[j] = A[j + 1]; A[n - 1] = 0; /* 0*/ A = B = A t = 2-9
2-10
02 2-11
2.4[] \Ch02\ex2_4.c /* A mn */ matrix_traverse(int m, int n, int A[m][n]) { int i, j; for(i = 0; i < m; i++){ for(j = 0; j < n; j++) printf("%d ", A[i][j]); printf("\n"); main() { int A[2][3] = {{11, 12, 13, {21, 22, 23; matrix_traverse(2, 3, A); 2.5[] /* A mn A B B nm */ matrix_transpose(int m, int n, int A[m][n], int B[n][m]) { int i, j; for(i = 0; i < m; i++) for(j = 0; j < n; j++) B[j][i] = A[i][j]; 2-12
02 2.6[] /* ABC mn C=A+B*/ matrix_add(int m, int n, int A[m][n], int B[m][n], int C[m][n]) { int i, j; for(i = 0; i < m; i++) for(j = 0; j < n; j++) C[i][j] = A[i][j] + B[i][j]; 1. [] 2. [] 3. [] for(i = 0; i < m; i++) for(j = 0; j < p; j++){ C[i][j] = 0; for(k = 0; k < n; k++) C[i][j] += A[i][k] * B[k][j]; 2-13
2-3 α α int A[5]; A[0] A[1] + 1 x sizeof(int) A[0] A[1] A[2] A[3] A[4] A[2] A[3] A[4] + 2 x sizeof(int) + 3 x sizeof(int) + 4 x sizeof(int) α α α 2-14
02 2.7 2.8 α α α α 2.9 2-15
α α α α 2.10 n α α + i 0 x upper 1 x upper 2 x x upper n-1 A[i 0 ][i 1 ][0] [0] α + i 0 x upper 1 x upper 2 x x upper n-1 + i 1 x upper 2 x upper 3 x x upper n-1 2-16
02 2.11 α α α α α α α α 1. 2. 3. 2-17
2-4 2-4-1 Polynomial [0] [1] [2] [n] n c n c n-1 c 1 n+2 [n+1 ] c 0 Polynomial [0] [1] [2] [2m-1] [2m] m c m-1 e m-1 c 0 e 0 2m+1 2-18
02 typedef struct{ int coef; int exp; NonZeroTerm; #define MAX_SIZE 100 typedef struct{ int count; NonZeroTerm terms[max_size]; Polynomial; /**/ /**/ /**/ /* MAX_SIZE */ /**/ /**/ /**/ Polynomial A, B; count 4 terms [0] [1] [2] [3] coef exp coef exp coef exp coef exp 3 5 8 4-6 2 5 0 count 3 terms [0] [1] [2] coef exp coef exp coef exp 2 6 4 2 1 0 2-19
2.12[] \Ch02\ex2_12.c ( 1/3) /* xy x < y -1 x == y 0 x > y 1*/ #define COMPARE(x, y) ((x < y)? -1 : (x == y)? 0 : 1) #define MAX_SIZE 100 /* MAX_SIZE */ typedef struct{ /**/ int coef; /**/ int exp; /**/ NonZeroTerm; 2-20
02 \Ch02\ex2_12.c ( 2/3) typedef struct{ int count; NonZeroTerm terms[max_size]; Polynomial; /**/ /**/ /**/ /**/ main() { Polynomial A, B, C; A.count = 0; attach(&a, 3, 5); attach(&a, 8, 4); attach(&a, -6, 2); attach(&a, 5, 0); B.count = 0; attach(&b, 2, 6); attach(&b, 4, 2); attach(&b, 1, 0); /* Polynomial ABC*/ /* A(X) = 3X 5 + 8X 4-6X 2 + 5 */ /* B(X) = 2X 6 + 4X 2 + 1 */ PolyAdd(&A, &B, &C); /* C(X) = A(X) + B(X)*/ int i; /* C(X) */ printf(" C(X)%d \n", C.count); for(i = 0; i < C.count; i++) printf("%d %d\t %d\n", i+1, C.terms[i].coef, C.terms[i].exp); /**/ attach(polynomial *ptr, int coef, int exp) { if (ptr->count >= MAX_SIZE) return; ptr->terms[ptr->count].coef = coef; ptr->terms[ptr->count].exp = exp; ptr->count++; 2-21
\Ch02\ex2_12.c ( 3/3) /* C(X) = A(X) + B(X)*/ PolyAdd(Polynomial *pa, Polynomial *pb, Polynomial *pc) { int currenta = 0, currentb = 0; pc->count = 0; while(currenta < pa->count && currentb < pb->count){ switch(compare(pa->terms[currenta].exp, pb->terms[currentb].exp)){ /* A B B */ case -1: attach(pc, pb->terms[currentb].coef, pb->terms[currentb].exp); currentb++; break; /* A B */ case 0: if((pa->terms[currenta].coef + pb->terms[currentb].coef)!= 0) attach(pc, pa->terms[currenta].coef + pb->terms[currentb].coef, pa->terms[currenta].exp); currenta++; currentb++; break; /* A B A */ case 1: attach(pc, pa->terms[currenta].coef, pa->terms[currenta].exp); currenta++; while(currenta < pa->count){ /* A */ attach(pc, pa->terms[currenta].coef, pa->terms[currenta].exp); currenta++; while(currentb < pb->count){ /* B */ attach(pc, pb->terms[currentb].coef, pb->terms[currentb].exp); currentb++; 2-22
02 2-4-2 A = 45 #define MAX_SIZE 100 typedef struct{ int row; int col; int value; SparseTerm; /* MAX_SIZE */ /**/ /**/ /**/ /**/ SparseTerm A[MAX_SIZE]; A [0] [1] [2] [3] [4] [5] [6] row 4 0 0 1 2 2 3 col 5 1 4 3 0 2 4 value 6 1 2 3 4 5 6 2-23
2.13[] B [0] [1] [2] [3] [4] [5] [6] row 5 0 1 2 3 4 4 col 4 2 0 2 1 0 3 value 6 4 1 5 3 2 6 SparseTranspose(SparseTerm A[], SparseTerm B[]) /* B = A t */ { int currentb; int i, j; B[0].row = A[0].col; 2-24
02 B[0].col = A[0].row; B[0].value = A[0].value; if (B[0].value == 0) return; currentb = 1; for(i = 0; i < A[0].col; i++) for(j = 0; j <= B[0].value; j++) if (A[j].col == i){ B[currentB].row = A[j].col; B[currentB].col = A[j].row; B[currentB].value = A[j].value; currentb++; /**/ /* A */ /**/ /* B*/ 2.14[] 2-25
1. [] 2. [] 2-26
02 2-5 char name1[] = "Jean Chen"; char name2[10] = "Mary"; char name3[5] = {'J', 'o', 'e', '\0'; [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] name1 J e a n C h e n \0 [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] name2 M a r y \0 [0] [1] [2] [3] [4] name3 J o e \0 printf("%c", name1[3]); /* name1 'n' */ name3[2] = 'y'; /* name3 'y' */ 2-27
char str[] = "Jean Chen"; printf("%d", strlen(str)); /* str 9*/ char str[50]; strcpy(str, "Happy New Year"); /* "Happy New Year" str */ char str1[] = "Happy Birthday"; char str2[] = "Happy New Year"; printf("%d", strcmp(str1, str2)); /* -1 str1 str2*/ 2-28
02 2.15[] int StrLength(char str[]) { int i = 0; while(str[i]!= '\0') i++; return i; 2.16[] StrCopy(char str1[], char str2[]) { int i = 0; while(str2[i]!= '\0'){ str1[i] = str2[i]; i++; str1[i] = '\0'; 2-29
2.17[ (pattern matching)] int StrMatch(char str[], char pat[]) { int i, j; int start = 0; /* str */ int end = strlen(str) - 1; /* str */ int endp = strlen(pat) - 1; /* pat */ int endmatch = endp; /* str pat */ for(i = 0; endmatch <= end; endmatch++, start++){ if (str[endmatch] == pat[endp]) for(j = 0, i = start; j < endp && str[i] == pat[j]; i++, j++); if (j == endp) return start; /**/ return -1; /**/ 2-30
02 j endp pat[] = y y x \0 start end match end str[] = y x y x x x y y x x x \0 no match start end match end y x y x x x y y x x x \0 no match start end match end y x y x x x y y x x x \0 no match start end match end y x y x x x y y x x x \0 no match start end match end y x y x x x y y x x x \0 no match start end match end y x y x x x y y x x x \0 no match start end match end y x y x x x y y x x x \0 match 2-31
2-6 typedef struct{ int year; int month; int day; DATE; typedef struct{ char name[50]; DATE birthday; PERSON; PERSON employee; employee.name = "Mary Wang"; employee.birthday.year = 1995; employee.birthday.month = 2; employee.birthday.day = 14; 2-32
02 α α α α α α α 2-33
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. count? terms [0] [1] [2] coef exp coef exp coef exp coef exp?????? 2-34
02 11. 12. [] 13. 14. 15. [ ] 2-35