hw2- 找出下面两个程序中的错误

下面是第一题的题目:

Below are two faulty programs. Each includes a test case that results in failure.
Answer the following questions (in the next slide) about each program.

(1) Identify the fault.
(2) If possible, identify a test case that does not execute the
fault. (Reachability)
(3) If possible, identify a test case that executes the fault, but
does not result in an error state.
(4) If possible identify a test case that results in an error, but
not a failure

1.
public int findLast (int[] x, int y) {
//Effects: If x==null throw
NullPointerException
// else return the index of the last element
// in x that equals y.
// If no such element exists, return -1
for (int i=x.length-1; i > 0; i--)
{
if (x[i] == y)
{
return i;
}
}
return -1;
}
// test: x=[2, 3, 5]; y = 2
// Expected = 0

答:

(1)在这个程序中并没有测试到数x中的最开始的一个x[0],如果x[0]刚好等于y而之前的不等于那么结果就会出现偏差!

(2)text:x=[0,5,2]; y = 2

(3)text:x=[1,3,4]; y = 2

(4)text: x=[2,1,0]; y = 2

第二题:

public static int lastZero (int[] x) {
//Effects: if x==null throw
NullPointerException
// else return the index of the LAST 0 in x.
// Return -1 if 0 does not occur in x
for (int i = 0; i < x.length; i++)
{
if (x[i] == 0)
{
return i;
}
} return -1;
}
// test: x=[0, 1, 0]
// Expected = 2

答:

(1)这个程序只会显示第一个x=0的下标而不是最后一个

(2)case text: x=[0,1,2]

(3)case text: x=[1,2,2]

(4)case text:x=[0,0,0]

转载于:https://www.cnblogs.com/landi/p/6492486.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用STM32F103C8驱动HW-AD7606-F4,您需要了解以下步骤: 1. 硬件连接:将HW-AD7606-F4与STM32F103C8连接。确保将时钟信号(CLK)、片选信号(CS)、串行数据输出(DOUT)和串行数据输入(DIN)正确连接到STM32F103C8的相应引脚。 2. 配置STM32F103C8的GPIO:使用STM32的库函数或寄存器级编程,将相应的引脚配置为GPIO,并设置为输出或输入模式。 3. 配置SPI:使用STM32的库函数或寄存器级编程,初始化SPI外设,并设置SPI的主从模式、数据位宽、时钟极性和相位等参数。 4. 与AD7606-F4通信:使用SPI接口与AD7606-F4进行通信。根据AD7606-F4的通信协议,通过SPI发送配置命令和接收数据。 5. 数据处理:接收AD7606-F4传输的数据,并根据需要进行处理和解析。 下面是一个简单的示例代码,用于启动SPI并从AD7606-F4读取数据: ```c #include "stm32f10x.h" // 配置SPI void SPI_Configuration(void) { SPI_InitTypeDef SPI_InitStructure; // 使能SPI1时钟 RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE); // 配置SPI1引脚 // ... // SPI1配置 SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); // 使能SPI1 SPI_Cmd(SPI1, ENABLE); } // 通过SPI从AD7606-F4读取数据 uint16_t AD7606_ReadData(void) { uint16_t data; // 使能AD7606-F4的片选信号 // ... // 发送读取数据的命令 // ... // 等待接收数据完成 while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); // 读取接收到的数据 data = SPI_I2S_ReceiveData(SPI1); // 禁用AD7606-F4的片选信号 // ... return data; } int main(void) { // 初始化SPI SPI_Configuration(); while (1) { // 从AD7606-F4读取数据 uint16_t data = AD7606_ReadData(); // 处理数据 // ... // 延时一段时间 // ... } } ``` 请注意,这只是一个简单的示例代码,具体的实现可能需要根据您的具体需求进行调整和扩展。建议参考STM32F103C8的相关文档和寄存器级编程手册以获取更详细的信息和示例代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值