Microsoft Word - C-pgm-ws2010.doc

Similar documents
C/C++ 语言 - 循环

C/C++语言 - C/C++数据

C/C++ - 文件IO

C C

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

untitled

C/C++语言 - 分支结构

C/C++ - 数组与指针

CC213

C++ 程式設計

C

C/C++程序设计 - 字符串与格式化输入/输出

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

Microsoft PowerPoint - ds-1.ppt [兼容模式]

3.1 num = 3 ch = 'C' 2

CC213

untitled

Microsoft PowerPoint - 20-string-s.pptx

第5章修改稿


zt

, 7, Windows,,,, : ,,,, ;,, ( CIP) /,,. : ;, ( 21 ) ISBN : -. TP CIP ( 2005) 1


2006..,1..,2.,.,2..,3..,3 22..,4..,4 :..,5..,5 :..,5..,6..,6..,8..,10 :..,12..,1..,6..,6.., ,5,:..,1 :..,1 :..,1 :..,2..,2..,3 :..,1 :..,1..,1.

Microsoft Word - 新1.doc

untitled

2009年挑战乔戈里

ENGG1410-F Tutorial 6

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

Microsoft Word - 09.數學 docx

nooog

C 1

2011-论文选集-2.cdr

( CIP) /. :, ( ) ISBN TP CIP ( 2005) : : : : * : : 174 ( A ) : : ( 023) : ( 023)

2010年江西公务员考试行测真题

ttian

! "#$%& $()*+#$, $(-.&,./.+#/(-.&01( &-#&(&$# (&2*(,#-3.,14& $ +()5(*-#5(-#/-/#(-1#&-+)(& :;<<= > A B?

3. 給 定 一 整 數 陣 列 a[0] a[1] a[99] 且 a[k]=3k+1, 以 value=100 呼 叫 以 下 兩 函 式, 假 設 函 式 f1 及 f2 之 while 迴 圈 主 體 分 別 執 行 n1 與 n2 次 (i.e, 計 算 if 敘 述 執 行 次 數, 不

untitled

新・明解C言語入門編『索引』

中華民國青溪協會第四屆第三次理監事聯席會議資料

# # # # # # = #, / / / / # 4 # # # /# 02-1 / 0 /? / 0 / 0? # # / >

优合会计考点直击卷子之财经法规答案——第八套

untitled



1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

KillTest 质量更高 服务更好 学习资料 半年免费更新服务

zyk00168ZW.PDF

重 要 声 明 长 城 证 券 股 份 有 限 公 司 编 制 本 报 告 的 内 容 及 信 息 来 源 于 陕 西 东 岭 工 贸 集 团 股 份 有 限 公 司 提 供 的 证 明 文 件 以 及 第 三 方 中 介 机 构 出 具 的 专 业 意 见 长 城 证 券 对 报 告 中 所 包

Python a p p l e b e a r c Fruit Animal a p p l e b e a r c 2-2

Microsoft PowerPoint - STU_EC_Ch04.ppt

e bug 0 x=0 y=5/x 0 Return 4 2

Generated by Unregistered Batch DOC TO PDF Converter , please register! 浙江大学 C 程序设计及实验 试题卷 学年春季学期考试时间 : 2003 年 6 月 20 日上午 8:3

4. 债 务 人 明 确 表 示 撞 行 拖 欠 的 债 务, 这 在 法 律 上 将 引 起 ( ) 人. 诉 讼 时 效 的 中 止 日. 诉 讼 时 效 的 中 黯 C. 诉 讼 时 效 的 延 长 D. 法 定 诉 讼 时 敷 黯 爵 的 改 变 5. 职 工 代 表 大 会 是 国 有 企



第3章.doc

VHDL(Statements) (Sequential Statement) (Concurrent Statement) VHDL (Architecture)VHDL (PROCESS)(Sub-program) 2

2015年计算机二级(C语言)模拟试题及答案(四)

WWW PHP

Microsoft Word - 财务d08z.doc

Ps22Pdf

Important Notice SUNPLUS TECHNOLOGY CO. reserves the right to change this documentation without prior notice. Information provided by SUNPLUS TECHNOLO

FY.DOC

CC213

TC35短信发送程序设计

<313034A4BDB67DA4C0B56FBA5DB3E65FBD64A5BB2E786C7378>

epub 33-8

51 C 51 isp 10 C PCB C C C C KEIL

<4D F736F F D C4EAC8EBD1A74D4241C1AABFBCD7DBBACFB2CEBFBCB4F0B0B8BCB0CFEABDE22E646F6378>

3 (s05q6) The diagram shows the velocity-time graph for a lift moving between floors in a building. The graph consists of straight line segments. In t

山东2014第四季新教材《会计基础》冲刺卷第三套


《米开朗琪罗传》


Microsoft Word - CPE考生使用手冊 docx

Historical Fund Prices_TC_mt_2017.pdf

( ) Wuhan University

zt


数量关系部分题目溯源:

就 构 成 了 盗 窃 罪 与 破 坏 交 通 设 施 罪 的 想 象 竞 合, 按 照 其 中 处 罚 较 重 的 犯 罪 处 罚 5. 答 案 :B 本 题 主 要 考 察 如 何 区 分 收 买 被 拐 卖 的 妇 女 儿 童 罪 与 拐 卖 妇 女 儿 童 罪 的 共 犯 问 题 ( 对 向

标题

逢甲大學實習工場

扉页

<4D F736F F D20A5C1B6A1B3E0C2A7B2DFAB55A4B6B2D02E646F63>

6 C51 ANSI C Turbo C C51 Turbo C C51 C51 C51 C51 C51 C51 C51 C51 C C C51 C51 ANSI C MCS-51 C51 ANSI C C C51 bit Byte bit sbit

科学计算的语言-FORTRAN95

试卷

z x / +/- < >< >< >< >< > 3 b10x b10x 0~9,a~f,A~F, 0~9,a~f,A~F, x,x,z,z,?,_ x,x,z,z,?,_ h H 0~9,_ 0~9,_ d D 0~7,x,X,z,Z

考 查 知 识 点 肝 气 疏 泄 调 畅 气 机 的 作 用, 主 要 表 现 在 以 下 几 个 方 面 :(1) 促 进 血 液 与 津 液 的 运 行 输 布 ;(2) 促 进 脾 胃 的 运 化 功 能 和 胆 汁 分 泌 排 泄 ;(3) 调 畅 情 志 ;(4) 促 进 男 子 排 精

Microsoft Word - template.doc

第三节 软件测试的过程与策略

untitled

PowerPoint Presentation

保母人員丙級應檢資料第二部份 doc

ebook39-5

zt

_汪_文前新ok[3.1].doc

Transcription:

Information and Communication Technology 資訊與通訊科技 Loops (while/for) C 廻路 姓名 : 班別 : ( ) CS

C Programming #1 Functions 函數 : 1 若 n=14, 求以下表示式的值 Expressions 表示式 Value 值 Expressions 表示式 Value 值 A 20 2 * (n / 2) K ceil (n / 5.0) B 5 n % 4 L floor (n / 5.0) C n % 10 + 1 M round (n / 5.0) * 5 D (n+1) % 5 N 'a' 'A' E n (n / 5) * 5 O sqrt (pow(-3,2)) F n % 5 P abs (10-100) G n / 5 Q abs ('@' '6') H n n % 5 R 'E' 'A' I n / 5.0 S (char) ( 'f' 'a' + 'A') J n / 100 T (int) floor(1234.9) /100 *100 Boolean expressions 布爾表示式 2. 求以下表示式的值 假如 int A=3, B=4, C=5; Boolean expressions 布爾表示式 ( 1=TRUE / 0=FALSE) A printf ("%i", (-6 > 5)); Result B C D E F G H I J K L M N O printf ("%i", ((A+6) > (C+4))); printf ("%i",!(a+b<0)); printf ("%i", (3>3) (C>0)); printf ("%i", (A!=10) && (C<=7)); printf ("%i", ('A'=='a')); if('a'=='a') printf("1"); else printf("0"); printf ("%i", ('A'=='a')?1:0); printf ("%i", strcmp("ab","ef")); printf ("%i", strcmp("abc","abcd")); printf ("%i", strcmp("lee","li")); printf ("%i", strcmp("lee","li")); printf ("%i", strcmp("lee","lee")); if(strcmp("the","the")) x=1; else x=0; printf ("%i", x); char gender='x'; if(gender=='m') puts(" 男 "); else puts(" 女 "); Szeto CY 2010 p.1

C Programming #2 Assignment statements 賦值句子 1. 若 a = 5, b = 3, c = 2, 求 a, b, c 的最終值? 執行以下 C 句子 a b c A c = a; a = b; b = c; B c = a; b = c; a = b; C a = b; b = a; c = a+b; D a = b*2; b = a*2; c = b*c; 2. 若 a = 4, b = 2, c = 1, 求 a, b, c 的最終值? 執行以下 C 句子 a b c A c = a+b; b = a+c; a = b+c; B c = a+b-c; b = a+b-c; a = a+b-c; C a = b; b = a; c = a+b; 3 執行以下 C 句子, 並寫下輸出 int a=10, b=20; A C 句子 printf ("A+B = A-B"); 輸出 B C printf ("A+B = %i-%i", a, b); printf ("A = %i B = %i", a+b, a-b); 4 寫出以下 x 值的可能範圍 C 句子 A x = rand(); B x = rand()%10; C x = rand()%10 +5; D x = rand()%5 +rand()%4; E x = rand()%5 -rand()%4; F x = abs(rand()%5 -rand()%4); x 值的可能範圍 Szeto CY 2010 p.2

C Programming #3 While-loops 1. 寫出下列程式所產生的輸出程式 Program statements A i=0; while (i<=5){ i=i+1; printf ("%i", i); B i=0; while (i<5){ i++; printf ("%i", i); C i=0; while (i<=5){ printf ("%i", i); ++i; D n=1; sum = 0; while (sum<100){ sum += n; n+=20; printf ("%i %i", n, sum); E n = 0; while (n<5) printf ("%i\t", n); printf (" #"); F n = 1; while (n<=5){ n = n+1; printf ("%2i", n); G n = 1; while (n<5) printf ("%2i", n++); 輸出 H n = 10; while (n<5){ n = n-1; printf ("%02i", n); printf (" #"); I x = 1; while (x<=10) x = x+1; printf ("%i\n", x); Szeto CY 2010 p.3

C Programming #4 While-loops 1. 寫出下列程式所產生的輸出 程式 Program statements A n = 10; while (n>5){ n = n-1; printf ("%2d", n); printf (" #"); B scanf ("%i", &n); // input = 1234 while (n>0){ printf ("%3i", n%10); n = n/10; printf ("#"); C scanf ("%f", &n); // input = 0.37 printf ("0."); c = 0; while (n>0 && c<4){ n = n*2; c = c+1; if(n>=1) printf ("1"); else printf ("0"); if(n>=1) n = n-1; D scanf ("%i", &x); // input = 1234 n = 0; y = x; d = 1; while (y>=10){ n++; y = y / 10; d = d * 10; printf ("%5d %8d %8d \n", n,y,d); E a = 50; b = 14; c = 999; while (c>0){ c = a%b; if(c>0){ a = b; b = c; printf ("The GCD is %i \n", b); 輸出 Szeto CY 2010 p.4

C Programming #5 While-loops 1. 寫出下列程式所產生的輸出 程式 A scanf ("%i", &n); // input = 9 c = 0; while (n>0){ if((n>7) ((n>0) && (n<4))) n = n-8; else n = n+10; printf ("%3i ", n) c++; printf ("%3i \n", c); B prin = 121000; // principal( 本金 ) pay = 12000; rate = 1.01; n = 0; while (prin > 0){ n++; prin = prin * rate pay; prin = floor (prin / 1000) * 1000; if(n>9) printf ("%3d %10.0f \n", n, prin); C scanf ("%i", &x); // input = 1924 n = 1; y = x; d = 1; while (y>=10){ n++; y = y / 10; d = d * 10; for(k=1; k<=n; k++){ printf ("%i \n", x/d); x = x % d; d = d / 10; 輸出 D E n =??? n = 15; n = 19; char S[20] = "123Chan45Tai67Man89"; printf ("#"); while (n<19){ n++; printf ("%c ", S[n]); n =??? n = 1; n = 10; char S[20] = "123Chan45Tai67Man89"; while (n<19 && S[n]<'a') n++; printf ("%i \n", n); Szeto CY 2010 p.5

C Programming #6 Iterations 廻路 (For-loops) 1 求以下 C 句子的輸出 C 句子 A for(k=0; k<5; k++); printf ("%d", k); 輸出 B C for(k=5; k<0; k++) printf ("%5d", k); printf (" #\n"); for(k=1; k<=4; k++) printf ("%5d", k); D a = 1; b = 0; for(k=1; k<6; k++){ c = a+b; a = b; b = c; printf ("%5d", c); printf (" #"); E y = 1; for(k=0; k<3; k++){ printf ("%5d", y); y = y*2; printf ("%i \n", y); F y = 1; for(k=-1; k<=1; k++) printf ("%5d", y); y = y*2; printf ("%i \n", y); G n = 10; for(k=1; k<=n; k++){ printf ("%5d", k); if(k%7==0) H d1 = 5; printf ("%*s", d1*5, " "); for(k=1; k<=10; k++){ printf ("%5d", k); if((d1+k)%7==0) Szeto CY 2010 p.6

C Programming #7 Iterations 廻路 (For-loops) 1. 求以下 C 句子的輸出 C 句子 A for(k=3; k<=7; k++) printf ("%3d", 9-abs(k-5)); 輸出 B ch = '@'; n = 5; for(k=0; k<n; k++) printf ("%2c", ch); printf ("\n#"); C for(ch='a'; ch<'d'; ch++) printf ("%i %4c \n", ch, ch); D n = 12.4; for(k=10; k<=floor(n); k++) printf ("%5c", (k+ 'A')); E x = 1; for(k=2; k>=-2; k--) x = x * k; printf ("%i \n", x); F sum = 0; for(k=1; k<6; k++) sum = pow(k,2); printf ("%i\n", sum); G sum = 0; for(k=1; k<6; k++) sum += pow(k,2); printf ("Sum = %i\n", sum); H for(k=-2; k<=2; k++) printf ("%5d", 10-k); I J K L for(k=1; k<=4; k++) printf ("%5d", k); for(k=65; k<=67; k++) printf ("%i %3c", k, k); for(k=1; k<4; k++) printf ("%i %*c \n", k,k,'#'); for(k=0; k<4; k++) printf ("%i", k*2+1); Szeto CY 2010 p.7

C Programming #8 Iterations 廻路 (For-loops) 求以下 C 句子的輸出 C 句子 A x = 0; for(k=-2; k<=2; k++) x = x + abs(k); printf ("%i \n", x); 輸出 B x = 1; for(k=2; k>=-2; k--) x = 2*(x-1) +k; printf ("%i \n", x); C x = 1; for(k=5; k>1; k--) x = 2*x +1; printf ("%i \n", x); D E F G for(k=0; k<3; k++){ printf ("O"); printf ("X\n"); for(k=0; k<3; k++){ printf ("O\n"); printf ("X"); for(k=0; k<3; k++) printf ("O"); printf ("X"); for(k=0; k>3; k--); printf ("O"); printf ("X"); H for(k=1; k<=3; k++) printf ("6 x %i = %i \n", k, 5*k); I for(k=1; k<=3; k++) printf ("%i \n", pow(2*k,2)); J K for(k=1; k<=5; k++) if(k%2==1) printf ("%i", k); else printf ("%i", -k); for(ch1='a'; ch1<='b'; ch1++){ for(ch2='a'; ch1<='b'; ch2++) printf ("%c %c \n", ch1,ch2); Szeto CY 2010 p.8

C Programming #9 For-loops 1. To find the sum of the first n consecutive integers (i.e. sum = 1 + 2 + 3 + + n) 求 n 個連續數之和 do{ Q: Enter a positive # n (>0) : 4 printf ("Q: Enter a positive # n (>0) : "); A: Sum = 1 + 2 + 3 + 4 = 10 scanf ("%i", &n); sum = Q: Enter a positive # n (>0) : 9 printf ("A: Sum = "); A: Sum = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 = 45 for(k=1; Q: Enter a positive # n (>0) : 0 sum = A: Bye! if(k<n) printf else printf printf ("%i\n\n", sum); while (n>0); printf ("A: Bye\n!"); 2. To find the sum of the first n odd integers (i.e. sum = 1 + 3 + 5 + + 2n-1) 求 n 個連續單數之和 do{ Q: Enter a +ve integer n (>0) : 6 printf ("Q: Enter a +ve integer n (>0) : "); A: Sum = 1 + 3 + 5 + 7 + 9 + 11 = 36 scanf ("%i", &n); sum = Q: Enter a +ve integer n (>0) : 7 printf ("A: Sum = "); A: Sum = 1 + 3 + 5 + 7 + 9 + 11 + 13 = 49 for(k=1; Q: Enter a +ve integer n (>0) : 0 sum = A: Bye! if(k<n) printf else printf printf ("%i\n\n", sum); while (n>0); printf ("A: Bye!\n"); 3. To calculate the factorial (n!) of n, where n! = n (n-1) 3 2 1. 求 n 個連續數之積 do{ Q: Enter a pos integer n (>0) : 10 printf ("Q: Enter a pos integer n (>0) : "); A: 10! = 10 9 8 7 6 5 4 3 2 1 = 3628800 scanf ("%i", &n); factorial = Q: Enter a pos integer n (>0) : 5 printf ("A: %i! = ", n); A: 5! = 5 4 3 2 1 = 120 for(k= factorial = printf printf (" 1 = %.0f \n", while (n>0); Szeto CY 2010 p.9

C Programming #10 Iterations (Nested For-loops) A sum = 0; for(i=1; i<=4; i++) for(j=1; j<=3; j++) sum = sum + i*j; printf ("%i", sum); B C for(i=1; i<=2; i++) for(j=2; j<=3; j++) printf ("%i - %i \n", i, j); for(i=1; i<=2; i++) for(j=3; j>i; j--) printf ("%i + %i \n", i, j); D for(i=1; i<=3; i++){ printf ("%c", i+64); for(j=1; j<=3; j++) printf ("%3d", j); E char A[] = "1234"; sum = 0; for(n=2; n<= strlen(a); n++) sum = sum + A[n] * (5-n); printf ("%i\n", sum); 已知 : int score[40][3]; 假設輸入是 : 1,2,3,, 120 F for(st=1; st<=40; st++){ for(subj=1; subj<=3; subj++) scanf ( "%i", &score [st][subj]); printf ("%i", score [2][1]); G for(subj=1; subj<=3; subj++){ for(st=1; st<=40; st++) scanf ( "%i", &score [st][subj]); printf ("%i", score [2][1]); H for(st=1; st<=40; st++){ for(subj=1; subj<=3; subj++) scanf ( "%i", &score [subj][st]); printf ("%i", score [2][1]); Szeto CY 2010 p.10

C Programming #11 For-loops 1. To display the alphabets (A to Z) on the screen. 顯示字母 printf ("Q: From alphabet (A-Z) : "); scanf ("%c", &frch); Q: From alphabet (A-Z) : C printf (" To alphabet (A-Z) : "); scanf ("%c", &toch); To alphabet (A-Z) : K printf ("A: "); A: CDEFGHIJK for (ch= printf ("%c", ch); 2. To display the alphabets (A to Z) on the screen. 顯示字母 printf ("Q:From alphabet (1-26) : "); scanf ("%i", &x); Q: From alphabet (1-26) : 3 printf (" To alphabet (1-26) : "); scanf ("%i", &y); To alphabet (1-26) : 11 printf ("A: "); A: CDEFGHIJK for (n= printf ("%c", 3. To display a triangle of stars (asterisks) on the screen with its vertex pointing upward. for(x=1; printf ( "* "); for(x=5; printf ( "* "); * * * * * * * * * * * * * * * 4. To display a triangle of stars (asterisks) on the screen with its vertex pointing downward. for(x=5; printf ("* "); for(x=1; printf ("* "); * * * * * * * * * * * * * * * 5. To display a triangle of stars (asterisks) on the screen with its vertex pointing downward. for(x=5; printf ("%*s", printf ( "* "); * * * * * * * * * * * * * * * * * * * * * * * * * Szeto CY 2010 p.11

C Programming #12 For-loops 1. To display the alphabets (A to Z) on the screen. 顯示字母 printf ("Q: From alphabet (A-Z) : "); scanf ("%c", &frch); Q: From alphabet (A-Z) : C printf (" To alphabet (A-Z) : "); scanf ("%c", &toch); To alphabet (A-Z) : K printf ("A: "); A: CDEFGHIJK if for(ch= printf ("%c", ch); Q: From alphabet (A-Z) : K else To alphabet (A-Z) : C for(ch= printf ("%c", ch); A: CDEFGHIJK 2. To display a triangle of stars (asterisks) on the screen with its vertex pointing upward. 輸出 for(x=1; x<=5; x++){ printf ("%*s", (5-x)*2, ""); printf ("* "); for(x=5; x>=1; x--){ printf ("%*s", printf ("* "); * * * * * * * * * * * * * * * 3. To display a triangle of stars (asterisks) on the screen with its vertex pointing downward. 輸出 for(x=5; printf ("%*s", printf ("* "); for(x=1; printf ("%*s", printf ("* "); * * * * * * * * * * * * * * * 4. To display a triangle of stars (asterisks) on the screen with its vertex pointing downward. 輸出 for(x=1; printf ("%*s", printf ("* "); * * * * * * * * * * * * * * * * * * * * * * * * * 5. To accept user input via keyboard until valid. do{ printf ("Q: Enter choice (A-D,Q)? "); scanf opt = 輸出 Q: Enter choice (A-D,Q)? Z A: Invalid input!... Q: Enter choice (A-D,Q)? A A: OK! valid = ((opt=='q') if(!valid) printf ("A: Invalid input!\n"); while (!valid); Szeto CY 2010 p.12

C Programming #13 For-loops + Array 1 What is the output after the execution of the following program segments? 有什麼輸出 A for(k=1; k<=5; k++){ A[k] = k * 2-1; printf ("%i", A[k]); B for(k=1; k<=5; k++){ A[k] = (k*5) % 3 + 1; printf ("%3d", A[k]); if(k%3==0) C A[1] = 5; A[2] = 1; A[3] = 4; A[4] = 3; A[5] = 2; for(k=1; k<=5; k++) if(a[k]>a[6-k]) printf ("%3d", A[k]); D for(x=0; x<=3; x++) y<=4; y++) A[4*x+y] = x + y*2; printf ("%i", A[11]); E for(n=1; n<=10; n++) A[n] = 10-n; printf ("%i", A[4]); F for(n=1; n<=10; n++) A[n] = n; for(n=1; n<=5; n++) A[n] = A[11-n]; printf ("%i", A[4]); G for(n=1; n<=5; n++) A[n] = n; for(n=1; n<=4; n++) A[n] = A[n+1]; for(n=1; n<=5; n++) printf ("%i", A[n]); H for(n=1; n<=5; n++) A[n] = n; for(n=2; n<=5; n++) A[n] = A[n-1]; for(n=1; n<=5; n++) printf ("%i", A[n]); I for(n=1; n<=5; n++) A[n] = n; for(n=4; n>=1; n--) A[n+1] = A[n]; for(n=1; n<=5; n++) printf ("%i", A[n]); J for(n=1; n<=5; n++) A[n] = n; for(n=4; n>=1; n--) A[n] = A[n+1]; for(n=1; n<=5; n++) printf ("%i", A[n]); Szeto CY 2010 p.13

C Programming #14 For-loops + Array Write program segments to perform the same tasks as shown on the right. 改寫句子 int A[10]; 1. To find the sum of all elements of the array A[]. 陣列 A[] 所有元素的總和 sum = 0; sum = A[1] + A[2] + + A[9]; for(n=1; n< sum = 2. To find the product of all elements of the array A[ ]. 陣列 A[] 所有元素的積 product = product = A[1] * A[2] * * A[9]; for(n=1; n< product = 3. To accept inputs from user via keyboard and put the input values into the element of the array A[ ]. for(n=1; n< scanf ("%i", scanf ("%i \n", A[1]); scanf ("%i \n", A[9]); 4. To output the value stored in each element of the array A[ ]. for(n=1; n< printf ("%i \n", printf ("%i \n", A[1]); printf ("%i \n", A[9]); 5. To reverse 倒轉 the values of the elements in the array A[ ]. (i.e. swap the values of A[1] and A[9], ) for(n=1; n< A[1] A[9] 對換 temp = A[n] = A A[2] A[8] A[3] A[7] A[4] A[6] 6. To shift up/down the values of the elements in array A[ ]. for(n=1; n< A[n] = for(n= A[n] = A[1] = A[2]; { shift up 上移 A[2] = A[3]; A[8] = A[9] A[5] = A[4]; { shift down 下移 7. To rotate up/down the values of the elements in array A[ ]. temp = for(n=1; n<=8; n++) A[n] = A[9] = temp = for(n= A[1] = A[n] = A[1] = A[2]; { rotate up A[2] = A[3]; A[9] = A[1]; A[5] = A[4]; { rotate down A[1] = A[9]; Szeto CY 2010 p.14

C Programming #15 Sub-programs 1. 寫出下列程式所產生的輸出程式碼 Program listing #include <stdio.h> int A, B, C; void p1(){ C = A+B; void p2 (int X, int Y){ float C = X+Y; void p3 (int X, int Y){ C = X+Y; X = Y = 0; void p4 (int *X, int *Y){ if(*x==*y) *X+=*Y; if(*x<*y) *X=*Y; else *Y=*X; void p5 (int *X, int *Y){ if(*x==*y) *X+=*Y; else if(*x<*y) *X=*Y; else *Y=*X; void p6 (int *X, int *Y, int *Z){ int T=*X; *X=*Y; *Y=*Z; *Z=T; main(){... 程式碼 Program statements a A=1; B=3; C=5; p1(); printf ("%i %3i %3i \n", A, B, C); 輸出 b A=1; B=3; C=5; p2(a,b); printf ("%i %3i %3i \n", A, B, C); c A=1; B=3; C=5; p3(a,b); printf ("%i %3i %3i \n", A,B,C); p3(b,c); printf ("%i %3i %3i \n", A,B,C); d A=1; B=3; C=5; p4(a,b); printf ("%i %3i %3i \n", A,B,C); p4(c,b); printf ("%i %3i %3i \n", A,B,C); e A=1; B=3; C=5; p5(b,a); printf ("%i %3i %3i \n", A,B,C); p5(a,b); printf ("%i %3i %3i \n", A,B,C); f A=1; B=3; C=5; p6(a,b,c); printf ("%i %3i %3i \n", A,B,C); p6(c,b,a); printf ("%i %3i %3i \n", A,B,C); g A=1; B=3; C=5; p6(b,c,a); printf ("%i %3i %3i \n", A, B,C); p6(a,b,c); printf ("%i %3i %3i \n", A, B,); Szeto CY 2010 p.15