的地址是( a,b )。
(a) &[2][1] (b) *(a[2]+1) (c) a[2]+1 (d) *(a+2)+1 8.已知char *a[]={ \fortran\, \basic\, \pascal\, \java\, \c++\ ; 则 cout<
(a) t (b) 一个地址值 (c) java (d) javac++ 9.若用数组名作为调用函数的实参,则传递给形参的是( a )。
(a) 数组存贮首地址 (b) 数组的第一个元素值 (c) 数组中全部元素的值 (d) 数组元素的个数 10.在下列选项中,( b, d )是错误的。
(a) gets和puts函数可以输入输出包含空格的字符串 (b) cin不能输入包含空格的字符串 (c) cout不能输出包含空格的字符串
(d) 使用赋值运算符可以对字符数组整体赋值 11.下列描述中,错误的是( c )。 (a) 输出字符指针就是输出字符串
(b) 输出字符指针的间接引用就是输出单个字符 (c) 具有相同字符的两个字符串常量相等 (d) 两个数组名的比较是地址的比较
12.判断字符串s1和s2是否相等的表达式为( d )。 (a) s1=s2 (b) s1==s2
(c) strcpy(s1,s2)==0 (d) strcmp(s1,s2)==0 13.判断字符串s1是否大于字符串s2的表达式为( c )。
(a) s1>s2 (b) strcmp(s1,s2)==0 (c) strcmp(s1,s2)>0 (d) strcmp(s2,s1)>0
4.2 阅读下列程序,写出执行结果
1.#include
void main()
{ int i, conut=0, sum=0 ; float average ;
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 } ; for( i=0; i<10; i++ )
{ if( a[i] % 2 == 0 ) continue ; sum += a[ i ] ; conut ++ ; }
average = sum / conut ;
cout << \
26
}
答案:
conut = 5 average = 5 2.#include
void main()
{ int a[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 } ; int *p = a , sum = 0 ; for( ; p
if( *p % 2 == 0 ) sum += *p ; cout << \}
答案: sum = 20
3.const int n = 5 ;
#include
{ int a[n][n]={ 0 }, i, j, k ; for( k=1 , i=0 ; i cout << setw( 3 ) << a[i][j] ; cout << endl ; } } 答案: 1 3 6 10 15 2 5 9 14 0 4 8 13 0 0 7 12 0 0 0 11 0 0 0 0 4.int f(int [],int); #include { int a[] = { -1, 3, 5, -7, 9, -11 } ; cout << f( a, 6 ) << endl ; } 27 int f( int a[], int size ) { int i, t=1 ; for( i=0 ; i 答案: 135 5.int f( int [][3], int, int ) ; #include { int a[][3] = { 0, 1, 2, 3, 4, 5, 6, 7, 8 } ; cout << f( a, 3, 3 ) << endl ; } int f( int a[][3], int row, int col ) { int i, j, t=1 ; for( i=0; i if( i == j ) t *= a[i][j] ; } return t ; } 答案: 45 6.#include void main() { char s[] = \ int i ; char c ; for( i = 1 ; ( c=s[i] ) != '\\0'; i ++ ) { switch( c ) { case 'a' : cout << '%' ; continue ; case 'b' : cout << '$' ; break ; case 'c' : cout << '*' ; break ; case 'd' : continue ; } cout << '#' << endl ; } } 28 答案: $# *# *# % 7.#include void main() { char *str[] = { \ char **p ; int i ; p = str ; for( i=0 ; i<3 ; i++ ) cout << *( p+i ) << endl ; } 答案: c++ basic pascal 8.#include void main() { char s1[] = \ char *p , *q ; p = s1 ; q = s2 ; while( *p && *q ) { if ( *p == *q ) cout << *p ; p ++ ; q ++ ; } cout << endl ; } 答案: For 9.#include #include { char str[][10] = { \ strcpy( s , ( strcmp( str[0] , str[1] ) < 0 ? str[0] : str[1] ) ) ; if( strcmp( str[2], s ) < 0 ) strcpy( s, str[2] ) ; 29 cout << s << endl ; } 答案: C++ 4.3 编程题 nn1.已知求成绩的平均值和均方差公式:ave??i?1sin,dev?(s?i?1i?ave)n2, 其中n为学 生人数,si为第i个学生成绩。求某班学生的平均成绩和均方差。 解答:略。 2.用随机函数产生10个互不相同的两位整数存放到一维数组中,并输出其中的素数。 解答: 程序定义了三个函数:函数createAry对数组元素赋值,函数outAry输出数组,以及函数isPrime判断素数。由主函数main调用它们,完成题目要求。 #include void createAry(int[], int); void outAry(const int[], int); void main() { int a[10]={0}; createAry(a,10); outAry(a,10); //对数组元赋值 //输出数组元素 //寻找数组中的素数 for( int i=0; i<10; i++ ) { if (isPrime(a[i])) cout << a[i] << \是素数!\ } cout<<(isPrime(25))< void createAry(int a[], int n) { srand( time(0) ); int i, j, x; i=0; while(i 30