Q 1 10 6 10 baby Baby Month
Q 1 以下是 10個初生嬰兒,首 6個月的體重改變紀錄 第 10個 baby Baby# Month & wt W 1[] W 2[] 0 2. 4 2. 6 2. 7 1 3. 2 3. 6 2 4. 1 4. 5 4. 6 3 5. 0 5. 4 4 6. 5 5. 9 6. 6 5 6. 4 7. 3 6 6. 3 6. 7 7. 7 1月 … W 10[] float w 1[7]={2. 4, 3. 2, 4. 1, 5. 0, 6. 5, 6. 4, 6. 3}; float w 2[7]={2. 6, 3. 6, 4. 5, 5. 4, 5. 9, 6. 4, 6. 7}; 10/30/2020 CIT 2008 kg 1
float w 1[7]={2. 4, 3. 2, 4. 1, 5. 0, 6. 5, 6. 4, 6. 3}; float w 2[7]={2. 6, 3. 6, 4. 5, 5. 4, 5. 9, 6. 4, 6. 7}; // 有邏輯錯誤的程式段 float s, ave; for (i=0; i<=6; i++){ s = 0; s = s + w 1[i]; ave = s / 7; } printf("平均體重 = %. 1 fn", ave); 0. 9 ave的值: ______ 10/30/2020 // 修正後 float s, ave; // sum, average s = 0; for (i=0; i<=6; i++){ s = s + w 1[i]; } ave = s / 7; printf("平均體重 = %. 1 fn", ave); 4. 8 ave的值: ______ CIT 2008 2
C[0][6] #define MAX 10 float C[MAX][7]={2. 4, 3. 2, 4. 1, 5. 0, 6. 5, 6. 4, 6. 3, 2. 6, 3. 6, 4. 5, 5. 4, 5. 9, 6. 4, 6. 7, 2. 7, 3. 6, 4. 6, 5. 4, 6. 6, 7. 3, 7. 7}; 提示: 先Baby# 後Month // weight difference 體重相差 float WD (int bno, int mnth){ return (C[bno][mnth]-C[bno][mnth-1]); } 本月-上月 x = WD(0, 1); Month Baby# & wt 0 1 2 3 4 5 6月 0 2. 4 3. 2 4. 1 5. 0 6. 5 6. 4 6. 3 1 2. 6 3. 6 4. 5 5. 4 5. 9 6. 4 6. 7 3. 6 4. 6 5. 4 6. 6 7. 3 7. 7 . . . 9 10/30/2020 2. 7 CIT 2008 3
#define MAX 10 float C[MAX][7]={2. 4, 3. 2, 4. 1, 5. 0, 6. 5, 6. 4, 6. 3, 2. 6, 3. 6, 4. 5, 5. 4, 5. 9, 6. 4, 6. 7, 2. 7, 3. 6, 4. 6, 5. 4, 6. 6, 7. 3, 7. 7}; C[0][6] void checkbaby(){ int healthy, i, j; // i=baby, j=month 體重相差<0 for (i=0; i<MAX; i++){ 出現負增長 healthy = 1; for (j=1; j<=6; j++) // 6 months if (WD(i, j)<=0) healthy = 0; if (healthy==1) printf(“Baby %d Healthy健康n", i); else printf("Baby %d Unhealthyn", i); } 10/30/2020 CIT 2008 } 4
陣列 2 a A[ ] 鏈表 1 P 2 T 3 R 4 S 5 Q p (i) 陣列 5 × 50 = 250 bytes 50 head P 資料A[] 指標Pointer 10/30/2020 T 記憶體需求量多少? (ii) 鏈表 4 + 5×(50+4) = 274 bytes 4 R S Q nil : 50 bytes : 4 bytes CIT 2008 5
First Front 2 c A[ ] 1 P F 1 1 F 4 2 T Size 5 2 Size 2 3 R 3 4 S 5 Q F 4 Size 3 A[ ] 1 U 2 3 10/30/2020 CIT 2008 4 S 5 Q 7
刪除數據元素 remove a data item, SIZE = 0 如果 _____ 便顯示 “The queue is empty. ” 及停止執行 刪除Remove A[F] F+1 F = _____ 6 F=1 如果F = _____ 便 _____ SIZE – 1 SIZE = ______ 新增數據元素 append a data item U, SIZE = 5 如果 _____ 便顯示 “The queue is full. ” 及停止執行 F + SIZE tempindex = _____ TEMPINDEX = TEMPINDEX – 5 >5 如果 tempindex _____ 便 __________ A[tempindex] U SIZE + 1 SIZE = _____ 10/30/2020 CIT 2008 8
3 A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] 5 2 4 3 6 8 7 1 #include <stdio. h> int A[8]={5, 2, 4, 3, 6, 8, 7, 1}; 11 int func 1(int n){ 13 int s, i; 14 s = -1; // 找不到n 15 for (i=0; i<8; i++) 16 if (n==A[i]) 17 s=i; 18 return s; 19 } 10/30/2020 (a) 4 func 1(6) = ______ -1 func 1(9) = ______ 8 行16執行了至少______次 (b) 返回值代表什麼? 數字n 在陣列A[ ]中的位置 CIT 2008 9
3 A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] 5 2 4 3 6 8 7 1 #include <stdio. h> int A[8]={5, 2, 4, 3, 6, 8, 7, 1}; 11 int func 2(int n){ 13 int s, i; 14 s = -1; // 找不到n 15 i = 0; 16 while (i<8 && s==-1){ 18 if(n==A[i]) 19 s=i; 20 i++; 21 } 22 return s; 23 } 10/30/2020 (c) 3 func 2(4) = _____次 6 func 2(8) = _____次 若要句 18執行次數最少, 5 ) func 2( _____ CIT 2008 10
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] 1 2 4 3 6 8 7 1 11 int func 1(int n){ 13 int s, i; 14 s = -1; // 找不到n 15 for (i=0; i<8; i++) 16 if (n==A[i]) 17 s=i; 18 return s; 19 } 10/30/2020 11 int func 2(int n){ 13 int s, i; 14 s = -1; // 找不到n 15 i = 0; 16 while (i<8 && s==-1){ 18 if(n==A[i]) 19 s=i; 20 i++; 21 } 22 return s; 23 } (d) 假設 A[0]=1; 7 func 1(1) = _____ 0 func 2(1) = _____ 最後出現的數字 1 最先出現的數字 1 CIT 2008 11
12 -17 char st[]="True friend scolds like a DAD cares like a MOM. "; a 1 a 2 temp[]="scolds"; void Check. Word (int a 1, int a 2){ char temp[50]; int i; for (i=a 1; i<=a 2; i++) (b) temp[i-a 1] = st[i]; ________ int checkdict(char wd[]){ } temp[a 2 -a 1+1]='