题目详情

阅读以下C代码,回答问题1至问题3,将解答填入答题纸的对应栏内。

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

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

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

1.【问题1】

对于【C代码1】,写出下面的函数调用后x1、x2和x3的值。

(1)x1=adjustSalary(1,25,9000.0);

(2)x2=adjustSalary(8,32,7000.0);

(3)x3=adjustSalary(5,41,5000.0)。

2.【问题2】

(1)写出函数调用为foo(1)的输出结果;

(2)写出函数调用为foo(5)的输出结果;

(3)写出函数调用为foo(10)的输出结果;

(4)写出函数调用为foo(25)的输出结果。

3.【问题3】

(1)简要说明【C代码3】中的函数getSum的功能;

(2)程序员小王将函数getSum改写后得到下面的函数getSum_v2(即用for语句来描述循环)。请问,getSum_v2的功能是否与【C代码3】的getSum完全相同,若不同,请说明原因。

正确答案及解析

正确答案
解析

【问题1】(1)x1=9000.000000

(2)x2=14000.000000

(3)x3=6000.000000

解析:(1)对于x1,service_year=1,age=25,salary=9000.0,首先判断service_year<=5,因此再判断age<30,不进行任何运算,salary仍为9000.0,由于salary为float类型数据,因此输出为9000.000000。

(2)对于x2,service_year=8,age=32,salary=7000.0,首先判断service_year>5,因此直接进行else中的运算,salary=7000.0×2.0=14000.0,由于salary为float类型数据,因此输出为14000.000000。

(3)对于x3,service_year=5,age=41,salary=5000.0,首先判断service_year<=5,因此再判断age>30,进行运算salary=5000.0×1.2=6000.0,由于salary为float类型数据,因此输出为6000.000000。

【问题2】(1)foo(1):输出为两行,第一行是Cent,第二行是Nickel。

(2)foo(5):Nickel。

(3)foo(10):输出为两行,第一行是Dime,第二行是Quarter。

(4)foo(25):Quarter。

解析:(1)foo(1):coin=1,执行printf("Cent\n"),输出Cent并回车,继续执行printf("Nickel\n"),输出Nickel并回车,再执行break,结束foo函数。

(2)foo(5):coin=5,执行printf("Nickel\n"),输出Nickel并回车,再执行break,结束foo函数。

(3)foo(10):coin=10,执行printf("Dime\n"),输出Dime并回车,继续执行printf("Quarter\n"),输出Quarter并回车,结束foo函数。

(4)foo(25):coin=25,执行printf("Quarter\n"),输出Quarter并回车,结束foo函数。

【问题3】(1)函数getSum是求n个不小于0的整数之和。

(2)函数getSum_v2的功能与函数getSum不同,getSum是求n个不小于0的整数之和,计算和的数目总数是n个,而getSum_v2是总共输入了n个数,这n个数中有大于等于0,也有小于0的数,最终只把大于等于0的数进行求和,因此最终计算的和的个数有可能是小于n的,即二者的计算结果可能不同。

解析:(1)函数getSum是求n个不小于0的整数之和。

(2)函数getSum_v2的功能与函数getSum不同,原因:在getSum中,当t<0时,继续运行scanf函数获得t,而不会运行i++运算,i的值不会改变,最终会获得n个不小于0的整数并求和;而在getSum_v2中,当t<0时,首先进行i++,再继续scanf函数,因此当输入的数出现负值时,会导致最终运算的值可能少于n个。

你可能感兴趣的试题

单选题

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
查看答案

相关题库更多 +