課程名稱︰計算機程式設計
課程性質︰系定必修
課程教師︰丁培毅
開課學院:
開課系所︰數學系
考試日期(年月日)︰97年11月10日
考試時限(分鐘):120分鐘
是否需發放獎勵金:是
(如未明確表示,則不予發放)
試題 :
I. Please correct possible errors and/or fill in the missing expression in the
following program (18)
01#include <stdio.h>;
02int main(void);
03{
04 int b; c;
05 int status;
06 do
07 {
08 error = 0;
09 printf('Please input two positive number
10 (with the first one less than the second) :');
11 status = scanf("%d %d", b, c)
12 if(status = 2)
13 {
14 / * Validity check * /
15 if (b < 0 || c < 0)
16 error = 1;
17 printf("Input numbers (%d, %d) are"
18 "negative\n", b, c);
19 }
20 else if(b > c)
21 printf("Order is incorrect.\n");
22 }while error;
23
24 b = sum(b, c);
25 print("The sum is %f\n", b);
26 return;
27}
28
29int sum(int num1, num2)
30{
31 int summation;
32 int num1;
33 int num2;
34 num1 + num2 = summation;
35 Return summation;
36}
II. Basic program development
1. What are the five steps of the software development method to slove a
programming problem? (5)
2. What is the basic language construct to achieve "Modular Programming" in a
C program? (3)
3. Why do you need to define variable in a C program (e.g. double number;)? (6)
Also, why do you need to specify its type (e.g. double)? (6)
4. When do you need to define a variable as an integer (e.g. int x;) and when
do you need to define a variable as a floating point number (e.g. double x;)
(6, please address the representation accuracy, range and the computation
issues)
5. When do you consider using a "counting for loop" instead of a general "while
loop"? (6)
III. Program writing (30)
1. Assume that we have a function "double myrand(void);" that returns a random
number uniformly distributed in [0,1) upon each call, please write a function
"int toss(void);" that simulates a crooked dice and returns a random integer
representing the number of pips. The probability masses of each pips are
shown in the following table:
╔═════════╤═══╤═══╤═══╤═══╤═══╤═══╗
║ Pips │ 1 │ 2 │ 3 │ 4 │ 5 │ 6 ║
╟─────────┼───┼───┼───┼───┼───┼───╢
║ Probability mass │ 1/3 │ 1/4 │ 1/6 │ 1/12 │ 1/12 │ 1/12 ║
╚═════════╧═══╧═══╧═══╧═══╧═══╧═══╝
2. Please design a driver program using the "switch statement" to test the above
function (Hint: use a loop to run many times, accumulate the results'
relative occurrence frequency, and print out the difference with the above.)
IV. Program reading (Please explain the hign level purpose and algorithm of the
following function and replace the first "while loop" with a "for loop")(20)
01double func(double x, int y)
02{
03 double p;
04 p = 1.0;
05 if(y>0)
06 while(y>0)
07 {
08 p *= x;
09 y--;
10 }
11 else
12 while(y<0)
13 {
14 p /= x;
15 y++;
16 }
17 return p;
18}
V. Program desk checking
1. Please simulate the execution of the following expressions: show me the
sequence of each operator and tell me the intermediate results of each
operations (10)
a. 4 / 3 * 1.5 - 1.2 * -1 > 1.8
b. fabs(-7) note: double fabs(double);
2. How many lines does the following program segment print out? (show me the
detailed analysis) (10)
for(i=0; i<5; i++)
for(j=0; j<i; j++)
if((i+j)%2)
printf("%d %d\n", i, j);
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.136.9.109