Course: Computer Programming Exam. #1 Date:
Instructor: 趙坤茂 (台大資訊工程系) TA: 黃耀廷 莊竣捷
請務必依題號在答案卷作答,共兩面,記得填上系級、學號及姓名
考試時間:100分鐘
1. (10分) 請使用三個scanf函式分別讀入 (a) 一個字元(char);(b) 一個整數(int); (c) 一個雙倍精準浮點數(double)。請寫出兩個printf函式分別印出 (d) 一個雙倍精準浮點數(double);(e) 一個單倍精準浮點數 (float) 但只列印至小數點以下兩位數。
(a) scanf(“%c”);
(b) scanf(“%d”);
(c) scanf(“%lf”);
(d) printf(“%f”);
(e) printf(“%.2f”);
2. (8分) (a) C 語言提供一些複合運算元來縮短程式碼,請解釋total += number所代表的意義 (2分)。 (b)
撰寫程式時我們很少用一個主函式(main function)寫完所有功能,而通常將功能分別寫在不同子函式(sub-function)裡面,請問原因為何 (3分)? (c)
假設n1和d1為二個int型態之變數,而frac為float型態,考慮以下程式碼:frac = n1 / d1; 若n1 = 2 且 d1 = 4,則frac值會為0。請使用C語言所提供之資料型態轉換技巧
(explicit type conversion),改寫上述程式碼,使其結果較為精確。(3分)
(a) total = total + number;
(b) 程式碼重複使用;程式比較好維護;避免主程式過於冗長;可以專注於撰寫一個函式
(d) frac = (double) n1 / (double)
d1;
3. (10分) 請寫出下面這段程式印出的counter1,counter2和counter3的值分別為何?
#include
<stdio.h>
void main(void)
{
int i;
int counter1 = 0,
counter2 = 0, counter3 = 0;
while(counter1
< 50){
for(i=0;
i<100; i++){
++counter2;
}
counter3
= counter1++;
}
printf("counter1=%d\n",
counter1);
printf("counter2=%d\n",
counter2);
printf("counter3=%d\n",
counter3);
}
counter1=50
counter2=5000
counter3=49
4. (10分) 請解釋下面這段程式碼的功能,印出來的x值和讀進來的n值有何關係?
#include
<stdio.h>
void main(void)
{
int n;
int i, x;
printf("Input
the number>");
scanf("%d",
&n);
x = 1;
i = 1;
while (i <=
n){
x
= x * i;
i
= i+1;}
printf("\nx = %d\n", x);
}
x是1*2*3*…*n的值, 也就是n!
5. (10分) 請寫出下面這段程式的列印結果
#include
<stdio.h>
#define
START_CHAR 'a'
#define END_CHAR 'z'
void main(void)
{
int
char_code; /* numeric code of each character printed */
for (char_code =
(int)START_CHAR;
char_code <= (int)END_CHAR;
char_code = char_code + 1)
printf("%c",
(char)char_code);
printf("\n");
}
abcdefghijklmnopqrstuvwxyz
6. (5分) 假設有一函式(compare)輸入參數 (input
arguments)為二個整數(int),此函式會比較兩個輸入整數之大小,並回傳一整數
(0、1、或2分別代表大於、小於、或等於)作為比較結果。此函式使用範例如下:呼叫compare(3, 2) 回傳0,呼叫compare(1, 2) 回傳1,呼叫compare(1, 1)回傳2。請寫出此函式(compare)之完整程式碼 (須包含function prototype)。
int
compare(int a, int b){
if(a > b)
return
0;
else if(a < b)
return
1;
else
return
2;
}
7. (10分) 第二個作業要求計算在地表下某一深度(depth)所對應之華式(Celsius)與攝氏(Fahrenheit)溫度。給定以下兩個公式:
(1) Celsius =
10*(depth)+20;
(2) Fahrenheit =
Celsius*1.8 + 32。
請寫出兩個子函式的完整程式碼,分別計算以上兩個公式。請注意,第一個函式輸入參數為depth,需回傳Celsius,第二個函式輸入參數為Celsius,需回傳Fahrenheit
。
double
f1(double depth){
return
10*depth+20;
}
double
f2(double c){
return c*1.8+32;
}
8. (10分) 請寫個程式,讀入三個浮點數,輸出這三個數中最大的數及最小的數。
#include <stdio.h>
void main(void)
{
float
f1,f2,f3, max, min;
scanf("%f%f%f",
&f1,&f2,&f3);
if(f1>f2){
max
= f1;
min
= f2;
}else{
max
= f2;
min
= f1;
}
if(max
< f3)
max
= f3;
if(min
> f3)
min
= f3;
printf("Max:
%f\nMin: %f\n", max, min);
}
9. (10分) 請寫個程式,可讓使用者一直輸入正整數,直到所輸入的數為負整數或零為止,請輸出剛剛輸入的正整數中最大的數及最小的數。
#include <stdio.h>
void main(void)
{
int
input, max, min;
scanf("%d",
&input);
max
= input;
min
= input;
while(input
> 0){
if(input
> max)
max
= input;
if(input
< min)
min
= input;
scanf("%d",
&input);
}
printf("Max:
%d\nMin: %d\n", max, min);
}
10. (10分) 費氏數 (Fibonacci number)定義如下:
請寫個程式,輸入非負的整數n,可印出。例如:輸入0,得0;輸入1,的1;輸入2,得1;輸入3,得2;輸入4,得3;輸入5,得5。
#include <stdio.h>
void main(void)
{
int
n, i;
int
fi, fi_minus1, fi_minus2;
printf("Which
Fibonacci Number would you like to ask?");
scanf("%d",
&n);
if
(n == 0) fi=0;
else
if (n == 1) fi=1;
else
{
fi_minus2
= 0;
fi_minus1
= 1;
for
(i=2; i<=n; i++) {
fi
= fi_minus1 + fi_minus2;
fi_minus2
= fi_minus1;
fi_minus1
= fi;
}
}
printf("The
%d-th Fibonacci number is %d\n", n, fi);
}
11. (7分) (a) 在使用Microsoft Visual C++時,我們可以產生不同的專案
(project) 以管理程式,請問以下兩種專案:MFC AppWizard [exe] 和Windows Console Application Wizard,有何差異 (3分)? (b)
有些同學直接執行Visual C++產生出的執行檔(.exe)後,程式會立即關閉而看不到輸出結果,請提出可能的解決方法 (4分)。
(a) MFC AppWizard [exe]是用來開發具有視窗介面之專案;Windows Console Application
Wizard則是用來開發純文字輸出和輸入介面之專案。
(b) #include <conio.h>,並在程式最後加上 getch(); 這行指令。