题目详情

阅读以下问题说明、C 程序和函数,将解答填入答题纸的对应栏内。

【问题 1】

分析下面的C 程序,指出错误代码(或运行异常代码)所在的行号。

【C 程序】

初级程序员,章节练习,基础复习,案例分析

【问题 2】

函数inputArr(int a[], int n)的功能是输入一组整数(输入0或输入的整数个数达到n时结束)存入数组 a,并返回实际输入的整数个数。函数 inputArr 可以成功编译。但测试函数调用inputArr后,发现运行结果不正确。

请指出错误所在的代码行号,并在不增加和删除代码行的情况下进行修改,写出修改正确后的完整代码行,使之符合上述设计意图。

【C 函数】

初级程序员,章节练习,基础复习,案例分析

正确答案及解析

正确答案
解析

【问题1】

5,或arrChar="test"

7,或*P='0';

【问题2】

初级程序员,章节练习,初级程序员模拟

【解析】

本题考查 C 程序编写和调试中常见错误的识别和改正。

【问题1】在C语言中,指针表示内存单元的地址,指针变量可用于存储指针类 型的值,即内存单元的地址值。变量的值在程序运行过程中允许修改,而常量则不允许修改。可以令指针指向一个变量或常量,但若指针指向一个常量,则不允许通过指针修改该常量。

第 5 行代码有错,即对数组名arrChar的赋值处理是错误的。在 C语言中,数组名是表示数组空间首地址的指针常量,程序中不允许对常量赋值。

第 7行代码有错,在第 6行中,通过 p = "testing"使指针变量指向了一个字符串常量,此后可以再令指针 p指向其他字符或字符串,但不能通过指针修改字符串常量的内容。

【问题2】该函数中出现的错误是编写 C程序时的常见错误。scanf 是 C标准库函数中的格式化输入函数,其原型如下:

int scanf(char *format ,...);

使用时,第一个实参是格式控制串,之后的实参是地址1,地址 2,…

在本题中,要求以十进制整数格式输入一个整数并存入 a[k] ,数组元素a[k]实质上一个整型变量,必须用"&"求得 a[k]的地址作为实参调用 scanf 函数,因此,第 4行出错,正确代码应为"scanf("%d&a[k]);"。

C程序中将相等运算符"="误用为赋值运算符"="也是常见的一个错误,由于"="也是合法的运算符并且 C语言中用 0 和非 0来表示逻辑假和逻辑真,因此在应产生逻辑值的地方产生了其他数值也可以,因此该错误通常只能用人工检查和排除。第 6 行的正确代码应为"if (k==n)break;"。

在该程序中,结束循环的一个条件是k等于n,另一个条件是输入的整数为0。另 外,do-whi1e的循环条件为真(非0)时要继续循环,因此,循环条件应该是判断输入的值不等于 0。观察循环体中与数组元素有关的部分,如下所示:

scanf("%d",&a[k]);

包含此试题的试卷

你可能感兴趣的试题

单选题

The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答5处

  • A.sensor
  • B.searching
  • C.service
  • D.source
查看答案
单选题

The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答4处

  • A.low-level
  • B.low-cost
  • C.high-cost
  • D.high-performance
查看答案
单选题

The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答3处

  • A.19th
  • B.20th
  • C.21th
  • D.22th
查看答案
单选题

The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答2处

  • A.path
  • B.Internet
  • C.route
  • D.switch
查看答案
单选题

The Internet of( 1)(IoT) describes physical objects that are embedded with Sensor,processing abilities,softwares,and other technologies that connect with other devices and systems over the ( 2) or other communication and exchange data networks .Over the past few years, IoT has become one of the most important technologies of the ( 3 ) centuryWe can connect objects to the Internet via embedded devices. By means of( 4) computing, the cloud, big data, and mobile technologies, physical things can share and collect data with minimal human intervention. Traditional fields of embedded systems,wireless ( 5) networks (WSNs),control systems,automation, independently and collectively enable IoT.回答1处

  • A.this
  • B.thing
  • C.think
  • D.things
查看答案

相关题库更多 +