Presentation is loading. Please wait.

Presentation is loading. Please wait.

Q1 以下是10個初生嬰兒,首6個月的體重改變紀錄

Similar presentations


Presentation on theme: "Q1 以下是10個初生嬰兒,首6個月的體重改變紀錄"— Presentation transcript:

1 Q1 以下是10個初生嬰兒,首6個月的體重改變紀錄
第10個baby Baby# Month & wt W1[] W2[] W10[] 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月 float w1[7]={2.4,3.2,4.1,5.0,6.5,6.4,6.3}; float w2[7]={2.6,3.6,4.5,5.4,5.9,6.4,6.7}; kg 2019年10月14日 CIT 2008

2 float w1[7]={2.4,3.2,4.1,5.0,6.5,6.4,6.3}; float w2[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 + w1[i]; ave = s / 7; } printf("平均體重 = %.1f\n", ave); // 修正後 float s, ave; // sum, average s = 0; for (i=0;i<=6;i++){ s = s + w1[i]; } ave = s / 7; printf("平均體重 = %.1f\n", ave); 0.9 4.8 ave的值: ______ ave的值: ______ 2019年10月14日 CIT 2008

3 // weight difference 體重相差 float WD (int bno, int mnth){
#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] 提示: 先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 1 2 3 4 5 6月 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.7 ... 9 2.7 4.6 6.6 7.3 7.7 2019年10月14日 CIT 2008

4 int healthy,i,j; // i=baby, j=month
#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}; void checkbaby(){ int healthy,i,j; // i=baby, j=month C[0][6] for (i=0;i<MAX;i++){ healthy = 1; for (j=1;j<=6;j++) // 6 months if (WD(i,j)<=0) healthy = 0; 體重相差<0 出現負增長 if (healthy==1) printf(“Baby %d Healthy健康\n", i); else printf("Baby %d Unhealthy\n", i); } 2019年10月14日 CIT 2008

5 2a A[ ] p 1 P 2 T 3 R 4 S 5 Q 50 (i) 陣列 5 × 50 = 250 bytes (ii) 鏈表
記憶體需求量多少? (i) 陣列 5 × 50 = 250 bytes 鏈表 (ii) 鏈表 4 + 5×(50+4) = 274 bytes head P T R S Q nil 資料A[] : 50 bytes 指標Pointer : 4 bytes 2019年10月14日 CIT 2008

6 (b) (i) 以鏈表(linked-list)實現隊列 (queue)結構,
完成執行『添加』(append)指令會花很多功夫。為什麼? (i) 增添新記錄時,若新增位置在表末,必須經過每一元素才能到達表末,進行新增。 head P T R S Q nil X nil (ii) 以鏈表(linked-list)實現隊列 (queue)結構, 完成執行『移除』(remove)指令會比較簡單。為什麼? (ii) 移除首元素只涉及讀取鏈表的第一個元素 2019年10月14日 CIT 2008

7 2c A[ ] 1 P 2 T 3 R 4 S 5 Q A[ ] 1 2 3 4 S 5 Q F 1 Size 5 F 4 Size 2
First Front 2c A[ ] 1 P 2 T 3 R 4 S 5 Q A[ ] 1 2 3 4 S 5 Q F 1 Size 5 F 4 Size 2 A[ ] 1 U 2 3 4 S 5 Q F 4 Size 3 2019年10月14日 CIT 2008

8 刪除數據元素 remove a data item, 如果 __________
便顯示 “The queue is empty.” 及停止執行 刪除Remove A[F] F = __________ 如果F = __________ 便 __________ SIZE = ___________ SIZE = 0 F + 1 6 F = 1 SIZE – 1 新增數據元素 append a data item U, 如果 __________ 便顯示 “The queue is full.” 及停止執行 tempindex = __________ 如果 tempindex __________ 便 ____________________ A[tempindex]  U SIZE = __________ SIZE = 5 F + SIZE TEMPINDEX = TEMPINDEX – 5 > 5 SIZE + 1 2019年10月14日 CIT 2008

9 (a) func1(6) = ______ func1(9) = ______ 4 行16執行了至少______次 -1
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 func1(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 } (a) func1(6) = ______ func1(9) = ______ 行16執行了至少______次 (b) 返回值代表什麼? 4 -1 8 數字n 在陣列A[ ]中的位置 2019年10月14日 CIT 2008

10 (c) func2(4) = _____次 func2(8) = _____次 3 若要句18執行次數最少, 6
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}; (c) func2(4) = _____次 func2(8) = _____次 若要句18執行次數最少, func2( _____ ) 3 11 int func2(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 } 6 5 2019年10月14日 CIT 2008

11 func1(1) = _____ func2(1) = _____ 7
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7] 1 2 4 3 6 8 7 11 int func1(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 } 11 int func2(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; func1(1) = _____ func2(1) = _____ 7 最後出現的數字1 最先出現的數字1 2019年10月14日 CIT 2008

12 將儲存在字符陣列st[]內的英文句子中, 找出所有不正確invalid英文字。 句子內只有英文字母、空格字符space 及一個句號。
4 將儲存在字符陣列st[]內的英文句子中, 找出所有不正確invalid英文字。 句子內只有英文字母、空格字符space 及一個句號。 句子起首為英文字母,而句號之後的字符無需處理。例如: char st[ ]="True friend scolds like a DAD cares like a MOM."; 2019年10月14日 CIT 2008

13 void CapLet (char st[ ]) { int i;
文字 (a) 把句子st[ ]轉大楷字母 void CapLet (char st[ ]) { int i; } i = 0; while (st[i] != '.') { if _______________________ st[i] = st[i]-32; i++; } 若是小楷字母 ((st[i]>='a') && (st[i]<='z')) 轉大楷字母 2019年10月14日 CIT 2008

14 void CheckWord (int a1, int a2){ char temp[50]; int i;
12-17 char st[]="True friend scolds like a DAD cares like a MOM."; a1 a2 temp[]="scolds"; void CheckWord (int a1, int a2){ char temp[50]; int i; for (i=a1;i<=a2;i++) _______________ temp[a2-a1+1]='\0'; if _______________ printf("%s\n", temp); } (b) int checkdict(char wd[]){ int ok=0; 檢查文字wd 是否在字典上找到 return ok; } temp[i-a1] = st[i]; (! (checkdict(temp)) ) 2019年10月14日 CIT 2008

15 void ListIW(){ // 列出所有錯字 list invalid words int a1, a2, i, ct=0;
CapLet(st); // 轉大楷 puts(st); a1 = _____; i = -1; do{ i++; }while(_____________); printf("No.of words = %i\n", ct); } a1 a2 st[i] 非字母(字尾) || if (st[i]<'A' ____ st[i]>'Z'){ a2 = _____; CheckWord(a1,a2); ct++; a1 = _____; } i-1 i+1 字數 st[i]!='.' 2019年10月14日 CIT 2008


Download ppt "Q1 以下是10個初生嬰兒,首6個月的體重改變紀錄"

Similar presentations


Ads by Google