【计算机类职业资格】二级C语言-343 (1)及答案解析.doc

上传人:deputyduring120 文档编号:1325253 上传时间:2019-10-17 格式:DOC 页数:5 大小:33.50KB
下载 相关 举报
【计算机类职业资格】二级C语言-343 (1)及答案解析.doc_第1页
第1页 / 共5页
【计算机类职业资格】二级C语言-343 (1)及答案解析.doc_第2页
第2页 / 共5页
【计算机类职业资格】二级C语言-343 (1)及答案解析.doc_第3页
第3页 / 共5页
【计算机类职业资格】二级C语言-343 (1)及答案解析.doc_第4页
第4页 / 共5页
【计算机类职业资格】二级C语言-343 (1)及答案解析.doc_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

1、二级 C 语言-343 (1)及答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.请补充 main()函数,该函数的功能是求方程 ax 2 +bx+c=0 的两个实数根。方程的系数 a、b、c 从键盘输入,如果判别式(disc=b*b-4*a*c)小于 0,则要求重新输入 a、b、c 的值。 例如,当 a=1,b=2,c=1 时,方程的两个根分别是:x 1 =-1.00,x 2 =-1.00。 注意:部分源程序给出如下。 请勿改动函数中的其他任何内容,仅在横线上填入所编写的若干表达式或语句。 试题程序: #includemath.h #inc

2、ludestdio.h void main() double a,b,c,x1,x2,disc; do printf(“Input a,b,c:“); scanf(“%1f,%1f,1f“, disc=b*b-4*a*c; if(disc0) printf(“disc=%1f/n Input again!/n“,disc); while( 1); printf(“*the result*/n“); x1=(-b+ 2(disc)/(2*a); x2=(-b- 3(disc)/(2*a); printf(“/nx1=%6.21f/nx2=%6.21f/n“,x1,x2); (分数:30.00)二

3、、程序改错题(总题数:1,分数:30.00)2.由 N 个有序整数组成的数列已放在一维数组中,下列给定程序函数 fun()的功能是:利用折半查找法查找整数 m 在数组中的位置。若找到,返回其下标值;否则,返回-1。 折半查找的基本算法是:每次查找前先确定数组中待查的范围 low 和 high(lowhigh),然后用 m 与中间位置(mid)上元素的值进行比较。如果 m 的值大于中间位置元素的值,则下一次的查找范围落在中间位置之后的元素中;反之,下一次的查找范同落在中间位置之前的元素中,直到 lowhigh,查找结束。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动 main()函

4、数,不得增行或删行,也不得更改程序的结构! 试题程序: #includestdio.h #define N 10 /*found* void fun(int a,int m) int low=0,high=N-1,mid; while(low=high) mid=(low+high)/2; if(mamid) high=mid-1; /*found* else if(mamid) low=mid+1; else return(mid); return(-1); void main() int i,aN=-3,4,7,9,13,45,67,89,100,180,k,m; printf(“a 数组

5、中的数据如下:“); for(i=0;iN;i+) printf(“%d“,ai); printf(“Enter m:“); scanf(“%d“, k=fun(a,m); if(k=0) printf(“m=%d,index=%d/n“,m,k); else printf(“Not be found!/n“); (分数:30.00)三、程序设计题(总题数:1,分数:40.00)3.请编写函数 fun,其功能是:找出一维整型数组元素中最大的值所在的下标,并通过形参传回。数组元素中的值已在主函数中赋予。 主函数中 x 是数组名,n 是 x 中的数据个数,max 存放最大值,index 存放最大值

6、所在元素的下标。 注意:部分源程序给出如下。 请勿改动主函数 main()和其他函数中的任何内容,仅在函数 fun()的花括号中填入你编写的若干语句。 试题程序: #includetime.h #includestdlib.h #includestdio.h void fun(int a,int n,int*max,int*d) void main() int i,x20,max,index,n=10; sranf(unsigned)time(NULL); for(i=0;i=n;i+) xi=rand()%50; printf(“%4d“,xi);/输出一个随机数组 printf(“/n“)

7、; fun(x,n, printf(“Max=%5d,Index=%4d/n“,max,index); (分数:40.00)_二级 C 语言-343 (1)答案解析(总分:100.00,做题时间:90 分钟)一、程序填空题(总题数:1,分数:30.00)1.请补充 main()函数,该函数的功能是求方程 ax 2 +bx+c=0 的两个实数根。方程的系数 a、b、c 从键盘输入,如果判别式(disc=b*b-4*a*c)小于 0,则要求重新输入 a、b、c 的值。 例如,当 a=1,b=2,c=1 时,方程的两个根分别是:x 1 =-1.00,x 2 =-1.00。 注意:部分源程序给出如下。

8、 请勿改动函数中的其他任何内容,仅在横线上填入所编写的若干表达式或语句。 试题程序: #includemath.h #includestdio.h void main() double a,b,c,x1,x2,disc; do printf(“Input a,b,c:“); scanf(“%1f,%1f,1f“, disc=b*b-4*a*c; if(disc0) printf(“disc=%1f/n Input again!/n“,disc); while( 1); printf(“*the result*/n“); x1=(-b+ 2(disc)/(2*a); x2=(-b- 3(disc

9、)/(2*a); printf(“/nx1=%6.21f/nx2=%6.21f/n“,x1,x2); (分数:30.00)解析:disc0 或 0disc sqrt sqrt解析 本题考查的是 do-while 循环,第 1 处填空:“disc0”或“0disc”;本题引入“#includemath.h”头文件可知,可调用函数 sqrt。二、程序改错题(总题数:1,分数:30.00)2.由 N 个有序整数组成的数列已放在一维数组中,下列给定程序函数 fun()的功能是:利用折半查找法查找整数 m 在数组中的位置。若找到,返回其下标值;否则,返回-1。 折半查找的基本算法是:每次查找前先确定数组

10、中待查的范围 low 和 high(lowhigh),然后用 m 与中间位置(mid)上元素的值进行比较。如果 m 的值大于中间位置元素的值,则下一次的查找范围落在中间位置之后的元素中;反之,下一次的查找范同落在中间位置之前的元素中,直到 lowhigh,查找结束。 请改正程序中的错误,使它能得出正确的结果。 注意:不要改动 main()函数,不得增行或删行,也不得更改程序的结构! 试题程序: #includestdio.h #define N 10 /*found* void fun(int a,int m) int low=0,high=N-1,mid; while(low=high) m

11、id=(low+high)/2; if(mamid) high=mid-1; /*found* else if(mamid) low=mid+1; else return(mid); return(-1); void main() int i,aN=-3,4,7,9,13,45,67,89,100,180,k,m; printf(“a 数组中的数据如下:“); for(i=0;iN;i+) printf(“%d“,ai); printf(“Enter m:“); scanf(“%d“, k=fun(a,m); if(k=0) printf(“m=%d,index=%d/n“,m,k); els

12、e printf(“Not be found!/n“); (分数:30.00)解析:错误:void fun(int a,int m) 正确:int fun(int a,int m)或 fun(int a,int m) 错误:else If(mamid) 正确:else if(mamid) 解析 本题考查的是折半查找算法、函数的定义和 ifelse 语句。首先,函数的定义需要考虑返回值的类型,返回值为 int 类型。本题需求值,所以函数的返回值不能是 void 类型。因为如果略去函数类型标识符,系统默认为 int 类型,所以函数定义应为“int fun(int a,int m)”或“fun(in

13、t a,int m)”;关键词 if 要区分大小写,写错系统将无法识别。三、程序设计题(总题数:1,分数:40.00)3.请编写函数 fun,其功能是:找出一维整型数组元素中最大的值所在的下标,并通过形参传回。数组元素中的值已在主函数中赋予。 主函数中 x 是数组名,n 是 x 中的数据个数,max 存放最大值,index 存放最大值所在元素的下标。 注意:部分源程序给出如下。 请勿改动主函数 main()和其他函数中的任何内容,仅在函数 fun()的花括号中填入你编写的若干语句。 试题程序: #includetime.h #includestdlib.h #includestdio.h vo

14、id fun(int a,int n,int*max,int*d) void main() int i,x20,max,index,n=10; sranf(unsigned)time(NULL); for(i=0;i=n;i+) xi=rand()%50; printf(“%4d“,xi);/输出一个随机数组 printf(“/n“); fun(x,n, printf(“Max=%5d,Index=%4d/n“,max,index); (分数:40.00)_正确答案:()解析:void fun(int a,int n,int*max,int*d) int i; *max=a0; *d=0; for(i=0;in;i+)/*将最大的元素放入指针 max 所指的单元,最大元素的下标放入 指针 d 所指的单元*/ if(*maxai) *max=ai;*d=i; 解析 本题考查的是 for 循环和指针变量。通过 for 循环实现对数组的遍历,形参 max 和 d 都是指针型变量,所以引用它们指向的变量时要进行指针运算。先有*max=a0,在遍历中如果遇到比 max 大的元素,则将 ai赋值给 max,下标 i 赋值给*d。循环语句用来遍历数组元素,然后通过条件语句比较数组元素找到最大值。

展开阅读全文
相关资源
猜你喜欢
  • ANSI T1.250-1996 Telecommunications – Operations Administration Maintenance and Provisioning (OAM&P) – Extension to Generic Network Information Model for Interfaces between Operatfi.pdf ANSI T1.250-1996 Telecommunications – Operations Administration Maintenance and Provisioning (OAM&P) – Extension to Generic Network Information Model for Interfaces between Operatfi.pdf
  • ANSI T1.257-1997 Telecommunications - Operations Administration Maintenance and Provisioning (OAM&P) - Traffic Management Services and Information Model for Interfaces between Oper.pdf ANSI T1.257-1997 Telecommunications - Operations Administration Maintenance and Provisioning (OAM&P) - Traffic Management Services and Information Model for Interfaces between Oper.pdf
  • ANSI T1.260-1998 Telecommunications - Operations Administration Maintenance and Provisioning (OAM&P) - Extension to Generic Network Information Model for Interfaces between Servicee.pdf ANSI T1.260-1998 Telecommunications - Operations Administration Maintenance and Provisioning (OAM&P) - Extension to Generic Network Information Model for Interfaces between Servicee.pdf
  • ANSI T1.331-1999 Telecommunications - Description of Above-Baseline Physical Threats to Telecommunications Links《电信.基线上对电信链路的物理性威胁的描述》.pdf ANSI T1.331-1999 Telecommunications - Description of Above-Baseline Physical Threats to Telecommunications Links《电信.基线上对电信链路的物理性威胁的描述》.pdf
  • ANSI T1.401.03-1998 Network to Customer Installation Interfaces - Analog Voicegrade Switched Access Lines with Calling Number Delivery Calling Name Delivery or Visual Message-Waiti.pdf ANSI T1.401.03-1998 Network to Customer Installation Interfaces - Analog Voicegrade Switched Access Lines with Calling Number Delivery Calling Name Delivery or Visual Message-Waiti.pdf
  • ANSI T1.401.04-2000 Network to Customer Installation Interfaces - Analog Voicegrade Switched Access Lines with Call Waiting Distinctive Call Waiting or Calling Identity Delivery on.pdf ANSI T1.401.04-2000 Network to Customer Installation Interfaces - Analog Voicegrade Switched Access Lines with Call Waiting Distinctive Call Waiting or Calling Identity Delivery on.pdf
  • ANSI T1.403.01-1999 Telecommunications - Network and Customer Installation Interfaces - Integrated Services Digital Network (ISDN) Primary Rate Layer 1 Electrical Interface Specifi.pdf ANSI T1.403.01-1999 Telecommunications - Network and Customer Installation Interfaces - Integrated Services Digital Network (ISDN) Primary Rate Layer 1 Electrical Interface Specifi.pdf
  • ANSI T1.403.02-1999 Network and Customer Installation Interfaces - DS1 Robbed-Bit Signaling State Definitions《电信.网络与用户装置接口.DSI金属接口》.pdf ANSI T1.403.02-1999 Network and Customer Installation Interfaces - DS1 Robbed-Bit Signaling State Definitions《电信.网络与用户装置接口.DSI金属接口》.pdf
  • ANSI T1.414-1998 TNetwork to Customer Installation Interfaces - Enhanced 911 Analog Voicegrade PSAP Access Using Loop Reverse-Battery Signaling《网络与用户的安装接口.使用环路反向电池信令的增强型911模拟音频PSAP.pdf ANSI T1.414-1998 TNetwork to Customer Installation Interfaces - Enhanced 911 Analog Voicegrade PSAP Access Using Loop Reverse-Battery Signaling《网络与用户的安装接口.使用环路反向电池信令的增强型911模拟音频PSAP.pdf
  • 相关搜索

    当前位置:首页 > 考试资料 > 职业资格

    copyright@ 2008-2019 麦多课文库(www.mydoc123.com)网站版权所有
    备案/许可证编号:苏ICP备17064731号-1