Assignment 4答案
- 格式:doc
- 大小:41.50 KB
- 文档页数:3
Assignment 4
Read Dale’s book, Chapter 7 (function) and Chapter 9 (recursion).
1选择题
1.已知 int i,x,y;在下列选项中错误的是( c )。
(a) if(x == y)i++; (b) if(x = y)i--;
(c) if( xy )i--; (d) if( x+y )i++;
2.设有函数关系为y=010001xxx ,下面选项中能正确表示上述关系为( c )。
(a) y = 1; (b) y = -1;
if( x>=0 ) if( x!=0)
if( x==0 )y=0; if( x>0 )y = 1;
else y = -1; else y = 0
(c) if( x<=0 ) (d) y = -1;
if( x<0 )y = -1; if( x<=0 )
else y = 0; if( x<0 )y = -1;
else y = 1; else y = 1;
3.假设i=2,执行下列语句后i的值为( b )。
switch(i)
{ case 1:i++;
case 2:i--;
case 3:++i;break;
case 4:--i;
default:i++;
}
(a) 1 (b) 2 (c) 3 (d) 4
4.已知int i=0,x=0;下面while语句执行时循环次数为( d )。
while(!x && i<3 ){ x++;i++;}
(a) 4 (b) 3 (c) 2 (d) 1
5.已知int i=3;下面do_while 语句执行时循环次数为( b )。
do{ i--; cout<
(a) 1 (b) 2 (c) 3 (d) 无限
6.下面for语句执行时循环次数为( b )。
for ( int i=0,j=5;i=j;)
{ cout << i << j << endl;
i++;j--;
}
(a) 0 (b) 5 (c) 10 (d) 无限
7.以下死循环的程序段是( b )。
(a) for(int x=0;x<3 ;){ x++;};
(b) int k=0; do { ++k;} while( k>=0 );
(c) int a=5;while(a){ a--;};
(d) int i=3;for(;i;i--);
8.以下正确的函数原型为( d )。
(a) f( int x; int y ); (b) void f( x, y );
(c) void f( int x, y ); (d) void f( int, int );
9.有函数原型 void fun1( int ); 下面选项中,不正确的调用是( c )。
(a) double x = 2.17 ; fun1( x );
(b) int a = 15 ; fun1( a*3.14 ) ;
(c) int b = 100 ; fun1( & b );
(d) fun1( 256 );
2阅读下列程序,写出执行结果
1. #include
void main()
{ int i,j;
for( i=1, j=5; i { j--; } cout << i << ′\t′<< j << endl; } 3 3 2. #include void main() { int i, s = 0; for( i=0; i<5; i++ ) switch( i ) { case 0: s += i; break; case 1: s += i; break; case 2: s += i; break; default: s += 2; } cout << "s=" << s < } s=7 3. #include void main() { int i, j, x = 0; for( i=0; i<=3; i++ ) { x++; for( j=0; j<=3; j++ ) { if( j % 2 ) continue; x++; } x++; } cout << "x=" << x << endl; } x=16 3. 编程题:求100到999中的水仙花数。所谓水仙花数是指一个三位数,它的每位数字的立方之和等于该数。例如,因为153=13+53+33,所以153为水仙花数。 #include using namespace std; int main () {int i,j,k,n; cout<<"narcissus numbers are:"< for (n=100;n<1000;n++) {i=n/100; j=n/10-i*10; k=n%10; if (n == i*i*i + j*j*j + k*k*k) cout< } cout< return 0; }