課程名稱:計算機程式設計(一)
課程性質:必修
課程範圍:1~7章 (introduction ~ pointer)
開課教師:蔡銘峰
開課學院:理學院
開課系級:資科一
考試日期(年月日):2011/11/16
考試時限(Mins):180分
附註:
試題本文:
1. Basic Unix Commands [5%] ( 1 point for each )
Write Unix command for the following situations:
a) Create a directory named nccucs
b) Rename a file from a.txt to b.txt
c) Display the line-by-line difference between MRU.txt and GTR.txt
d) Display the on-line manual pages about the cp command
e) List all the files whose filename starts with assign, and store the
result into a file named result.txt
2. Basic C statements [12%] ( 2 points for each )
Write a "single" C statement to accomplish each of the following: (Note that
in C each statement should end with a semicolon.)
a) Define the variables c, thisVariable, q76354 and number to be of
type int.
b) Print "The product is" followed by the value of the integer variable
result.
c) Define a variable x and initialize the variable to 1.
d) Add variable x to variable sum and assign the result to variable sum
e) Cauculate the reminder after q is divided by divisor and assign the
result to q.
f) Print the value 123.4567 with 2 digits of precision.
3. Write down the output of the following programs.
a)
#include <stdio.h>
int main(){
int i;
for( i = 1; i <= 100; i++ ){
if( i % 9 != 0 ) continue;
if( i >= 80 ) break;
printf("%d", i);
}
printf("\n");
return 0;
}
b)
#include <stdio.h>
int main() {
int i = 20;
while (1) {
switch(i % 2){
case 0:
printf("even");
break;
case 1:
printf("odd");
break;
default:
printf("Not a number!");
}
i -= 2;
if (i < 10) break;
}
printf("\n");
return 0;
}
4. Operator Priority and Logical Operations [10%] (5 points for each)
Please write down the values of each variable in the following programs:
a) What are the values of i, j, k, x, y, and z? (1 point for each answer)
#include <stdio.h>
int main(){
int i = 2;
int j = 3;
int k = 4;
int x = i+++j;
int y = 0;
if(j=k<<1)
y = j + k;
else
y = k - k;
int z = k + (k+=1);
printf("i=%d,j=%d,k=%d,x=%d,y=%d,z=%d\n",i,j,k,x,y,z);
return 0;
}
b) What are the values of x and y? (2.5 points for each answer)
#include <stdio.h>
int main(){
int a = 4, b = 7, x = 0, y = 0;
x = a & b;
if(a && b)
y = (!a||!b)+(a&&!b);
else
y = (!a||b)+(!a&&b);
return 0;
}
5. Function Prototype [8%] (2points for each)
Write down the function prototype for each of the following: (Note that a
function prototype should end with a semicolon.)
a) Fuction hypotenuse that takes two double arguments: side1 and side2, and
returns a double result
b) Fuction smallest that taskes three integers: x, y, z, and returns an
integer
c) Function instructions that does not receive any arguments and does
not return a value.
d) Function intToFloat that takes an integer argument: number, and returns
a floating-point result
6. Call-by-Value and Call-by-Reference [12%]
What is the result of the following program? (3 points for each answer)
#include <stdio.h>
void callBy Value( int number ){
number = number * 10;
}
void callByReference( int *nPtr ) {
*nPtr = *nPtr * *nPtr * *nPtr;
}
int main( void ) {
int number = 2;
callByValue( number );
printf( "%d ", number );
callByReference( &number );
printf( "%d ", number );
callByValue( number );
printf( "%d ", number );
callByReference( &number );
printf( "%d\n", number );
return 0;
}
7. Write down the output of the program:
#include<stdio.h>
int love, hate;
int fp(int x){
int life=1;
love++; hate=x+1;
life+=love+hate; x++;
return life;
}
int f2(int x){
static int life=1;
love++; hate=x+1;
life+=love+hate; x++;
return life;
}
int main(){
int life;
love=hate=1;
life=fp(love); printf("%3d %3d %3d\n", life, love, hate);
life=fp(love); printf("%3d %3d %3d\n", life, love, hate);
love=hate=1;
life=fp(love); printf("%3d %3d %3d\n", life, love, hate);
life=fp(love); printf("%3d %3d %3d\n", life, love, hate);
life=fp(love+1); printf("%3d %3d %3d\n", life, love, hate);
return 0;
}
8. Pointer [8%] (2 points for each)
#include<stdio.h>
int main(){
int *a;
int b = 2, c = 5;
a = &c;
b = *a;
int *d = &b;
return 0;
}
Suppose that the address of b is at 3000 and that of e at 4000. What is the
result of following questions? (Note that the space of an integer is 4 byte)
a) ++*a = ?
b) (++b)*2 = ?
c) (c+2) = ?
d) (d+1) = ?
9. Pointer and Array [12%] (2 pts for each)
An array of integers is stored starting at address 1000.
int arr[] = {12, 15, 32, 14, 66};
/* the address of the first in in the array is 1000*/
/* arr:[1000]