第4章数组4.1 选择题1.以下对一维数组a 的定义正确的是(C )。
(A)int n = 5, a[n]; (B)int a(5);(C)const int N = 5; int a[N]; (D)int n; cin>>n; int a[n];2.下列数组定义语句中,不合法的是(A )。
(A)int a[3] = { 0, 1, 2, 3 }; (B)int a[] = { 0, 1, 2 };(C)int a[3] = { 0, 1, 2 }; (D)int a[3] = { 0 };3.已知int a[10] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }, *p = a;,不能..表示数组 a 中元素的式子是( C )。
(A)*a (B)*p (C)a (D)a[ p-a ]4.已知int a[] = { 0,2,4,6,8,10 }, *p = a+1; 其值等于0的表达式是(D )。
(A)* (p++) (B)*(++p) (C)*(p--) (D)*(--p)5.以下不能对二维数组a进行正确初始化的语句是(C )。
(A)int a[2][3] = { 0 };(B)int a[][3] = { { 0,1 }, { 0 } };(C)int a[2][3] = { { 0, 1 }, { 2, 3 }, { 4, 5 } };(D)int a[][3] = { 0, 1, 2, 3, 4, 5 };6.已知int a[][3] = { { 0, 1 }, { 2, 3, 4 }, { 5, 6 }, { 7 } }; 则a[2][1]的值是(C )。
(A)0 (B)2 (C)6 (D)77.已知int a[3][3] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 不能表示数组元素a[2][1]的地址是(B )。
(A)&a[2][1] (B)*(a[2]+1) (C)a[2]+1 (D)*(a+2)+18.已知char *a[]={ "fortran", " basic", "pascal", "java", "c++" };,则cout<<a[3];的显示结果是(C )。
(A)t (B)一个地址值(C)java (D)javac++9.若用数组名作为调用函数的实参,则传递给形参的是(A )。
(A)数组存储首地址(B)数组的第一个元素值(C)数组中全部元素的值(D)数组元素的个数10.设有char *s1="ABCDE", *s2="ABCDE",*s3=s1;,下列表达式中值等于true的是(D )。
(A)strcmp(s1,s2) (B)strcmp(s1,s3)==1 (C)strcmp(s2,s3)==-1(D)strcmp(s1,s3) ==011.设char *s1, *s2;分别指向两个字符串,可以判断字符串s1和s2是否相等的表达式为(D )。
(A)s1=s2 (B)strlen(s1,s2)(C)strcpy(s1,s2)==0 (D)strcmp(s1,s2)==012.设char *s1, *s2;分别指向两个字符串,可以判断字符串s1是否大于字符串s2的表达式为(C )。
(A)strcmp(s1,s2)<0 (B)strcmp(s1,s2)==0(C)strcmp(s1,s2)>0 (D)strcmp(s2,s1)>04.2 阅读下列程序,写出运行结果1.#include <iostream>using namespace std;int main(){int i, count=0, sum=0;double 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 ];count ++;}average = sum/count;cout << "count = " << count << '\t' << "average = " << average << endl;}【解答】conut = 5 average = 52.#include <iostream>using namespace std;int main(){int a[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };int *p = a, sum = 0;for(; p<a+9; p++ )if(*p % 2 == 0 )sum += *p;cout << "sum = " << sum << endl;}【解答】sum = 203.#include <iostream>#include <iomanip>using namespace std;const int N = 5;int main(){int a[N][N]={ 0 }, i, j, k;for( k=1, i=0; i<N; i++ )for( j=i; j>= 0; j--, k++ )a[j][i - j ] = k;for( i=0; i<N; i++ ){for( j=0; j<N; j++ )cout << setw( 3 ) << a[i][j];cout << endl;}}【解答】1 3 6 10 152 5 9 14 04 8 13 0 07 12 0 0 011 0 0 0 04.#include <iostream>using namespace std;int f(int [],int);int main(){int a[] = { -1, 3, 5, -7, 9, -11 };cout << f( a, 6 ) << endl;}int f( int a[], int size ){int i, t=1;for( i=0; i<size; i ++ )if( a[i]>0 ) *= a[i];return t;}【解答】1355.#include <iostream>using namespace std;int f( int [][3], int, int );int main(){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<row; i ++ )for( j=0; j<col; j++ ){a[i][j]++;if( i == j ) t *= a[i][j];}return t;}【解答】456.#include <iostream>using namespace std;void test1( int *a1 ){a1 = new int( 5 );cout << "*a1 = " << *a1 << endl;}void test2(int * & a2){a2 = new int( 5 );cout << "*a2 = " << *a2 << endl;}int main(){int *p = new int( 1 );test1( p );cout << "test1: *p1 = " << *p << endl;test2( p );cout << "test2: *p2 = " << *p << endl;}【解答】*a1= 5test1: *p1= 1*a2= 5test2: *p2= 57.#include <iostream>using namespace std;int main(){char s[] = "abccda";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;}}【解答】$#*#*#%8.#include <iostream>using namespace std;int main(){char *str[] = { "c++", "basic", "pascal" };char **p;int i;p = str;for( i=0; i<3; i++ )cout << * ( p+i ) << endl;}【解答】c++basicpascal9.#include <iostream>using namespace std;int main(){char s1[] = "Fortran", s2[] = "Foxpro";char *p, *q;p = s1; q = s2;while(*p && *q ){ if (*p == *q )cout << *p;p ++;q ++;}cout << endl;}【解答】For10.#include <cstring>#include <iostream>using namespace std;int main(){char str[][10] = { "vb", "pascal", "c++" }, s[10];strcpy_s( s, ( strcmp( str[0], str[1] ) < 0 ? str[0] : str[1] ) );if( strcmp( str[2], s ) < 0 )strcpy_s( s, str[2] );cout << s << endl;}【解答】C++4.3 思考题1.数组说明语句要向编译器提供什么信息?请写出一维数组、二维数组说明语句的形式。