ImageVerifierCode 换一换
格式:DOCX , 页数:28 ,大小:42.90KB ,
资源ID:3374197      下载积分:12 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/3374197.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(第三章习题答案.docx)为本站会员(b****5)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

第三章习题答案.docx

1、第三章习题答案第3章 函数3.1 选择题1以下正确的函数原型为( D )。(A)f1( int x; int y ); (B)void f1( x, y );(C)void f1( int x, y ); (D)void f1( int, int );2有函数原型 void fun2( int );,在下列选项中,不正确的调用是( C )。(A)int a = 21; fun2( a ); (B)int a = 15; fun2( a3 ); (C)int b = 100; fun2( &b ); (D)fun2( 256 );3有函数原型 void fun3( int );,在下列选项中,正

2、确的调用是( C )。(A)double x = 2.17; fun3( &x ); (B)int a = 15; fun3( a3.14 ); (C)int b = 100; fun3( &b ); (D)fun3( 256 );4有函数原型 void fun4( int & );,在下列选项中,正确的调用是( C )。(A)int a = 2.17; fun4( &a ); (B)int a = 15; fun4( a3.14 );(C)int b = 100; fun4( b ); (D)fun4( 256 );5有声明 void fun5( int & ); int a, int p

3、= &a;在下列选项中,正确的调用是( B )。(A)fun5(&a); (B)fun5(p); (C)fun5(a); (D)fun5(p);6有声明 int fun6( int ); int (pf)(int) = fun6; 在下列选项中,正确的调用是( C )。(A)int a=15; int n=fun6(&a); (B)int a = 15; cout(&pf)(a);(C)cout(pf)( 256 ); (D)cout pf( 256 );7在VC中,若定义一个函数的返回类型为void,则以下叙述正确的是( C )。(A)函数返回值需要强类型转换 (B)函数不执行任何操作(C)

4、函数本身没有返回值 (D)函数不能修改实际参数的值8函数参数的默认值不允许为( C )。(A)全局常量 (B)直接常量 (C)局部变量 (D)函数调用9使用重载函数编程序的目的是( A )。(A)使用相同的函数名调用功能相似的函数 (B)共享程序代码(C)提高程序的运行速度 (D)节省存储空间10在下列的描述中,( B )是错误的。(A)使用全局变量可以从被调用函数中获取多个操作结果(B)局部变量可以初始化,若不初始化,则系统默认它的值为0(C)当函数调用完后,静态局部变量的值不会消失(D)全局变量若不初始化,则系统默认它的值为011在下列选项中,( C )具有文件作用域。(A)语句标号 (B

5、)局部变量 (C)全局变量 (D)静态变量3.2 阅读下列程序,写出运行结果1#includeusing namespace std;#includeint f( int );int main() int i;for( i = 0; i 3; i + ) cout f( i ) endl;int f( int a ) int b = 0, c = 1;b +; c+;return int( a + pow( double(b), 2 ) + c );【解答】3452#includeusing namespace std;void func(int a, int b, int c = 3, in

6、t d = 4 );int main() func( 10, 15, 20, 30 );func( 10, 11, 12 );func( 12, 12 );void func( int a, int b, int c, int d ) coutatbtctd endl; 【解答】10 15 20 3010 11 12 412 12 3 43#includeusing namespace std;void func( int, int, int );int main() int x, y, z;func( 5, 6, &x );func( 7, x, &y );func( x, y, &z );

7、cout x , y , z endl;void func( int a, int b, int c ) b += a; c = b - a; 【解答】 6, 6, 64#includeusing namespace std;void func( int, int, int & );int main() int x=0, y=1, z=2;func( 1, 2, x );func( x + y, y, y );func( z, x + y, z );cout x , y , z endl;void func( int a, int b, int &c ) b += a; c = b - a;

8、【解答】 2, 1, 35#includeusing namespace std;void func( int , int , int & );int main() int a=10, b=20;int p=&a, q=&b;func( p, q, p );cout p= p , q= q endl;void func( int t1, int t2, int & rt ) t1 += 5; t2 += 5;rt = t1;rt += 5;cout t1= t1 , t2= t2 , rt= rt endl;【解答】*t1=20,*t2=25,*rt=20*p=20,*q=256#includ

9、eusing namespace std;int f2( int, int );int f1( int a, int b ) int c;a += a; b += b;c = f2( a+b, b+1 );return c;int f2( int a, int b ) int c;c = b % 2;return a + c;int main() int a = 3, b = 4;cout f1( a, b ) endl;【解答】 157#includeusing namespace std;int age( int n ) int f;if( n = 1 ) f = 10;else f =

10、age( n-1 ) + 2;return f;int main() cout age : age( 5 ) endl; 【解答】 age:188#includeusing namespace std;int f1( int a, int b ) return a + b; int f2( int a, int b ) return a - b; int f3( int(t )( int, int ), int a, int b ) return (t )( a, b ); int main() int (p )( int, int );p = f1;cout f3( p, 4, 8 ) en

11、dl;p = f2;cout f3( p, 8, 4 ) endl;【解答】 12 49#includeusing namespace std;int sub( int, int );int a = 1;int main() int m = 1, n = 2, f;f = sub( m, n );cout a t f endl;f = sub( m, n );cout a t f endl;int sub( int c, int d ) static int m = 2, n = 5;cout m t n t endl;a = + a; c = m +; d = n +;return c +

12、d;【解答】 2 5 2 7 3 6 3 93.3 思考题1函数的作用是什么?如何定义函数?什么是函数原型?【解答】函数的两个重要作用:(1)任务划分,把一个复杂任务划分为若干小任务,便于分工处理和验证程序正确性;(2)软件重用,把一些功能相同或相近的程序段,独立编写成函数,让应用程序随时调用,而不需要编写雷同的代码。函数的定义形式: 类型 函数名 ( 形式参数表 ) 语句序列函数原型是函数声明,告诉编译器函数的接口信息:函数名、返回数据类型、接收的参数个数、参数类型和参数顺序,编译器根据函数原型检查函数调用的正确性。2什么是函数值的返回类型?什么是函数的类型?如何通过指向函数的指针调用一个已

13、经定义的函数?编写一个验证程序进行说明。【解答】(1)函数的返回类型是函数返回的表达式的值得类型;(2)函数类型是指函数的接口,包括函数的参数定义和返回类型;(3)若有 functionType functionName; /functionType是已经定义的函数类型 functionType *functionPointer=functionName; /定义函数指针并获取函数地址则可以通过函数指针调用函数: (*functionPointer)(argumentList);或 functionPointer(argumentList);其中argumentList是实际参数表。验证程序:

14、#includeusing namespace std;int main() typedef int myfunc(int,int); myfunc f,*fp; int a=10,b=6; fp=f; coutUsing f(a):f(a,b)endl; /函数名调用函数 coutUsing fp(a):fp(a,b)endl; /函数指针调用函数 coutUsing (*fp)(a):(*fp)(a,b)endl; /函数指针调用函数 return 0;int f(int i,int j) return i*j;3 什么是形式参数?什么是实际参数?C+函数参数有什么不同的传递方式?编写一个

15、验证程序进行说明。【解答】参数是调用函数与被调用函数之间交换数据的通道。函数定义首部的参数称为形式参数,调用函数时使用的参数称为实际参数。C+有三种参数传递机制:值传递(值调用);指针传递(地址调用);引用传递(引用调用)。 验证程序:#includeusing namespace std;void funcA(int i) i=i+10; void funcB(int *j) *j=*j+20; void funcC(int &k) k=k+30; int main() int a=1; funcA(a);couta=aendl; funcB(&a);couta=aendl; funcC(a

16、);couta=aendl;程序输出:a=1 /传值参数,实际参数值不变a=21 /指针参数,形式参数通过间址修改实际参数a=51 /引用参数,形式参数通过别名方式修改实际参数4C+函数通过什么方式传递返回值?当一个函数返回指针类型时,对返回表达式有什么要求?当返回引用类型时,是否可以返回一个算术表达式?为什么?【解答】C+首先计算表达式的值,然后把该值赋给函数返回类型的匿名对象,通过这个对象,把数值带回调用点,继续执行后续代码。当函数返回指针类型时,返回的地址值所指对象不能是局部变量。因为局部变量在函数运行结束后会被销毁,返回这个指针是毫无意义的。返回引用的对象不能是局部变量,也不能返回表达

17、式。算术表达式的值被储存在匿名空间中,函数运行结束后会被销毁,返回这个变量的引用也是无意义的。5变量的生存期和变量作用域有什么区别?请举例说明。【解答】变量的生存期是指程序运行后,变量占有内存的时间;变量作用域指的是指变量声明之后,在程序正文中有效的那部分区域。例如,定义函数:void count() static int n=0; /该函数中n被定义为static变量,生存期是整个程序运行时期;但作用域只在count函数中。6静态局部变量有什么特点?编写一个应用程序,说明静态局部变量的作用。【解答】静态局部变量的生存期是全程的,作用域是局部的。程序开始执行时就分配和初始化存储空间(默认初始化

18、值为0)。定义静态局部变量的函数退出时,系统保持其存储空间和数值。下次调用这个函数时,static变量还是上次退出函数时的值。直至整个程序运行结束,系统才收回存储空间。程序略。7在一个语句块中能否访问一个外层的同名局部变量?能否访问一个同名的全局变量? 如果可以,应该如何访问?编写一个验证程序进行说明。【解答】一个语句块中不能访问外层的同名局部变量。可以访问一个同名的全局变量。验证程序:#includeusing namespace std;int a=0; /全局变量aint main() int a=1; /外层局部变量a int a=2; /内层局部变量a coutLocal a is

19、aendl; /输出内层局部变量a coutMain a is aendl; /输出外层局部变量a coutGlobal a is :aendl; /输出全局部变量a8有函数原型void f (int & n);和函数调用int a;/f(a);有人说,因为n是a的引用,则在函数f中访问n相当于访问a,所以,可以在f的函数体内直接使用变量名a。这种说法正确吗?为什么?编写一个验证程序。【解答】形式参数n的作用域是f函数,实际参数a的作用域是调用f函数的模块(例如main函数),所以在f函数中可见n而不可见a。因此,这种说法不正确。f函数内不能直接使用变量名a,只能通过别名n访问a。验证程序:#

20、includeusing namespace std;void f ( int&n );int main() int a = 1 ; f( a ); couta=aendl;void f ( int&n ) a+; /错误,直接使用a n+; /正确产生编译错误:error C2065: “a”: 未声明的标识符9有以下程序:#includeusing namespace std;int main() char c;cinc;if(c=y|c=Y)int a=1;elseint a=0;couta=aendl;编译错误为:error C2065: a : undeclared identifi

21、er,指示语句行 couta=aendl;变量a没有定义。请分析原因,并进行修改。【解答】if-else语句中分别出现变量a的说明语句,是作用域为if分支和else分支的两个变量。在if-else语句之后,这两个变量都是没有定义的,因此出现编译错误。正确的程序是:#includeusing namespace std;int main() char c; int a; cinc; if(c=y|c=Y) a=1; else a=0; couta=aendl;10有函数原型double function(int,double);函数function的返回值类型是什么?函数的类型是什么?使用typ

22、edef定义函数的类型。有函数调用语句x=function(10,(2 (0.314+5);其中的括号“()”与函数原型中括号有什么语义区别?【解答】函数function的返回值类型是double函数类型是: double (int,double)可以定义为: typedef double funType (int,double);函数调用function(10,(2*(0.314+5) 中,外层括号表示调用函数匹配的实际参数表,里面的两层括号是表达式运算。11请分析以下各语句的意义。int fun();int (pf)();fun();pf = fun;pf();【解答】int * fun(

23、) ; /函数原型声明。fun是返回int*类型,没有参数的函数int * (*pf)(); /声明指针变量。pf是指向函数的指针,函数类型为int*()fun() ; /调用函数语句pf = fun ; /向指针变量赋值。函数指针pf指向函数funpf() ; /用指针变量间址调用函数3.4 编程题1已知 ,其中,sh为双曲正弦函数,即。编写一个程序,输入x的值,求y的值。【解答】#include#include using namespace std;double sh( double t );int main() double x,y; cout x; y = sh( 1+sh(x) )

24、/( sh( 2*x )+sh( 3*x ) ); cout y= y endl;double sh( double t ) return ( exp( t )-exp( -t ) )/2; 2输入m、n和p的值,求s =的值。注意判断运算中的溢出。【解答】using namespace std;double f( long k,long num );int main()long m,n,p; double s,f1,f2,f3; cout mnp; f1=f( 1,m ); f2=f( 3,n ); f3=f( 5,p ); if (f1 & f2 & f3 ) s = ( f1 + f2)

25、 /f3; cout s= s endl; else cout溢出!n;double f( long k,long num ) long i; double sum=0; for( i=1; i=num & sum2147483647; i+ ) sum = sum + pow( double (i),double (k) ); if (i=num) return 0; /溢出时返回 return sum;3输入a、b和c的值,编写一个程序求这三个数的最大值和最小值。要求:把求最大值和最小值操作分别编写成一个函数,并使用指针或引用做为形式参数把结果返回main函数。【解答】(1)使用指针参数#

26、includeusing namespace std;void fmaxmin( double,double ,double ,double *,double * );int main() double a,b,c,max,min;cout a b c;fmaxmin( a,b,c,&max,&min );cout max= max endl;cout min= min y ) u = x; v = y; else u = y; v = x; ; if ( zu ) u = z; if ( zv ) v = z; *p1 = u; *p2 = v;(2)使用引用参数#includeusing namespace std;void fmaxmin( double,double ,double ,double& ,double& );int main() double a,b,c,max,min; cout a b c; fmaxmin( a,b,c,max,min ); cout max= max endl; cout min= min e

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1