C++程序设计课后习题:第4章 数组

上传人:汽*** 文档编号:495661418 上传时间:2023-02-08 格式:DOC 页数:20 大小:93KB
返回 下载 相关 举报
C++程序设计课后习题:第4章 数组_第1页
第1页 / 共20页
C++程序设计课后习题:第4章 数组_第2页
第2页 / 共20页
C++程序设计课后习题:第4章 数组_第3页
第3页 / 共20页
C++程序设计课后习题:第4章 数组_第4页
第4页 / 共20页
C++程序设计课后习题:第4章 数组_第5页
第5页 / 共20页
点击查看更多>>
资源描述

《C++程序设计课后习题:第4章 数组》由会员分享,可在线阅读,更多相关《C++程序设计课后习题:第4章 数组(20页珍藏版)》请在金锄头文库上搜索。

1、第4章 数组4.1 选择题1以下对一维数组 a 的定义正确的是( C )。(A)int n = 5, an;(B)int a(5);(C)const int N = 5; int aN;(D)int n; cinn; int an;2下列数组定义语句中,不合法的是( A )。(A)int a3 = 0, 1, 2, 3 ;(B)int a = 0, 1, 2 ;(C)int a3 = 0, 1, 2 ;(D)int a3 = 0 ;3已知 int a10 = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 , *p = a;,不能表示数组 a 中元素的式子是( C )。(A)*a (

2、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 a23 = 0 ;(B)int a3 = 0,1 , 0 ;(C)int a23 = 0, 1 , 2, 3 , 4, 5 ;(D)int a3 = 0, 1, 2, 3, 4, 5 ;6已知int a3 = 0, 1 , 2, 3, 4 , 5, 6 , 7 ; 则 a21的值是( C )。(A)0(B)2 (C)6(D

3、)77已知int a33 = 1, 2, 3, 4, 5, 6, 7, 8, 9 ; 不能表示数组元素a21的地址是( B )。(A)&a21(B)*(a2+1) (C)a2+1 (D)*(a+2)+18已知char *a= fortran, basic, pascal, java, c+ ;,则 couta3;的显示结果是( C )。(A)t(B)一个地址值(C)java(D)javac+9若用数组名作为调用函数的实参,则传递给形参的是( A )。(A)数组存储首地址 (B)数组的第一个元素值(C)数组中全部元素的值 (D)数组元素的个数10设有char *s1=ABCDE, *s2=ABC

4、DE,*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(D)strc

5、mp(s2,s1)04.2 阅读下列程序,写出运行结果1#include 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; i10; i+ ) if( ai % 2 = 0 ) continue;sum += a i ; count +;average = sum/count;cout count = count t average = average endl;【解答】conut = 5 average = 5

6、2#include using namespace std;int main() int a9 = 1, 2, 3, 4, 5, 6, 7, 8, 9 ;int *p = a, sum = 0;for(; pa+9; p+ ) if(*p % 2 = 0 ) sum += *p;cout sum = sum endl;【解答】sum = 203#include #include using namespace std;const int N = 5;int main() int aNN= 0 , i, j, k;for( k=1, i=0; i= 0; j-, k+ ) aji - j = k

7、;for( i=0; iN; i+ ) for( j=0; jN; j+ ) cout setw( 3 ) aij; 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 04#include 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; i0 ) *= ai;return

8、t;【解答】1355#include using namespace std;int f( int 3, int, int );int main() int a3 = 0, 1, 2, 3, 4, 5, 6, 7, 8 ;cout f( a, 3, 3 ) endl;int f( int a3, int row, int col ) int i, j, t=1;for( i=0; irow; i + ) for( j=0; jcol; j+ ) aij+;if( i = j ) t *= aij;return t;【解答】456#include using namespace std;void

9、 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 using namespace std;int

10、main() char s = abccda;int i; char c;for( i = 1; ( c=si ) != 0; i + ) switch( c ) case a : cout %; continue;case b : cout $; break;case c : cout *; break;case d : continue;cout # endl;【解答】$#*#*#%8#include using namespace std;int main() char *str = c+, basic, pascal ;char *p;int i;p = str;for( i=0; i

11、3; i+ ) cout * ( p+i ) endl;【解答】c+basicpascal9#include 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 #include using namespace std;int main() char str10 = vb, pascal, c+ , s10;strcpy_s( s, ( strcmp( str0, str1 ) 0 ? str0 : str1 ) );if( strcmp( str2, s ) 0 ) strcpy_s( s, str2 );cout s endl;【解答】C+4.3 思考题1数组说明语句要向编译器提供什么信息?请写出一维数组、二维数组说明语句的形式。【解答】数组说明语句要向编译器提供数组名(标识符),数组元素的类型、数组维数、数组长度(元素的个数)等信息。一维数组说明语句为: 类型 数组名表达式二维数组说明语句为: 类型 数组名表达式1 表达式2

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 其它相关文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号