Array int days days printf i days array

  • Slides: 15
Download presentation

Array 陣列 宣告句子 int days ; 賦值句子 days 輸出句子 printf ("%i", days 輸入句子 array

Array 陣列 宣告句子 int days ; 賦值句子 days 輸出句子 printf ("%i", days 輸入句子 array 陣列 days[ = 28; scanf ("%i", &days ); 0] 1] 2] 28 3] days[ 4] days[ 5] days[ 6] days[ 7] days[ 8] days[ 9] days[10] days[11] days[12] 2

for (i=1; days ; i++) = 31; days[ 1] days[ 2] days[ 3] days[

for (i=1; days ; i++) = 31; days[ 1] days[ 2] days[ 3] days[ 4] days[ 5] days[ 6]. . . days[10] days[11] days[12] = = = [i] array 陣列 31; 31; 31; = 31; days[ 0] 1] 31 2] 31 3] 31 days[ 4] 31 days[ 5] 31 days[ 6] 31 days[ 7] 31 days[ 8] 31 days[ 9] 31 days[10] 31 days[11] 31 days[12] 31 3

for (i=1; i<=12; i++) printf("%i", ); printf("%i", days[ 1]); printf("%i", days[ 2]); printf("%i", days[

for (i=1; i<=12; i++) printf("%i", ); printf("%i", days[ 1]); printf("%i", days[ 2]); printf("%i", days[ 3]); printf("%i", days[ 4]); printf("%i", days[ 5]); printf("%i", days[ 6]); . . . printf("%i", days[10]); printf("%i", days[11]); printf("%i", days[12]); [i] array 陣列 days[ 0] 1] 31 2] 28 3] 31 days[ 4] 30 days[ 5] 31 days[ 6] 30 days[ 7] 31 days[ 8] 31 days[ 9] 30 days[10] 31 days[11] 30 days[12] 31 4

for (i=1; i<=12; i++) scanf("%i", scanf("%i", . . . scanf("%i", array 陣列 &days[ &days[

for (i=1; i<=12; i++) scanf("%i", scanf("%i", . . . scanf("%i", array 陣列 &days[ &days[ ); 1]); 2]); 3]); 4]); 5]); 6]); [i] &days[10]); &days[11]); &days[12]); days[ 0] 1] 31 2] 28 3] 31 days[ 4] 30 days[ 5] 31 days[ 6] 30 days[ 7] 31 days[ 8] 31 days[ 9] 30 days[10] 31 days[11] 30 days[12] 31 5

a[0] a[1] a[999] for (i=0; i<1000; i++) a[i]=i; for (i=0; i<1000; i++) a[i]=1000 -i;

a[0] a[1] a[999] for (i=0; i<1000; i++) a[i]=i; for (i=0; i<1000; i++) a[i]=1000 -i; for (i=0; i<1000; i++) a[i]=2*i+1; array 陣列 6

int score[40]; char grade[40]; grade[0] = ; grade[1] = ; // 40個分數 // 40個等級

int score[40]; char grade[40]; grade[0] = ; grade[1] = ; // 40個分數 // 40個等級 for (i=0; ; i++){ if (score___>=50) grade___='P'; else grade___ = 'F'; } score[i] grade[i] 0 33 D 1 44 C 2 55 B 3 66 B 38 77 B 39 88 A … for (i=0; i<40; i++){ if (score[i]<=20) grade[i]='E'; else if (score[i]<=40) grade[i]='D'; else if (score[i]<=60) grade[i]='C'; else if (score[i]<=80) grade[i]='B'; else _______; } array 陣列 7

輸出畫面: Q: 輸入整數 (1 -999): 34 A: thirty four units[i] tens[i] 0 1 one

輸出畫面: Q: 輸入整數 (1 -999): 34 A: thirty four units[i] tens[i] 0 1 one 算法 algorithm: 1. 準備兩個陣列 units[] 、 tens[] 2. 使用者輸入整數 (n) 2 two twenty 3 three thirty 4 four forty 3. 若 n<20 則輸出文字 (______[n]) 4. 否則 (利用__及__) 把 n 分柝為 個位 unit digit 及十位 tens digit 輸出適當文字 5 five fifty 6 sixty 7 seventy 8 eighty 9 literals文字: 01 -10 one two three four five six seven eight nine ten … 11 -15 eleven twelve thirteen fourteen fifteen 16 -19 sixteen seventeen eighteen nineteen 18 20 -90 twenty thirty forty fifty sixty seventy eighty ninety 19 ninety array 陣列 eighteen nineteen 8

1. sum of first n integers 首n個整數的和 1+2+3… int i, sum=0; for (i=1; i<=10;

1. sum of first n integers 首n個整數的和 1+2+3… int i, sum=0; for (i=1; i<=10; i++) sum = _______; 2. factorial of n: 首n個整數的積 1 2 3… int i, factorial=1; for (i=1; i<=10; i++) factorial =______; 3. alphabets: 字母 char ch; for (ch=___; ch<=___; ch++) printf ("%d %cn", ch, ch); array 陣列 65 A 66 B … 90 Z 97 a 98 b … 122 z 9

3. shift a[0] = a[1] = a[2] = left 左移/上移 a[__]; 0 1 2

3. shift a[0] = a[1] = a[2] = left 左移/上移 a[__]; 0 1 2 3 4 5 6 7 8 9 10 for (i=0; i<9; i++) a[___] = a[___]; 4. shift a[1] = a[2] = a[3] = right 右移/下移 a[__]; for (i=__; i___) a[___] = a[___]; 0 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 當i=9, a[9]=a[__]; 當i=8, a[8]=a[__]; array 陣列 10 當i=1,

5. swapping x, y 互換 int x=100, y=50, t; 6. swapping a[0], a[9] 互換

5. swapping x, y 互換 int x=100, y=50, t; 6. swapping a[0], a[9] 互換 int a[10], t; t = __; x = __; y = __; 0 1 t = a[__]; a[__] = t; 2 3 4 7. reverse 倒轉(數值) for (i=0; i<__; i++){ t = a[i]; a[i] = a[___]; a[___] = t; } array 陣列 5 6 7 8 9 8. reverse 反轉/倒轉(列印) for (i=9; i____; i--) printf ("%dn", a[i]); for (i=0; i<10; i++) printf ("%dn", a[___]); 11

9. 陣列的應用 (a) char animals[__][3]={"猴", "雞", "狗", "豬", "鼠", "牛", "虎", "兔", "龍", "蛇",

9. 陣列的應用 (a) char animals[__][3]={"猴", "雞", "狗", "豬", "鼠", "牛", "虎", "兔", "龍", "蛇", "馬", "羊"}; yy=1980; printf ("%sn", animals[yy______]); (b) char monthname[__][4] ={"? ", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; mm=9; printf ("%sn", monthname[____]); (c) char cdow[___][4] ={"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; array 陣列 12

(d) char cnum[___] ={"○", "一", "二", "三", "四", "五", "六", "七", "八", "九"}; n=24;

(d) char cnum[___] ={"○", "一", "二", "三", "四", "五", "六", "七", "八", "九"}; n=24; x= y= printf ("%s十%sn", cnum[__]); (e) int maxday[13] ={0, 31, 28, 31, 30, 31}; do{ printf ("Enter mm, dd "); scanf ("%i%i", &mm, &dd); }while (mm<1 || mm>12 || dd<1 || dd>31); array 陣列 13

統計(0 -9) int i, n, count[10]={0}; for (i=0; i<10; i++) count[__]=0; do{ scanf("%i", &n);

統計(0 -9) int i, n, count[10]={0}; for (i=0; i<10; i++) count[__]=0; do{ scanf("%i", &n); if (n>=0 && _____) count[___]++; }while(1); i count [i] 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 for (i=0; i<10; i++) printf("%i : %i n", ___, count[__]); array 陣列 15